Need Help printing out results in order

closed account (16MjE3v7)
How do I meet this requirement: If the users criteria is "Gas", then print the output for the car with the lower gas consumption first. If the criteria is "Total", then print the output for the car with the lowest total cost first.
I have tried a few things, but nothing seems to be working ha. Any help would be awesome! Code below. I know the int=car_type needs to be fixed somehow
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
#include <iostream>
#include <string>
using namespace std;
 
 
int main() {
 
 
int miles_per_year;
double gallon_gas;
double hybrid_cost;
int hybrid_mpg;
double hybrid_resale;
double cost_nonhybrid;
int non_hybrid_mpg;
double nonhybrid_resale;
double fuel_five_years; 
double total_gallons;
double total_cost;
double depreciation;
double fuel_five_years_hybrid; 
double total_gallons_hybrid;
double total_cost_hybrid;
double depreciation_hybrid;
int car_type;
 
    cout <<"Please answer the following questions.\n" << endl;
     
do
{
    cout <<"The estimated miles driven per year?" << endl;
    cin >> miles_per_year;
if(miles_per_year < 0)
    cout <<"Please enter a positive value." << endl;
} while(miles_per_year < 0);
 
 
do
{
    cout <<"The estimated price of a gallon of gas?" << endl;
    cin >> gallon_gas;
if(gallon_gas < 0)
    cout <<"Please enter a positive value." << endl;
} while(gallon_gas < 0);
 
 
do
{
    cout <<"The cost of a hybrid car?" << endl;
    cin >> hybrid_cost;
if(hybrid_cost < 0)
    cout <<"Please enter a positive value." << endl;
} while(hybrid_cost < 0);
 
 
do
{
    cout <<"The efficiency of the hybrid car in miles per gallon?" << endl;
    cin >> hybrid_mpg;
if(hybrid_mpg < 0)
    cout <<"Please enter a positive value." << endl;
} while(hybrid_mpg < 0);
 
 
do
{
    cout <<"The estimated resale value for a hybrid after 5 years?" << endl;
    cin >> hybrid_resale;
if(hybrid_resale < 0)
    cout <<"Please enter a positive value." << endl;
} while(hybrid_resale < 0);
 
 
do
{
    cout <<"The cost of a non-hybrid car?" << endl;
    cin >> cost_nonhybrid;
if(cost_nonhybrid < 0)
    cout <<"Please enter a positive value." << endl;
} while(cost_nonhybrid < 0);
 
 
do
{
    cout <<"The efficiency of the non-hybrid car in miles per gallon?" << endl;
    cin >> non_hybrid_mpg;
if(non_hybrid_mpg < 0)
    cout <<"Please enter a positive value." << endl;
} while(non_hybrid_mpg < 0);
 
 
do
{
    cout <<"The estimated resale value for a non-hybrid after 5 years?" << endl;
    cin >> nonhybrid_resale;
if(nonhybrid_resale < 0)
    cout <<"Please enter a positive value." << endl;
} while(nonhybrid_resale < 0);
     
 
 string buying_crit;
    cout <<"Would you like results sorted by lowest gas consumption(\"gas\")\n or lowest total cost(\"total\")?" << endl;
    cout << "Enter 'gas' or 'total'\n"; 
    cin >> buying_crit;




//calculations for hybrid
total_gallons_hybrid = miles_per_year / hybrid_mpg;
fuel_five_years_hybrid = miles_per_year / hybrid_mpg * gallon_gas;
depreciation_hybrid = hybrid_cost - hybrid_resale;
total_cost_hybrid = fuel_five_years_hybrid + depreciation_hybrid;



cout <<"Your car is a "<< car_type <<endl;
cout <<"Total gallons of fuel used for 5 years: $ "<< total_gallons_hybrid << endl;
cout <<"Total cost of owning the car for 5 years: $ " << total_cost_hybrid << endl;

//calculations for NON-hybrid
total_gallons = miles_per_year / non_hybrid_mpg;
fuel_five_years = miles_per_year / non_hybrid_mpg * gallon_gas;
depreciation = cost_nonhybrid - nonhybrid_resale;
total_cost = fuel_five_years + depreciation;


cout <<"Your car is a "<< car_type <<endl;
cout <<"Total gallons of fuel used for 5 years: $ "<< total_gallons << endl;
cout <<"Total cost of owning the car for 5 years: $ " << total_cost << endl;

//A label indicating whether the car is Hybrid or Non-Hybrid 
// total gallons of fuel used for 5 years
//the total cost of owning the car for 5 years (fuel cost + depreciation in car value) 
//if the users criteria is "Gas", then print the output for the car with the lower gas consumption first.
// if criteria is "Total", then print the output for the car with the lowest total cost first.


system("pause");
return 0;
 
 
}
The cars really should be managed as objects, have you studied classes and or structs yet?
closed account (16MjE3v7)
I have not :(
Well what youre prolly going to have to do is set up a nested for loop. As your figure out which vehicle costs the least put its information into a vector (or array). Then when youre done with that just print out the vector (or array).

A class or struct like computergeek01 said would make this issue so much simplier, but alas you can do what I just said and itll work also.

If you need any other help let me know.
Topic archived. No new replies allowed.