#include <iostream>
using namespace std;
int grade_calculation (int grade_input){
if ( grade_input >= 100 && grade_input <= 90){
cout << 'A';
} else if (grade_input >= 89 && grade_input <= 80){
cout << 'B';
} else if (grade_input >= 79 && grade_input <= 70){
cout << 'C';
} else if (grade_input >= 69 && grade_input <= 60){
cout << 'D';
} else if (grade_input >= 59 && grade_input <= 0){
cout << 'F';
} else {
cout << "NOT VALID.";
}
}
int main()
{
int score = 100;
int grade_input;
cout << "Enter a grade: ";
cin >> grade_input;
while (grade_input != score){
cout << "Enter a new grade: ";
cin >> grade_input;
grade_calculation(grade_input);
} cout << grade_input;
return 0;
}
How can I properly use the while loop to ask the user for a letter and then output the corresponding grade? Is there another way to call the function besides using "grade_calculation (grade_input)"?
if ( grade_input >= 100 && grade_input <= 90)
This means: if the grade is above 100 and the grade is below 90 . There is no grade that satisfy this condition. You probably want:
if ( grade_input <= 100 && grade_input >= 90)
which is: if grade is below or equal to 100 and grade is equal or above 90 .
And then, change all other conditions in a similar way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.