C++ Program: Course Grading System
In this C++ exercise, you will create a program that simulates a letter-based grading system. The program takes a student's numeric grade (0-100) as input and converts it into a corresponding letter grade (A, B, C, D, or F) based on standard academic thresholds.
This is an essential task to master control flow using if-else if-else constructs and to practice input validation to ensure data integrity.
Core Concepts
Multi-way Selection
The else if ladder allows the program to evaluate multiple ranges sequentially until a match is found.
Input Validation
Using logical operators like || (OR) helps filter out numbers outside the valid 0-100 range immediately.
Pro Tip: Order Matters!
When using else if for ranges, always start from the highest (90) to the lowest (60). If you start with >= 60, a grade of 95 would incorrectly trigger the 'D' block.
Objectives
- 1. Prompt for a numeric grade between 0 and 100.
- 2. Validate the input using a range check.
- 3. Map numeric ranges to letters: A(90+), B(80+), C(70+), D(60+).
- 4. Assign F for any valid grade below 60.
Example C++ Code
#include <iostream>
using namespace std;
int main() {
int grade;
// Prompt the user for input
cout << "Enter your numeric grade (0-100): ";
cin >> grade;
// 1. Validation Logic
if (grade < 0 || grade > 100) {
cout << "Invalid grade. Please enter a number between 0 and 100." << endl;
} else {
// 2. Multi-range Determination
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;
}
Console Output:
Your letter grade is: B