Group
Advanced Conditional Structures in C++
Objective
1. Prompt the user to enter a numeric grade between 0 and 100.
2. Determine the letter grade using if-else statements based on the numeric value.
3. Display the corresponding letter grade to the user.
4. Ensure proper input validation is in place.
Write a program that simulates a course grading system using letter grades.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the input/output stream library
using namespace std;
int main() {
int grade; // Variable to store the student's numeric grade
// Prompt the user to enter their grade
cout << "Enter your numeric grade (0-100): ";
cin >> grade;
// Check if the input is within a valid range
if (grade < 0 || grade > 100) {
cout << "Invalid grade. Please enter a number between 0 and 100." << endl;
} else {
// Determine the letter grade based on numeric grade
if (grade >= 90) {
cout << "Your letter grade is: A" << endl;
} else if (grade >= 80) {
cout << "Your letter grade is: B" << endl;
} else if (grade >= 70) {
cout << "Your letter grade is: C" << endl;
} else if (grade >= 60) {
cout << "Your letter grade is: D" << endl;
} else {
cout << "Your letter grade is: F" << endl;
}
}
return 0; // End of program
}
Output
Enter your numeric grade (0-100): 87
Your letter grade is: B