【发布时间】:2014-03-20 10:09:13
【问题描述】:
函数 computeScore() 用于使用结构数组创建一个最多包含 50 名学生的数据库。该函数接收学生、姓名、考试成绩和考试成绩,计算总分,然后打印出来。当学生姓名为“END”时,输入将结束。之后,程序将计算总平均分或所有学生并打印出来。
当我输入第一个学生的信息时效果很好,但是当程序第二次进入 while 循环时我遇到了麻烦(当我想输入第二个学生的信息时)。
这是我到目前为止所做的:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <math.h>
struct student{
char name[20]; /* student name */
double testScore; /* test score */
double examScore; /* exam score */
double total; /* total score = test+exam scores */
};
void computeScore();
int main()
{
computeScore();
return 0;
}
void computeScore()
{
struct student info[50];
int count = 1;
double allStudent = 0, allStudentAvg;
while (info[count].name != 'END')
{
printf("\nEnter student name: ");
gets(info[count].name);
printf("Enter test score: ");
scanf("%lf", &info[count].testScore);
printf("Enter exam score: ");
scanf("%lf", &info[count].examScore);
info[count].total = (info[count].testScore + info[count].examScore) / 2;
printf("Student %s's total= %f\n", info[count].name, info[count].total);
allStudent += info[count].total;
count++;
}
allStudentAvg = allStudent / count;
printf("Overall average = %f", allStudentAvg);
}
预期输出:
Enter student name: John Doe
Enter test score: 34
Enter exam score: 46
Student John Doe's total = 40.000000
Enter student name: Jane Doe
Enter test score: 60
Enter exam score: 80
Student John Doe's total = 70.000000
Enter student name: END
Overall average: 55.000000
我得到的输出是:
Enter student name: John Doe
Enter test score: 34
Enter exam score: 46
Student John Doe's total = 40.000000
Enter student name: Enter test score:
\\Program skipped the input of 2nd student name
【问题讨论】: