I am learning C++ and am trying to create a relatively simple student report card generator. You input the marks for a certain topic and the application outputs a string showing said marks plus the actual grade for the topic.
Error checking mark input works in instances of:
- Entering a string of alphanumerical characters (i.e. hello, h, etc.)
- Entering a mark that is either less than 0 or greater than 100
However, when one enters an invalid decimal (i.e. 45.453ab.45a, 5.ab.c.a., etc.), then it does not catch the invalid input but continues the application as if nothing was wrong. Depending on the amount of periods used, it will automatically fill the remaining inputs before either terminating the program (if the amount of periods equal or are greater than the amount of remaining inputs) or treat whatever input is left as already raising the cin.clear() flag for the while loop.
Thank you for your time!
EXAMPLE CONSOLE BEHAVIOR 1
Student Report Card Generator
Please enter the marks for Math: 45.453ab.45a
Please enter the marks for English: Invalid number; please try again (English):
EXAMPLE CONSOLE BEHAVIOR 2
Student Report Card Generator
Please enter the marks for Math: 43.4.34.34.34.34.3
Please enter the marks for English:
Please enter the marks for Science:
Please enter the marks for Computing:
Please enter the marks for Art:
Math: 43.40% = E
English: 0.34% = F
Science: 0.34% = F
Computing: 0.34% = F
Art: 0.34% = F
Total Grade: 8.95% = F
CODE
#include <iostream>
#include <string>
#include <limits>
#include <iomanip>
std::string grade(double a)
{
std::string gradeL = (a >= 90) ? "A*"
: (a >= 80 && a < 90) ? "A"
: (a >= 70 && a < 80) ? "B"
: (a >= 60 && a < 70) ? "C"
: (a >= 50 && a < 60) ? "D"
: (a >= 40 && a < 50) ? "E"
: "F";
return gradeL;
}
int main()
{
double mathGrade, engGrade, sciGrade, compGrade, artGrade;
std::cout << "Student Report Card Generator\n";
std::cout << "\nPlease enter the marks for Math: ";
std::cin >> mathGrade;
while (std::cin.fail() || mathGrade < 0 || mathGrade > 100)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nInvalid number; please try again (Math): ";
std::cin >> mathGrade;
}
std::cout << "\nPlease enter the marks for English: ";
std::cin >> engGrade;
while (std::cin.fail() || engGrade < 0 || engGrade > 100)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nInvalid number; please try again (English): ";
std::cin >> engGrade;
}
std::cout << "\nPlease enter the marks for Science: ";
std::cin >> sciGrade;
while (std::cin.fail() || sciGrade < 0 || sciGrade > 100)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nInvalid number; please try again (Science): ";
std::cin >> sciGrade;
}
std::cout << "\nPlease enter the marks for Computing: ";
std::cin >> compGrade;
while (std::cin.fail() || compGrade < 0 || compGrade > 100)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nInvalid number; please try again (Computing): ";
std::cin >> compGrade;
}
std::cout << "\nPlease enter the marks for Art: ";
std::cin >> artGrade;
while (std::cin.fail() || artGrade < 0 || artGrade > 100)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nInvalid number; please try again (Art): ";
std::cin >> artGrade;
}
double totalGrade = (mathGrade + engGrade + sciGrade + compGrade + artGrade) / 5;
std::cout << "\nMath: " << std::setprecision(2) << std::fixed << mathGrade << "% = " << grade(mathGrade);
std::cout << "\nEnglish: " << std::setprecision(2) << std::fixed << engGrade << "% = " << grade(engGrade);
std::cout << "\nScience: " << std::setprecision(2) << std::fixed << sciGrade << "% = " << grade(sciGrade);
std::cout << "\nComputing: " << std::setprecision(2) << std::fixed << compGrade << "% = " << grade(compGrade);
std::cout << "\nArt: " << std::setprecision(2) << std::fixed << artGrade << "% = " << grade(artGrade);
std::cout << "\nTotal Grade: " << std::setprecision(2) << std::fixed << totalGrade << "% = " << grade(totalGrade);
return 0;
}