2016-12-07 16 views
0

我的代码要求我从一个.txt文件输入姓名,然后如果名称是有效的,我可以登录。不是让我在我的信息系统项目进入了一个学生的特定等级

当程序启动应该给用户三个选项; 按0退出 按1作者信息 按2 选择0退出程序 选择1打印您的姓名和学号 选择3,提示用户输入用户名和密码登录。 然后你应该打开文件“密码”,看看用户是否有权使用该程序*

我已经完成了这项工作。一旦我成功登录后,我可以按选项2添加新学生。到目前为止,我已经做到了。现在我对选项4犯了一个错误。选项4要求我为学生输入一个等级。在选择4后,它会要求我评分,只要我输入一个等级,该程序就会崩溃。我不确定我的错误是什么。

//This is my code: 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

/* Constants used in the application */ 
const char * PASSWORD_FILE = "passwords.txt"; 
const int MAX_STUDENTS = 1000; 
const int MAX_GRADES = 100; 
const int MAX_CHARS = 25; 

//Function to print Author Info 
void printAuthorInfo(); 

//Function to display initial Menu and Get the User's Selection 
int showMainMenu(); 

//Function to Login to the System 
int login(); 

//member menu 
int memberMenu(); 

//Enter GPA for current Student 
void enterGPA(char ids[1000][25], float grades[1000][100], int[], int); 

//Display student records to stdout 
void print(char[1000][25], char[1000][25], char[1000][25], float[1000][100],  
int[], int); 

//Save Records to File 
void save(char[1000][25], char[1000][25], char[1000][25], float[1000][100],  
int[], int); 

//Load Records from File 
void load(char fn[1000][25], char ln[1000][25], char ids[1000][25], float 
grades[1000][100], int counts[], int num); 

int main() 
{ 
    int loggedIn = 0; 
    int choice, subChoice; 
    char firstNames[1000][25]; 
    char lastNames[1000][25]; 
    char ids[1000][25]; 
    float grades[1000][100]; 
    int numGrades[1000]; 
    char fn[25]; 
    char ln[25]; 
    int numStudents = 0; 

    //Top Level Loop 
    do { 
     //display menu and get the user's selection 
     choice = showMainMenu(); 

     if (choice == 0) 
     { 
      printf("Thank you for using our Application! GoodBy!"); 
     } 
     else if (choice == 1) 
     { 
      printAuthorInfo(); 
     } 
     else if (choice == 2) 
     { 
      loggedIn = login(); 

      if (loggedIn == 1) 
      { 
       printf("Welcome! You are now Logged In\n"); 

       //Interact with the user 
       do 
       { 
        subChoice = memberMenu(); 

        if (subChoice == 1) 
        { 
         printAuthorInfo(); 
        } 
        else if (subChoice == 2) 
        { 
         printf("Enter First Name: "); 
         scanf("%s", firstNames[numStudents]); 
         printf("Enter Last Name: "); 
         scanf("%s", lastNames[numStudents]); 
         printf("Enter Student ID: "); 
         scanf("%s", ids[numStudents]); 
         numStudents++; 
         printf("Student Has been added to System"); 
        } 
        else if (subChoice == 3) 
        { 
         enterGPA(ids, grades, numGrades, numStudents); 
        } 
        else if (subChoice == 4) 
        { 
         print(firstNames, lastNames, ids, grades, numGrades, 
    numStudents); 
        } 
        else if (subChoice == 5) 
        { 
         void save(firstNames, lastNames, ids, grades, counts, 
    numStudents); 
        } 
        else if (subChoice == 6) 
        { 
         load(firstNames, lastNames, ids, grades, numGrades, 
    &numStudents); 
        } 
        else if (subChoice == 7) 
        { 
         printf("\nYou are logged Out Now"); 
        } 
        printf("\n"); 
       } while (subChoice != 7); 
      } 
      else 
      { 
       printf("Login Failed - Try again"); 
      } 
     } 
     printf("\n\n"); 
    } while (choice != 0); 


    system("pause"); 
    return 0; 
} 

void printAuthorInfo() 
{ 
    printf("Author Information\n"); 
    printf("Author Name: %s\n", "myinfo "); 
    printf("Student ID: %s\n", "12345"); 
} 

int showMainMenu() 
{ 
    //User Selection 
    int choice; 

    do 
    { 
     //Display menu 
     printf("Press 0 to exit\n"); 
     printf("Press 1 for Author Info\n"); 
     printf("Press 2 for Login\n"); 
     printf("Enter choice? "); 

     //Read the selection 
     scanf("%d", &choice); 
     printf("\n"); 
    } while (choice < 0 || choice > 2); 

    //Return selection 
    return choice; 
} 

int login() 
{ 
    //Get User's username and password 
    char username[30]; 
    char password[30]; 
    char usernameInFile[30]; 
    char passwordInFile[30]; 
    int valid = 0; 

    //Prompt and get Username 
    printf("Enter Username: "); 
    scanf("%s", username); 

    //Prompt and get Password 
    printf("Enter Password: "); 
    scanf("%s", password); 

    //Open the input file 
    FILE * fptr = fopen(PASSWORD_FILE, "r"); 

    while (fscanf(fptr, "%s %s", usernameInFile, passwordInFile) == 2) 
    { 
     if (strcmp(username, usernameInFile) == 0 && strcmp(password, 
    passwordInFile) == 0) 
     { 
      valid = 1; 
     } 
    } 

    fclose(fptr); 
    return valid; 
} 


