Write C Program to ask user to enter any three grade then pr
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
#include <string>
#include <sstream>
using namespace std;
int main() // driver method
{
cout << \"Please Enter the three Grades you Recieved. \"; // prompt for the data to be entered by the user
int grade; // locla variables
string input;
for(int i = 1; i <= 3; i++) { // iterate over the loop for 3
cout << \"\ Enter the Grade \" << i << \" : \" ; // message
while(true) { // check for the valid data
getline(cin,input); //Recieve the grade values from user.
stringstream temp(input);
if (temp >> grade) // check the stream of the data
{
if (grade >= 90 && grade <= 100) { // check for the data witht hte given consitions
cout << \"Congratulations! You Secured an A grade!\"; // print the message to user
} else if (grade >= 80 && grade < 90) {
cout << \"Good! You are above average with a B grade!\";
} else if (grade >= 70 && grade < 80) {
cout << \"You are average with a C grade!\";
} else if (grade >= 60 && grade < 70) {
cout << \"You below the average with a D grade!\";
} else if (grade >= 0 && grade < 60) {
cout << \"OOPS..! Failure! Try again. You are at F grade!\";
} else {
cout << \"Please Enter a Valid Number : \"; // message to enter the valid data
continue;
}
cout << endl;
break;
} else {
cout << \"Invalid number, Please Enter Again : \"; // message and get the data again
}
}
}
return 0;
}
OUTPUT :
Please Enter the three Grades you Recieved
Enter the Grade 1 : h
Invalid number, please try again: john
Invalid number, please try again: 57
OOPS..! Failure! Try again. You are at F grade!
Enter the Grade 2 : 87
Good! You are above average with a B grade!
Enter the Grade 3 : 98
Congratulations! You Secured an A grade!
Hope this is helpful.

