GPA计算程序

时间:2015-12-10 20:20:09

标签: c

我正在编写一个程序来计算GPA,我应该允许用户从键盘手动输入信息(课程名称,学分和字母等级)或从文件中读取。如果我选择从文件中读取它可以正常工作,但如果选择手动输入,则在完成输入信息后程序崩溃。我不知道是什么问题。如果您需要额外信息,请告诉我。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int main(){
    int i, count=0, credit[10], sumCredit = 0;
    double gpa, points[10], sumPoints = 0.0;
    char course[10][12], letter[10][2], fromfile;
    FILE *in, *out;

    printf("Do you want to read from a file? [Y/N]: ");
    scanf(" %c", &fromfile);
    if(fromfile == 'Y' || fromfile == 'y'){

        in = fopen("grades.txt", "r");
        if(in == NULL){
            printf("Error: Failed to open scores.txt\n");
            exit(1);
        }
        out = fopen("GPA.txt", "a");
        i=0;
        while(fscanf(in, "%s%d%s", course[i], &credit[i], letter[i]) != EOF){
            printf("%s\t%d\t%s\n", course[i], credit[i], letter[i]); 
            count++;
            i++;
            }

    }
    else if(fromfile == 'N' || fromfile == 'n'){

        for(i=0; strcmp(course[i-1], "0") ; i++){
             printf("Enter the name of the #%d course: ", i+1);
             scanf("%s", course[i]);
             if(strcmp(course[i], "0")){

             printf("Enter the credit hours for %s course: ", course[i] );
             scanf("%d", &credit[i]);

             printf("Enter your grade in %s course: ", course[i]);
             scanf(" %s", letter[i]);
             count++;}


        }
    }


     for(i=0; i<count; i++){

        if(!strcmp(letter[i], "a+")){
            points[i] = 4 * credit[i];
        }   
        else if(!strcmp(letter[i], "a"))
            points[i] = 3.75 * credit[i];
        else if(!strcmp(letter[i], "b+"))
            points[i] = 3.5 * credit[i];
        else if(!strcmp(letter[i], "b"))
            points[i] = 3 * credit[i];
        else if(!strcmp(letter[i], "c+"))
            points[i] = 2.75 * credit[i];
        else if(!strcmp(letter[i], "c"))
            points[i] = 2.5 * credit[i];
        else if(!strcmp(letter[i], "d+"))
            points[i] = 2 * credit[i];
        else if(!strcmp(letter[i], "d"))
            points[i] = 1.5 * credit[i];
        else if(!strcmp(letter[i], "f"))
            points[i] = 0 * credit[i];


     }
     fprintf(out, "\ncourse\thours\tletter grade\tpoints\n\n");
     printf("\ncourse\t\thours\tletter grade\tpoints\n\n");

     for(i=0; i < count; i++){
        fprintf(out, "%s\t\t%d%13s%15.2lf\n", course[i], credit[i], letter[i], points[i]);
        printf("%s\t\t%d%13s%15.2lf\n", course[i], credit[i], letter[i], points[i]);
        sumPoints += points[i];
        sumCredit += credit[i];

     }
     gpa = sumPoints / sumCredit;
     printf("your GPA = %0.2lf", gpa);
     fprintf(out, "your GPA = %0.2lf", gpa);
    fclose(in);
    fclose(out);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

以下行条件的逻辑是错误的。

for(i=0; strcmp(course[i-1], "0") ; i++){

应该是:

for(i=0; strcmp(course[i-1], "0") == 0 ; i++){
                                  ^^^^

但是,更重要的错误是您将在循环的第一次迭代中最终使用course[-1]。这导致了未定义的行为。

您需要稍微调整循环的逻辑。类似的东西:

// Don't exceed the limit of the valid indices for course.
for (i=0; i < 10  ; i++)
{
   printf("Enter the name of the #%d course: ", i+1);
   scanf("%s", course[i]);

   // If the condition to stop is met, break out of the loop.
   if(strcmp(course[i], "0") == 0 )
   {
      break;
   }

   printf("Enter the credit hours for %s course: ", course[i] );
   scanf("%d", &credit[i]);

   printf("Enter your grade in %s course: ", course[i]);
   scanf(" %s", letter[i]);
   count++;
}