- This is bad
if (destPlanetName == MERCURY_NAME) {
endWeight = startWeight * MERCURY_FACTOR;
cout << fixed << setprecision(5) ;
cout << "New Weight: " << endWeight << endl;
} else if (destPlanetName == VENUS_NAME) {
endWeight = startWeight * VENUS_FACTOR;
cout << fixed << setprecision(5) ;
cout << "New Weight: " << endWeight << endl;
} else if (destPlanetName == EARTH_NAME) {
endWeight = startWeight * EARTH_FACTOR;
cout << fixed << setprecision(5) ;
cout << "New Weight: " << endWeight << endl;
...
} else {
cout << "New Weight: " << "unknown" << endl;
}
- Move the cout statements to before and after the block.
-
cout << fixed << setprecision(5) ;
if (destPlanetName == MERCURY_NAME) {
endWeight = startWeight * MERCURY_FACTOR;
} else if (destPlanetName == VENUS_NAME) {
endWeight = startWeight * VENUS_FACTOR;
} else if (destPlanetName == EARTH_NAME) {
endWeight = startWeight * EARTH_FACTOR;
...
} else {
error = true;
}
cout << "New Weight: "
if (not error) {
cout << endWeight << endl;
} else {
cout << "unknown" << endl;
}
- I would move the computation out as well.
-
float conversionFactor{0}
cout << fixed << setprecision(5) ;
if (destPlanetName == MERCURY_NAME) {
conversionFactor = MERCURY_FACTOR;
} else if (destPlanetName == VENUS_NAME) {
conversionFactor = VENUS_FACTOR;
} else if (destPlanetName == EARTH_NAME) {
conversionFactor = EARTH_FACTOR;
...
} else {
error = true;
}
endWeight = startWeight * conversionFactor;
cout << "New Weight: "
if (not error) {
cout << endWeight << endl;
} else {
cout << "unknown" << endl;
}