所以我遇到了以下代码的问题,并且在编译时我一直遇到运行时错误。基本上,这是一个程序,用于输入未确定数量的学生和每个学生未确定的测试分数,然后计算每个学生的GPA并将其显示给用户。我使用的样本输入如下: 约翰尼 一个 乙
#include <stdio.h>
int main(void)
{
char StudentName[50];
char UserInput;
int GradeSum;
int TotalGrades;
float GPA;
int Test;
int Try;
Test = 0;
while ( Test != 1 )
{
printf ("Enter the student’s name...\n");
scanf ("%49s", StudentName);
Try = strcmp(StudentName, "");
if (Try != 0)
{
GradeSum = 0;
TotalGrades = 0;
GPA = 0;
while ( Test != 1 )
{
printf ("Enter the student’s letter grade...\n");
scanf (" %c", &UserInput);
if ((UserInput == 'A') || (UserInput == 'B') || (UserInput == 'C') || (UserInput == 'D') || (UserInput == 'F'))
{
if (UserInput == 'A')
{
GradeSum += 4;
TotalGrades += 1;
}
else if (UserInput == 'B')
{
GradeSum += 3;
TotalGrades += 1;
}
else if (UserInput == 'C')
{
GradeSum += 2;
TotalGrades += 1;
}
else if (UserInput == 'D')
{
GradeSum += 1;
TotalGrades += 1;
}
else if (UserInput == 'F')
{
TotalGrades += 1;
}
}
else
{
printf ("That is not a valid letter grade...\n");
}
}
GPA = ((float)GradeSum) / TotalGrades;
printf ("%s: %f\n", StudentName, GPA);
}
else
{
break;
}
}
return 0;
}
编辑:我向我建议了调整,我仍然遇到运行时错误,输出如下:
Enter the student’s name...
Enter the student’s letter grade...
Enter the student’s letter grade...
Enter the student’s letter grade...
Enter the student’s letter grade...
Enter the student’s letter grade...
Enter the student’s letter grade...
它只是继续这样......
答案 0 :(得分:1)
首先,而不是
==
添加
scanf ("%c", &UserInput);
就足够了。
然后,无法使用 scanf (" %c", &UserInput);
运算符比较数组的内容。您需要使用strcmp()
。
之后,更改
GPA = ((float)GradeSum) / TotalGrades;
到
xtail
避免以前存储的换行符。
最后,要强制执行浮点除法,可以使用强制转换,例如
tornado.proces.Subprocess
答案 1 :(得分:0)
编辑,为我解决了问题,根本没有运行时错误......
Enter the studentÆs name...
Jane
Enter the student's letter grade...
A
Enter the student's letter grade...
B
Enter the student's letter grade...
A
Enter the student's letter grade...
A
Enter the student's letter grade...
A
Enter the student's letter grade...
1
That is not a valid letter grade...
Jane: 3.800000
Enter the student's name...
代码我跑了....
#include <stdio.h>
int main(void)
{
char StudentName[50];
char UserInput;
int GradeSum;
int TotalGrades;
float GPA;
int Test;
int Try;
Test = 0;
while ( Test != 1 )
{
printf ("Enter the student’s name...\n");
scanf ("%49s", StudentName);
Try = strcmp(StudentName, "");
if (Try != 0)
{
GradeSum = 0;
TotalGrades = 0;
GPA = 0;
while ( Test != 1 )
{
printf ("Enter the student’s letter grade...\n");
scanf (" %c", &UserInput);
if ((UserInput == 'A') || (UserInput == 'B') || (UserInput == 'C') || (UserInput == 'D') || (UserInput == 'F'))
{
if (UserInput == 'A')
{
GradeSum += 4;
TotalGrades += 1;
}
else if (UserInput == 'B')
{
GradeSum += 3;
TotalGrades += 1;
}
else if (UserInput == 'C')
{
GradeSum += 2;
TotalGrades += 1;
}
else if (UserInput == 'D')
{
GradeSum += 1;
TotalGrades += 1;
}
else if (UserInput == 'F')
{
TotalGrades += 1;
}
}
else
{
printf ("\nThat is not a valid letter grade...\n");
break;
}
}
GPA = ((float)GradeSum) / TotalGrades;
printf ("\n\n%s: %f\n", StudentName, GPA);
}
else
{
break;
}
}
return 0;
}
答案 2 :(得分:0)
你永远不会实际设置Test的值,因此它将以无限循环运行。
答案 3 :(得分:0)
有一些语法错误,但我最大的问题是我使用的在线编译器。将代码插入另一个允许我实时输入数据的代码,它工作得很好。生活和学习。