Hello, I have to write a C++ program that allows the user to enter a test score
ID: 3795314 • Letter: H
Question
Hello,
I have to write a C++ program that allows the user to enter a test score in percentage for a student. Please use the while loop to validate the user input. The program should define a function to assign a letter grade to the score based on the following scheme:
grade is A if score >=90
grade is B if score >=80, and score <90
grade is C if score >=70, and score <80
grade is D if score >=60, and score <70
grade is F if score <60
The main program should call the function and display the score in letter grade.
This is what I have so far but I do not know if it is right. I still need to work on the while loop but I am not sure how to do it:
#include <iostream>
using namespace std;
int main()
{
int percent;
cout << "Enter the percentage: ";
cin >> percent;
if(percent >=90)
cout << "Your grade is an A";
else if (percent >= 80 && percent < 90)
cout << "Your grade is a B";
else if (percent >=70 && percent < 80)
cout << "Your grade is a C";
else if (percent >= 60 && percent < 70)
cout <<"Your grade is a D";
else if (percent < 60)
cout << "Your grade is an F";
}
return 0;
}
Thank you for your time and help.
Explanation / Answer
Program:
#include <iostream>
using namespace std;
int main()
{
int percent;
cout << "Enter the percentage: ";
cin >> percent;
while (percent != 0) // Here this loop is return 0 until percentage is equals to zero
{
if(percent >=90)
cout << "Your grade is an A";
else if (percent >= 80 && percent < 90)
cout << "Your grade is a B";
else if (percent >=70 && percent < 80)
cout << "Your grade is a C";
else if (percent >= 60 && percent < 70)
cout <<"Your grade is a D";
else if (percent < 60)
cout << "Your grade is an F";
return 0;
}
}
Output:
Enter the percentage: 90
Your grade is an A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.