I have an assignment that requires me to create a program which asks for a single letter grade (A,B,C,D,F) and based on that input, print a GPA (4,3,2,1,0). I wrote the program but I don't think it's the optimal solution. Can anyone give me tips on how to improve it? Thanks
Well, if you want to get nit picky, change that int to an unsigned int. Unsigned calculation and representation is easier for a computer. Also, you probably don't really need two calls to your function
You could use a switch statement instead of the if, if, if, if, if else statements and you could use tolower() or toupper() instead of letterGrade == 'C' || letterGrade == 'c' but its all just a matter of preference
if you use tolower() or toupper() make sure to #include <cctype>
If efficiency is your goal, I suggest you use a profiler (gprof is good and comes with GCC).
Alternatively, you can use the standard ctime functions to time the execution of each function, and then try something different. Look on Google if you can't find something interesting ;)
-Avoid global variables as much as possible
-Initialise all variables. Assignment isn't the same as initialisation
-When working with integers, I tend to favour switch()
-Declare a variable/parameter constant if their value doesn't change. The compiler can optimise constants
-Avoid implicit casts. If the right-hand side of an assignment doesn't match, the compiler will implicitly cast the right-hand side operand to the type that of the left-hand operand (if the types are close). Casts can be expensive