因此,我遇到以下代碼的問題,並且在編譯時遇到運行時錯誤。基本上,這是一個程序,輸入不確定數量的學生和每個學生未確定的測試分數,然後計算每個學生的GPA並將其顯示回給用戶。我使用的樣品輸入如下: 約翰尼 一個 乙C中的Char問題
#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...
它只是一直走下去,這樣的...
什麼是錯誤? – tonysdg
它只是說「運行時錯誤」,並沒有提供任何細節 – PorkchopDonut
假設你使用GCC,用'-Wall'和'-Wextra'編譯。除了Sourav Ghosh的建議外,就是這樣。 – tonysdg