int memberMenu() 
{ 
    int choice = 0; 

    do 
    { 
     printf("Press 1 for author info\n"); 
     printf("Press 2 to Enter new student\n"); 
     printf("Press 3 to enter grade for existing student\n"); 
     printf("Press 4 to print student records\n"); 
     printf("Press 5 to save student records\n"); 
     printf("Press 6 to load student records\n"); 
     printf("Press 7 to logout\nEnter Selection? "); 
     scanf("%d", &choice); 
     printf("\n"); 

    } while (choice < 1 || choice > 7); 

    return choice; 
} 

void print(char fn[1000][25], char ln[1000][25], char ids[1000][25], float 
    grades[1000][100], int counts[], int num) 
{ 
    int i, j; 
    float sum, avg; 
    sum = 0; 
    avg = 0; 

    printf("%-15s %-15s %-10s %-8s %-8s\n", "First Name", 
     "Last Name", "Std ID", "GPA", "Grades"); 

    for (i = 0; i < num; i++) 
    { 
     printf("%-15s %-15s %-10s", fn[i], ln[i], ids[i]); 

     for (j = 0; j < counts[i]; j++) 
     { 
      sum += grades[i][j]; 
     } 

     avg = sum/counts[i]; 
     sum = 0; 
     printf("%-8.2f", avg); 
     for (j = 0; j < counts[i]; j++) 
     { 
      printf("%-6.2f", grades[i]); 
     } 
     printf("\n"); 
    } 
} 

void save(char fn[1000][25], char ln[1000][25], char ids[1000][25], float 
    grades[1000][100], int counts[], int num) 
{ 
    int i, j; 
    float sum, avg; 
    char fname[100]; 
    sum = 0; 
    avg = 0; 

    //Get file name 
    printf("Enter Output File Name: "); 
    scanf("%s", fname); 

    FILE * fptr = fopen(fname, "w"); 

    fprintf(fptr, "%d\n", num); 
    for (i = 0; i < num; i++) 
    { 
     fprintf(fptr, "%s %s %s %d ", fn[i], ln[i], ids[i], counts[i]); 

     for (j = 0; j < counts[i]; j++) 
     { 
      fprintf(fptr, "%.2f ", grades[i]); 
     } 
     fprintf(fptr, "\n"); 
    } 

    fclose(fptr); 
} 

void load(char fn[1000][25], char ln[1000][25], char ids[1000][25], float 
    grades[1000][100], int counts[], int * num) 
{ 
    char fname[100]; 
    int cur = *num; 
    int i, j, n, numStudents = 0; 
    float grade; 

    //prompt and get input file 
    printf("Enter Input File Name: "); 
    scanf("%s", fname); 

    //open file to read 
    FILE * fptr = fopen(fname, "r"); 
    fscanf(fptr, "%d", &numStudents); 
    for (i = 0; i < numStudents && cur < 1000; i++) 
    { 
     fscanf(fptr, "%s %s %s %d", fn[cur], ln[cur], ids[cur], &n); 
     for (j = 0; j < n && counts[i] < 100; j++) 
     { 
      fscanf(fptr, "%f", &grade); 
      grades[i][counts[i]] = grade; 
      counts[i] += 1; 
     } 
     cur += 1; 
    } 

    *num = cur; 
    fclose(fptr); 

} 

void enterGPA(char ids[1000][25], float grades[1000][100], int counts[], int 
    num) 
{ 
    char id[25]; 
    int i, idx; 
    float gpa; 

    idx = -1; 

    printf("Enter Student ID: "); 
    scanf("%s", id); 

    //find student 
    for (i = 0; i < num && idx == -1; i++) 
    { 
     if (strcmp(id, ids[i]) == 0) 
     { 
      idx = i; 
     } 
    } 

    if (idx >= 0) 
    { 
     printf("Enter Grade: "); 
     scanf("%f", &gpa); 
     grades[idx][counts[idx]] = gpa; 
     counts[idx] += 1; 
    } 
    else 
    { 
     printf("NO Such Student Exist In The System"); 
    } 
} 
+1

[如何调试小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –

+0

@BJMyers这对他们来说很难调试,因为这比20行大一点,但建议仍然适用。 – byxor

回答

0

有两个问题,我可以立即看到。

  1. 在打印功能

    你有

    printf("%-6.2f", grades[i]; 
    

    由于grades是一个二维数组,它应该是

    printf("%-6.2f", grades[i][j] 
    
  2. 函数声明load不正确。它应该是

    void load(char fn[1000][25], char ln[1000][25], char ids[1000][25], 
        float grades[1000][100], int counts[], int *num); 
    

    (注意int* num

可能还有其他的问题,但没有输入文本文件也很难找出来。

+0

这并没有让我能够输入学生的成绩。 – Andrey

相关问题