So i had to write a program to determine which car is better depending on user preference and i didnt realize until the end that The TA will check part 4 to assure that you are using just one if-statement with and's and or's. (5 point deduction if missing). Any suggestions?
#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;
cout << "Please enter the following: " << endl << endl;
cout << "The estimated miles driven per year: " << endl;
cin >> milesPerYear;
if (milesPerYear <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter: ";//error message
cout << "The estimated miles driven per year: " << endl;
cin >> milesPerYear;
}
cout << "The estimated price of a gallon of gas during the 5 years of ownership: " << endl;
cin >> pricePerGallon;
if (pricePerGallon <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter: ";
cout << "The estimated price of a gallon of gas during the 5 years of ownership: " << endl;
cin >> pricePerGallon;
}
cout << "The initial cost of a hybrid car: " << endl;
cin >> initialCostOfHybrid;
if (initialCostOfHybrid <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter: ";
cout << "The initial cost of a hybrid car: " << endl;
cin >> initialCostOfHybrid;
}
cout << "The efficiency of the hybrid car in miles per gallon: " << endl;
cin >> efficiencyHybridMpg;
if (efficiencyHybridMpg <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter: ";
cout << "The efficiency of the hybrid car in miles per gallon: " << endl;
cin >> efficiencyHybridMpg;
}
cout << "The estimated resale value (a dollar amount) for a hybrid after 5 years: "<< endl;
cin >> resaleValueHybrid;
if (resaleValueHybrid <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter: ";
cout << "The estimated resale value (a dollar amount) for a hybrid after 5 years: "<< endl;
cin >> resaleValueHybrid;
}
cout << "The initial cost of a non-hybrid car: " << endl;
cin >> initialCostOfNonHybrid;
if (initialCostOfNonHybrid <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter: ";
cout << "The initial cost of a non-hybrid car: " << endl;
cin >> initialCostOfNonHybrid;
}
cout << "The efficiency of the non-hybrid car in miles per gallon: " << endl;
cin >> efficiencyNonHybridMpg;
if (efficiencyNonHybridMpg <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter: ";
cout << "The efficiency of the non-hybrid car in miles per gallon: " << endl;
cin >> efficiencyNonHybridMpg;
}
cout << "The estimated resale value (a dollar amount) for a non-hybrid after 5 years: " << endl;
cin >> resaleValueNonHybrid;
if (resaleValueNonHybrid <= 0) {
cout << "I'm sorry, you must enter a value bigger than zero. Please try again. Please enter: ";
cout << "The estimated resale value (a dollar amount) for a non-hybrid after 5 years: " << endl;
cin >> resaleValueNonHybrid;
}
cout << "The user's buying criterion, either minimized gas consumption or total cost (enter Gas or Total): " <<endl;
cin >> buyingCriterion;
cout << endl;
//variables for calculations
double totalGallonsHybrid = 0;
double totalGallonsNonHybrid = 0;
double fuelCostHybrid = 0;
double depreciationOfHybrid = 0;
double totalCostOfHybrid = 0;
double fuelCostNonHybrid = 0;
double depreciationOfNonHybrid = 0;
double totalCostOfNonHybrid = 0;
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") //part 4
{
if (totalGallonsHybrid < totalGallonsNonHybrid) {
cout << "For the Hybrid Car: " << endl << endl;
cout << "The total gallons of fuel consumed over 5 years: "<< fixed << setprecision(2) << totalGallonsHybrid << endl;
cout << "The total cost of owning the car for 5 years: "<< fixed << setprecision(2) << totalCostOfHybrid << endl;
cout << endl;
cout << "For the NON-Hybrid Car: " << endl << endl;
cout << "The total gallons of fuel consumed over 5 years: "<< fixed << setprecision(2) << totalGallonsNonHybrid << endl;
cout << "The total cost of owning the car for 5 years: "<< fixed << setprecision(2) << totalCostOfNonHybrid << endl;
cout << "** The HYBRID Car is better than the NON-HYBRID Car when \"Gas\" is the user's primary objective." << endl;
}
else {
cout << "For the Hybrid Car: " << endl << endl;
cout << "The total gallons of fuel consumed over 5 years: " << fixed << setprecision(2) << totalGallonsHybrid << endl;
cout << "The total cost of owning the car for 5 years: " << fixed << setprecision(2) << totalCostOfHybrid << endl;
cout << "For the NON-Hybrid Car: " << endl << endl;
cout << "The total gallons of fuel consumed over 5 years: " << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
cout << "The total cost of owning the car for 5 years: " << fixed << setprecision(2) << totalCostOfNonHybrid << endl;
cout << "** The NON-HYBRID Car is better than the HYBRID Car when \"Gas\" is the user's primary objective."<< endl;
cout << endl;
}
}
elseif (buyingCriterion == "Total") {
if (totalCostOfHybrid < totalCostOfNonHybrid) {
cout << "For the Hybrid Car: " << endl << endl;
cout << "The total gallons of fuel consumed over 5 years: " << fixed << setprecision(2) << totalGallonsHybrid << endl;
cout << "The total cost of owning the car for 5 years: " << fixed << setprecision(2) << totalCostOfHybrid << endl;
cout << endl;
cout << "For the NON-Hybrid Car: " << endl << endl;
cout << "The total gallons of fuel consumed over 5 years: " << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
cout << "The total cost of owning the car for 5 years: " << fixed << setprecision(2) << totalCostOfNonHybrid << endl;
cout << "** The HYBRID Car is better than the NON-HYBRID Car when \"Total\" is the user's primary objective."<< endl;
}
else {
cout << "For the Hybrid Car: " << endl << endl;
cout << "The total gallons of fuel consumed over 5 years: " << fixed << setprecision(2) << totalGallonsHybrid << endl;
cout << "The total cost of owning the car for 5 years: " << fixed << setprecision(2) << totalCostOfHybrid << endl;
cout << "For the NON-Hybrid Car: " << endl << endl;
cout << "The total gallons of fuel consumed over 5 years: " << fixed << setprecision(2) << totalGallonsNonHybrid << endl;
cout << "The total cost of owning the car for 5 years: " << fixed << setprecision(2) << totalCostOfNonHybrid << endl;
cout << "** The NON-HYBRID Car is better than the HYBRID Car when \"Total\" is the user's primary objective."<< endl;
cout << endl;
}
}
return 0;
}
In your case, the if-else block can combine two checks into a single statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if (buyingCrition == "Gas" && totalGalonsHybrid < totalGallonsNonHybrid)
{
// do stuff
}
elseif (buyingCrition == "Gas" && totalGalonsHybrid > totalGallonsNonHybrid)
{
// do other stuff
}
elseif ( . . .) // add more conditional evaluation here
{
// more stuff
}
else
{
// the last stuff
}
Keep in mind that AND will be true only if both conditions are true, while OR is true as long as one of the conditions is true. It's possible to chain more logical operators together if needed, but doing so can make the code harder to follow logically.