I have to turn in my code and just realized one of the requirements is to have just one single if statement. I have a lot of nested if statements and cant figure out how to make just one.
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main() {
double milesPerYear = 0;
double pricePerGallon = 0;
constdouble YEARS = 5;
double initialCostOfHybrid = 0;
double efficiencyHybridMpg = 0;
double resaleValueHybrid = 0;
double initialCostOfNonHybrid = 0;
double efficiencyNonHybridMpg = 0;
double resaleValueNonHybrid = 0;
string buyingCriterion;
double totalGallonsHybrid = 0;
double totalGallonsNonHybrid = 0;
double fuelCostHybrid = 0;
double depreciationOfHybrid = 0;
double totalCostOfHybrid = 0;
double fuelCostNonHybrid = 0;
double depreciationOfNonHybrid = 0;
double totalCostOfNonHybrid = 0;
cout << "Please enter the following:";
cout << endl;
cout << endl;
cout << "The estimated miles driven per year: ";
cout << endl;
cin >> milesPerYear;
if (milesPerYear <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
cout << endl; //error message
cout << "The estimated miles driven per year: ";
cout << endl;
cin >> milesPerYear;
}
cout << "The estimated price of a gallon of gas during the 5 years of ownership: ";
cout << endl;
cin >> pricePerGallon;
if (pricePerGallon <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
cout << endl;
cout << "The estimated price of a gallon of gas during the 5 years of ownership: ";
cout << endl;
cin >> pricePerGallon;
}
cout << "The initial cost of a hybrid car: ";
cout << endl;
cin >> initialCostOfHybrid;
if (initialCostOfHybrid <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
cout << endl;
cout << "The initial cost of a hybrid car: ";
cout << endl;
cin >> initialCostOfHybrid;
}
cout << "The efficiency of the hybrid car in miles per gallon: ";
cout << endl;
cin >> efficiencyHybridMpg;
if (efficiencyHybridMpg <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
cout << endl;
cout << "The efficiency of the hybrid car in miles per gallon: ";
cout << endl;
cin >> efficiencyHybridMpg;
}
cout << "The estimated resale value (a dollar amount) for a hybrid after 5 years: ";
cout << endl;
cin >> resaleValueHybrid;
if (resaleValueHybrid <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
cout << endl;
cout << "The estimated resale value (a dollar amount) for a hybrid after 5 years: ";
cout << endl;
cin >> resaleValueHybrid;
}
cout << "The initial cost of a non-hybrid car: ";
cout << endl;
cin >> initialCostOfNonHybrid;
if (initialCostOfNonHybrid <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
cout << endl;
cout << "The initial cost of a non-hybrid car: ";
cout << endl;
cin >> initialCostOfNonHybrid;
}
cout << "The efficiency of the non-hybrid car in miles per gallon: ";
cout << endl;
cin >> efficiencyNonHybridMpg;
if (efficiencyNonHybridMpg <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
cout << endl;
cout << "The efficiency of the non-hybrid car in miles per gallon: ";
cout << endl;
cin >> efficiencyNonHybridMpg;
}
cout << "The estimated resale value (a dollar amount) for a non-hybrid after 5 years: ";
cout << endl;
cin >> resaleValueNonHybrid;
if (resaleValueNonHybrid <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter:";
cout << endl;
cout << "The estimated resale value (a dollar amount) for a non-hybrid after 5 years: ";
cout << endl;
cin >> resaleValueNonHybrid;
}
cout << "The user's buying criterion, either minimized gas consumption";
cout << " or total cost (enter Gas or Total): ";
cout << endl;
cin >> buyingCriterion;
totalGallonsHybrid = (milesPerYear * YEARS) / efficiencyHybridMpg;
totalGallonsNonHybrid = (milesPerYear * YEARS) / efficiencyNonHybridMpg;
fuelCostHybrid = totalGallonsHybrid * pricePerGallon;
depreciationOfHybrid = initialCostOfHybrid - resaleValueHybrid;
totalCostOfHybrid = fuelCostHybrid + depreciationOfHybrid;
fuelCostNonHybrid = totalGallonsNonHybrid * pricePerGallon;
depreciationOfNonHybrid = initialCostOfNonHybrid - resaleValueNonHybrid;
totalCostOfNonHybrid = fuelCostNonHybrid + depreciationOfNonHybrid;
if (buyingCriterion == "Gas")
{
if (totalGallonsHybrid < totalGallonsNonHybrid) {
cout << "For the hybrid car:" << endl << endl;
cout << "The estimated total gallons of fuel consumed over 5 years: ";
cout << fixed << setprecision(2) << totalGallonsHybrid << endl;
cout << "The estimated total cost of owning the car for 5 years: ";
cout << fixed << setprecision(2) << totalCostOfHybrid << endl;
cout << endl;
cout << "For the non-hybrid car:" << endl << endl;
cout << "The estimated total gallons of fuel consumed over 5 years: ";
cout << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
cout << "The estimated total cost of owning the car for 5 years: ";
cout << fixed << setprecision(2) << totalCostOfNonHybrid << endl << endl;
cout << "** The hybrid car is better than the non-hybrid car when \"Gas\"";
cout << " is the user's primary objective.";
cout << endl;
}
else {
cout << "For the hybrid car:" << endl << endl;
cout << "The estimated total gallons of fuel consumed over 5 years: ";
cout << fixed << setprecision(2) << totalGallonsHybrid << endl;
cout << "The estimated total cost of owning the car for 5 years: " ;
cout << fixed << setprecision(2) << totalCostOfHybrid << endl << endl;
cout << "For the non-hybrid car:" << endl << endl;
cout << "The estimated total gallons of fuel consumed over 5 years: ";
cout << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
cout << "The estimated total cost of owning the car for 5 years: ";
cout << fixed << setprecision(2) << totalCostOfNonHybrid << endl << endl;
cout << "** The non-hybrid car is better than the hybrid car when \"Gas\"";
cout << " is the user's primary objective.";
cout << endl;
cout << endl;
}
}
elseif (buyingCriterion == "Total") {
if (totalCostOfHybrid < totalCostOfNonHybrid) {
cout << "For the hybrid car:" << endl << endl;
cout << "The estimated total gallons of fuel consumed over 5 years: ";
cout << fixed << setprecision(2) << totalGallonsHybrid << endl;
cout << "The estimated total cost of owning the car for 5 years: ";
cout << fixed << setprecision(2) << totalCostOfHybrid << endl;
cout << endl;
cout << "For the non-hybrid car:" << endl << endl;
cout << "The estimated total gallons of fuel consumed over 5 years: ";
cout << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
cout << "The estimated total cost of owning the car for 5 years: ";
cout << fixed << setprecision(2) << totalCostOfNonHybrid << endl << endl;
cout << "** The hybrid car is better than the non-hybrid car when \"Total\"";
cout << " is the user's primary objective.";
cout << endl;
}
else {
cout << "For the hybrid car:" << endl << endl;
cout << "The estimated total gallons of fuel consumed over 5 years: ";
cout << fixed << setprecision(2) << totalGallonsHybrid << endl;
cout << "The estimated total cost of owning the car for 5 years: ";
cout << fixed << setprecision(2) << totalCostOfHybrid << endl << endl;
cout << "For the non-hybrid car:" << endl << endl;
cout << "The estimated total gallons of fuel consumed over 5 years: ";
cout << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
cout << "The estimated total cost of owning the car for 5 years: ";
cout << fixed << setprecision(2) << totalCostOfNonHybrid << endl << endl;
cout << "** The non-hybrid car is better than the hybrid car when \"Total\"";
cout << " is the user's primary objective.";
cout << endl;
cout << endl;
}
}
return 0;
}
This is a terribly stupid requirement.
If teachers are going to impose arbitrary restrictions on how you write your software, you would hope they were reasonable. This can't even be considered a useful exercise.
What exactly did the teacher write? I doubt the intent is actually to forbid all but one if-statement.
If the ban is simply of nested conditionals, then realize that if (a) { if (b) x; else y; } else z;Is equivalent to
1 2 3
if (a && b) x;
if (a && !b) y;
if (!a) z;
The transformation is mechanical and can applied recursively if required.
this is a key component of computer engineering, where circuits that are smaller are preferred. I only had one class on that stuff, as I went to the softer side, but in there we learned about this great thing called a Karnaugh map which does exactly that --- it can be used to convert unrelated conditions together to reduce the logic needed to perform the action. The technique has been invaluable over the decades.
That said, in C you have tons of options... switch statements, or nest your logic so that the most likely to fail condition is first, and other tweaks.
The above Boolean logic approaches are also very, very handy.
The trouble with condensation of logic is that the final statement is usually illogical to humans. If the terms are not related, you get utter nonsense like
if (carcolor = blue and engine = v8 and seats = leather) then something else... just because those 3 things are incidentally correlated and happen to give the correct result.
Anyway, give it a try and ask again if you get stuck.
"The way most want to solve this part of the lab is by using nested if-statements (that is, an if-statement inside an if-statement), however we would like you to learn how to condense your code and practice with boolean expressions. For example,
if (isCheese){
if (isHoly){
cout << "The food is swiss cheese!" << endl;
}
}
can be written as
if (isCheese && isHoly){
cout << "The food is swiss cheese!" << endl;
}"
My question is can you do that if you have an else statement!
if (isCheese && isHoly)
{
cout << "The food is swiss cheese!" << endl;
}
else
{
cout << "The food isnt swiss cheese!" << endl;
}
I personally think youll have to start over, condense the logic, and then grab the print statements from your current version after that to put it back together.
My question is can you do that if you have an else statement!
Yes.
Did you see my previous post? Near the bottom, there's a brief example regarding how to do the transformation.
jonnin wrote:
This is a key component of computer engineering, where circuits that are smaller are preferred.
Good point -- I've got very little training when it comes to this stuff, but IIRC, K-maps help minimize the number of gates, i.e., the number of boolean operators / transistors, but not nesting. If you flatten the representation you'll end up with duplicate sub-circuits. The nesting isn't allowed in OP's case. I don't know in what circumstances the "flattened" representation will be an improvement.
An interesting note is that modern hardware is so sophisticated the circuit design is mostly performed by computer; the logic design problems have been far too large for humans for a long time.
The first thing I would do is correctly indent your program. It's easier to understand nesting with some sort of consistent visual clue.
Here is your entire program with everything except the control-statements removed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main() {
if (milesPerYear <= 0) {}
if (pricePerGallon <= 0) {}
if (initialCostOfHybrid <= 0) {}
if (efficiencyHybridMpg <= 0) {}
if (resaleValueHybrid <= 0) {}
if (initialCostOfNonHybrid <= 0) {}
if (efficiencyNonHybridMpg <= 0) {}
if (resaleValueNonHybrid <= 0) {}
if (buyingCriterion == "Gas") {
if (totalGallonsHybrid < totalGallonsNonHybrid) {}
else {}
} elseif (buyingCriterion == "Total") {
if (totalCostOfHybrid < totalCostOfNonHybrid) {}
else {}
}
}
Lines 2 through 10 are all flat. There's no nesting in those lines.
The only stuff you have to change is the nested conditionals on the lines in [11, 17]. Here are those lines. In between each set of empty braces is a label -- a, b, c, etc.; that label represents all the stuff that's in between the braces in your original program; those are the alternatives.