General rule of thumb: If you find yourself copy/pasting blocks of code... you're probably doing it wrong. (This of course is not always the case, but it is a decent guideline for beginners)
1) Don't create redundant 'reverse' conditionals.
If you have an 'if' statement checking one thing, you do not need your future 'else' statements to check the opposite. For example...
1 2 3
|
if ((mpg > 34.75) && (cpm >= .0975)) // line 35
...
else if((mpg > 34.75) && (cpm < .0975)) // line 43
|
There is no need to check if cpm is < 0.0975 on line 43 because the 'else' already ensures that is true.
2) Don't create duplicate conditionals unless necessary.
Again referring to line 35 vs 43. Both are checking if mpg > 34.75. No need to do that twice. A better (but still bad) way is to put them in a single block:
1 2 3 4 5 6 7
|
if(mpg > 34.75)
{
if(cpm >= 0.0975)
....
else // else ensures cpm < 0.0975 .. no need to have an 'if' here
....
}
|
3) Don't have every conditional exit
You copy/pasted code into all your if blocks. That's bad. You're also returning in each if block which is also bad. returning exits the function (since the function is main, it means it closes the program). There is no reason to do that here.
If you let all your if blocks "fall through" without returning, then the program will continue after them as normal. You can then have code that is run regardless of the previous 'if's. It's hard for me to explain... but here's an example:
1 2 3 4 5 6 7 8 9 10
|
if( foo > 1 )
{
cout << "this will print only if foo is > 1\n";
}
else
{
cout << "this will print only if foo is <= 1\n";
}
cout << "this will print regardless of what foo is";
|
The point is, each if block does not need to cin.get and return 0... because that is already done AFTER the else/if chain. So just let execution fall through to that point.
4) Don't try to print unrelated things at all once.
There is no reason for the cpm and mpg comparisons to be joined like they are. It would make more sense to do those comparisons each in their own if/else chain and print their message seperately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// have one else/if chain for mpg
if( mpg < foo )
cout << "bad mpg"
else if(mpg < bar)
cout << "medium mpg"
else
cout << "good mpg"
// then another for cpm
if( cpm < foo )
cout << "low cpm";
else
cout << "high cpm";
// then your quitting code
cin.get();
return 0;
|