patterncppMinor
Optimizing simple C++ grading program
Viewed 0 times
gradingprogramsimpleoptimizing
Problem
I have started working on the projects listed here. I want to practice my C++ coding skills. I would love any constructive criticism I can get!
This is for the very first problem, #1 "Grading Program"
This is for the very first problem, #1 "Grading Program"
#include
using std::cout;
using std::cin;
int main()
{
int UserNumber;
char Grade;
cout > UserNumber;
cout 89 && UserNumber 79 && UserNumber 69 && UserNumber 59 && UserNumber -1 && UserNumber < 61)
{
Grade = 'F';
}
else
{
Grade = 'Z';
}
switch (Grade)
{
case 'A':
cout << "Perfect! You got a: " << Grade;
break;
case 'B':
cout << "Could've been worse. You got a: " << Grade;
break;
case 'C':
cout << "It's okay I guess, you got a: " << Grade;
break;
case 'D':
cout << "Uggh.. You got a: " << Grade;
break;
case 'F':
cout << "Better luck next time! You got a: " << Grade;
break;
case 'Z':
cout << "Undefined value! Make sure it's a value from 0-100!";
break;
}
cout << "\n\n\n\t\t\t";
return 0;
}Solution
There's probably much that can be done, but I'll give it a quick review:
-
Are only integer grades allowed? They can be decimals in real life, and that would make for a more robust grading system.
-
Consider renaming
-
Do not "list" variables at the top of a function in C++, unless it's absolutely necessary. Here, you can declare
-
There's no need for a
-
You don't need an explicit
-
Are only integer grades allowed? They can be decimals in real life, and that would make for a more robust grading system.
-
Consider renaming
UserNumber to something like numericalGrade. The former sounds like it's for an ID number, not a grade.-
Do not "list" variables at the top of a function in C++, unless it's absolutely necessary. Here, you can declare
UserNumber between the std::cout and std::cin.-
There's no need for a
Z case here. Use a default case instead, which will be reached if no other case is reached.-
You don't need an explicit
return 0 at the end. The compiler will do this return for you at the end of main() as successful termination is always implied here.Context
StackExchange Code Review Q#67276, answer score: 5
Revisions (0)
No revisions yet.