reducing an if statement

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  #include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

int main() {
 
double milesPerYear = 0;   
double pricePerGallon = 0;
const double 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;
		}

	}
else if (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;
}
If statements can use logical operators AND (&&), OR (||) and NOT (!) to check multiple conditions at one time. http://www.cplusplus.com/doc/tutorial/operators/#logical

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
}
else if (buyingCrition == "Gas" && totalGalonsHybrid > totalGallonsNonHybrid)
{
    // do other stuff
}
else if ( . . .)  // 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.
Last edited on
yes ok! Thanks!
Topic archived. No new replies allowed.