在C

时间:2016-10-19 20:39:59

标签: c

我完全被这个问题困住了。我正在尝试创建一个简单的成绩计算器。是的,这是一项学校作业。然而,有些事情被称为截止日期,我的老师让他们无法满足,所以我已经完成了我的大部分工作计划。但我是一个好奇的人,我的老师无法回答我的问题 - 所以我在这里。

在我的项目中,一切运作良好;但是,在打印输出时,第一个学生的名字永远不会完全显示。我不能为我的生活开始理解每个其他循环如何产生名称而没有问题,但第一次迭代。究竟我做错了什么?

我相信我的问题在我的变量声明中或在for语句中。任何帮助将不胜感激,请随意批评。我不会以其他方式学习。

    for (int m=0; m<numstudents; m++) {
        printf("\n\n*****************\n\n***Report card***\n\n*****************");
        printf("\nName of student: %s", namestudent[m]);
        printf("\nPhysics: %.2f/100", physicsstudent[m]);
        printf("\nChemistry: %.2f/100", chemistrystudent[m]);
        printf("\nMath: %.2f/100", physicsstudent[m]);

        total = physicsstudent[m]+chemistrystudent[m]+mathstudent[m];
        realtotal = (total/300)*100;
        printf("\nTotal: %.2f/300", total);

        if(realtotal > 89) {
            printf("\nYour letter grade: A");
        } else if(realtotal > 79) {
            printf("\nYour letter grade: B");
        } else if(realtotal > 69) {
            printf("\nYour letter grade: C");
        } else if(realtotal > 49) {
            printf("\nYour letter grade: D");
        } else if((realtotal) < 50) {
            printf("\nYour letter grade: F");
        } 
    }

整个代码:

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

int main() {
  char line[100][100], namestudent[20][20];
int numstudents, grade;
float realtotal, total, physicsstudent[100], chemistrystudent[100], mathstudent[100];

  printf("\n***Welcome to the grade calculator***\n\n=>Press G to enter grade calculator\n\n=>Press H for help\n\nPlease enter your option: ");
char userinput = getchar();

if (userinput == 'g' || userinput == 'G') {
    printf("\nWelcome to the grade calculator\nEnter the total number of students in your class: ");
    scanf("%d", &numstudents);

    if (numstudents <= 0) {
        printf("Terminating program");
        return(0);
    }

    for (int i=0; i<numstudents; i++) {
        printf("\nPlease enter the name of student %d: ", i+1);
        fgets(namestudent[i], sizeof(namestudent[i]), stdin);
          namestudent[strlen(namestudent[i])-1][i] = '\0';
          scanf("%s", namestudent[i]);

          printf("Please enter %s\'s Physics score (out of 100 points): ", namestudent[i]);
        scanf("%f", &physicsstudent[i]);

        printf("Please enter %s\'s Chemistry score (out of 100 points): ",namestudent[i]);
        scanf("%f", &chemistrystudent[i]);

        printf("Please enter %s\'s Math score (out of 100 points): ",namestudent[i]);
        scanf("%f", &mathstudent[i]);

    }

    for (int m=0; m<numstudents; m++) {
        printf("\n\n*****************\n\n***Report card***\n\n*****************");
        printf("\nName of student: %s", namestudent[m]);
        printf("\nPhysics: %.2f/100", physicsstudent[m]);
        printf("\nChemistry: %.2f/100", chemistrystudent[m]);
        printf("\nMath: %.2f/100", physicsstudent[m]);

        total = physicsstudent[m]+chemistrystudent[m]+mathstudent[m];
        realtotal = (total/300)*100;
        printf("\nTotal: %.2f/300", total);

        if(realtotal > 89) {
            printf("\nYour letter grade: A");
        } else if(realtotal > 79) {
            printf("\nYour letter grade: B");
        } else if(realtotal > 69) {
            printf("\nYour letter grade: C");
        } else if(realtotal > 49) {
            printf("\nYour letter grade: D");
        } else if((realtotal) < 50) {
            printf("\nYour letter grade: F");
        } 
    }

} else if (userinput == 'h' || userinput == 'H') {
    printf("~~~Help~~~\n\n*After typing G, you will be entering the grade calculator\n\n1.Then, you will be asked to enter the number of students that you are going to enter grades for.\nThis will be an integer value only.\n\n2.If you enter any other character (other thaninteger), you must prompt to the user to enter a valid integer number only.\n\n3.Then the calculator asks you for the names of the students (if you entered 3 in the step 1, the loop will run for 3 times....taking 3 student names).\n\n4.Then the program will start asking for the individual scores (physics, chemistry and math in our case).\n\n5.Finally, it will print a report card with scores and the grade (calculated). I have programmed to give A, B, C and F grades only.You are free to use other grading scales as well.");
} else {
    printf("You entered a value that is not accepted. Terminating program");
}
  return(0);
}

结果:

*****************

***Report card***

*****************
Name of student: a    <--- input was 'apple'
Physics: 90.00/100
Chemistry: 90.00/100
Math: 90.00/100
Total: 270.00/300
Your letter grade: A

*****************

***Report card***

*****************
Name of student: orange
Physics: 90.00/100
Chemistry: 90.00/100
Math: 90.00/100
Total: 270.00/300
Your letter grade: A

*****************

***Report card***

*****************
Name of student: banana
Physics: 90.00/100
Chemistry: 90.00/100
Math: 90.00/100
Total: 270.00/300
Your letter grade: A

Process exited with code: 0

2 个答案:

答案 0 :(得分:0)

下面的fgets和/或作业正在捣乱;把它们拿走,然后像其他人一样使用scanf

答案 1 :(得分:0)

通话后

scanf("%d", &numstudents);

换行符仍在输入流中。下一次调用fgets

    fgets(namestudent[i], sizeof(namestudent[i]), stdin);

返回一个空行,对应i = 0

您需要添加代码以忽略上述scanf行之后的其余部分。

   int c;
   while ( (c = getchar()) != EOF && c != '\n');