我完全陷入了困境。不管我做什么,编译器都认为我在上一个printf上需要一个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: %.1f\n", 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"); }
代码运行得很好,并且可以完成我期望的所有工作。但是编译器似乎认为这有问题。
答案 0 :(得分:6)
您的while循环位于do语句内。 如果你在做
do while
它应该看起来像这样:
do {
} while (...);