Dan*_*ith 0 c printf while-loop do-while
我完全难过了.无论我做什么,我的编译器都认为我需要在上一次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"); }
Run Code Online (Sandbox Code Playgroud)
代码运行得很好,并且做了我期望它做的一切.但编译器似乎认为它有问题.
小智 6
你的while循环在do语句中.如果你在做
do while
Run Code Online (Sandbox Code Playgroud)
它应该是这样的:
do {
} while (...);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71 次 |
| 最近记录: |