C - main{} 括号上的语法错误,"expected while" ?



我完全被难住了。无论我做什么,我的编译器都认为我需要在最后一次打印时循环?它只是不断要求没有意义的事情。当我将它们添加到其中时,它只是告诉我它不应该在那里,当我删除它时,我被告知我需要它。

int main() {
float userInput; //The grades that the user inputs.
int passingGrade = 0; //Number of passing grades
int failingGrade = 0; //Failing grades
int invalidGrade = 0; //Invalid grades
//This do for loop is simply counting the number of grades of each catagory posted.
do {
printf("Enter a grade (Enter -1 to quit):");
scanf("%f", &userInput);
printf("You entered: %.1fn", userInput);
if ((userInput > 100) || (userInput < 0)) { //Over 100 is impossible.
invalidGrade = invalidGrade + 1;
printf("You input an impossible grade!n");
}
else {
if (userInput > 70) { //Greater than 70 means passing.
passingGrade = passingGrade + 1;
}
else {
if ((userInput < 70) && !(userInput < 0)) {
failingGrade = failingGrade + 1;
}
}
}
while (userInput != -1);
}
printf("You entered %i passing grades.n", passingGrade);
printf("You entered %i failing grades.n", failingGrade);
printf("You entered %i invalid grades.n", invalidGrade);
system("pause"); }

代码运行良好,并完成了我期望它执行的所有操作。但是编译器似乎认为它有问题。

你的 while 循环在 do 语句内。 如果你正在做

do while

它应该看起来像这样:

do {
} while (...);

最新更新