### program efficiency if/elseif

C++ noob here, my program works, but how can i make it MUCH less redundant and inefficient?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788`` `````` #include #include using namespace std; int main() { float miles, gallons, ppg, mpg, tripc, cpm; cout<< "2013 Pure Michigan Winter Vacation Fuel Efficiency Calculation"; cout<< "\n\nHow mant miles did you travel? "; cin>> miles; cout<< "How many Gallons of Gas did you use? "; cin>> gallons; cout<< "What was the price per Gallon of Gas? "; cin>> ppg; if (ppg <= 0.0) { cout<<"/nGas price Invalid"; cin.get(); cin.get(); return 0; } mpg = (miles / gallons); tripc = (gallons * ppg); cpm = (tripc / miles); cout<< "\n\nYou got "< 34.75) && (cpm >= .0975)) { cout<<"\n\nThis is a GREEN MACHINE automobile in fuel economy,"; cout<<"\nand this was a PRICEY Trip."; cin.get(); cin.get(); return 0; } else if((mpg > 34.75) && (cpm < .0975)) { cout<<"\n\nThis is a GREEN MACHINE automobile in fuel economy,"; cout<<"\nand this was a REASONABLE Trip."; cin.get(); cin.get(); return 0; } else if((mpg < 34.75) && (mpg > 18.5) && (cpm < .0975)) { cout<<"\n\nThis is a AVERAGE automobile in fuel economy,"; cout<<"\nand this was a REASONABLE Trip."; cin.get(); cin.get(); return 0; } else if((mpg < 34.75) && (mpg > 18.5) && (cpm >= .0975)) { cout<<"\n\nThis is a AVERAGE automobile in fuel economy,"; cout<<"\nand this was a PRICEY Trip."; cin.get(); cin.get(); return 0; } else if((mpg < 18.5) && (cpm >= .0975)) { cout<<"\n\nThis is a GAS GUZZLER automobile in fuel economy,"; cout<<"\nand this was a PRICEY Trip."; cin.get(); cin.get(); return 0; } else if((mpg < 18.5) && (cpm < .0975)) { cout<<"\n\nThis is a GAS GUZZLER automobile in fuel economy,"; cout<<"\nand this was a REASONABLE Trip."; cin.get(); cin.get(); return 0; } cin.get(); cin.get(); return 0; }``````
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...

 ``123`` `````` 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:
 ``1234567`` ``````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:

 ``12345678910`` ``````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.

 ``1234567891011121314151617`` ``````// 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;``````
Last edited on
You have no idea how much that helped, thank you!

If you see any other flaws/bad habits please let me know! thanks again

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include using namespace std; int main() { float miles, gallons, ppg, mpg, tripc, cpm; cout<< "2013 Pure Michigan Winter Vacation Fuel Efficiency Calculation"; cout<< "\n\nHow mant miles did you travel? "; cin>> miles; cout<< "How many Gallons of Gas did you use? "; cin>> gallons; cout<< "What was the price per Gallon of Gas? "; cin>> ppg; if (ppg <= 0.0) { cout<<"\nGas price Invalid"; cin.ignore(); } mpg = (miles / gallons); tripc = (gallons * ppg); cpm = (tripc / miles); cout<< "\n\nYou got "< 18.5) && (mpg < 34.75)) { cout<<"AVERAGE"; } else { cout<<"GREEN MACHINE"; } cout<<" auto mobile in fuel economy, \nand this was a "; // cpm if else if (cpm < .0975) { cout<<"REASONABLE"; } else { cout<<"PRICEY"; } cout<<" Trip."; cin.get(); cin.get(); return 0; }``````
Last edited on
You're still adding a redundant "reverse" conditional here:

 ``12345678`` `````` if (mpg < 18.5) // <- line 38 { cout<<"GAS GUZZLER"; } else if ((mpg > 18.5) && (mpg < 34.75)) // line 42 { cout<<"AVERAGE"; }``````

line 38 checks to see if mpg < 18.5
Which means the else on line 42 ensures that mpg >= 18.5. There is no reason to check against that number again.

But yes that is definitely a big improvement. Good job =)
Last edited on
Topic archived. No new replies allowed.