Clueless as to why this doesn't work. While/number storage

This is supposed to be as the first comment says but it does nothing for some reason. It's a big jumble of ifs but don't judge me for it. I'm working out of a book designed introduce some basic operations and operators in the first few lessons (while and if for now). Apparently this is doable without vectors and other things. I just need help expressing this code with while and if statements.

I got a more primitive version of this to work without the units but it still
starts at 0. If you would like to look I'd gladely post. I know this is long
the if statements are insane. Sorry. I cut litteraly half of the code for
the other units.

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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// While loop that prints smallest and largest numbers so far with units included
// Bugs --- smallest always starts off as 0.
//Doesn't work.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){char ch; cin>>ch;}
int main() 
{  
	/* 
	1m == 100cm
	1in == 2.54cm
	1ft == 12in
	*/
	double input;
	double smallest_input;
	double largest_input;
	string unit;
	string smallest_unit;
	string largest_unit;
	const double cm_per_m = 100;
	const double cm_per_in = 2.54;
	const double in_per_ft = 12;
	const double in_per_m = 39.37; 
	const double ft_per_m = 3.28; 
	const double cm_per_ft = 30.48; 
	
	cout << "Enter a number and a unit \n";
	 
	while (cin >> input >> unit)
		// cm to cm
		if (unit == "cm" && largest_unit == "cm" && input > largest_input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "cm" && smallest_unit == "cm" && input < smallest_input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		// in to in
		if (unit == "in" && largest_unit == "in" && input > largest_input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "in" && smallest_unit == "in" && input < smallest_input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		// ft to ft
		if (unit == "ft" && largest_unit == "ft" && input > largest_input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "ft" && smallest_unit == "ft" && input < smallest_input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		// m to m
		if (unit == "m" && largest_unit == "m" && input > largest_input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "m" && smallest_unit == "m" && input < smallest_input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		// cm to in and vise versa
		else if (unit == "cm" && largest_unit == "in" && (largest_input*cm_per_in) < input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "cm" && smallest_unit == "in" && (smallest_input*cm_per_in) > input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "in" && largest_unit == "cm" && (input*cm_per_in) > largest_input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "in" && smallest_unit == "cm" && (input*cm_per_in) < smallest_input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		// cm to feet and vise versa
		else if (unit == "cm" && largest_unit == "ft" && (largest_input*cm_per_ft) < input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "cm" && smallest_unit == "ft" && (smallest_input*cm_per_ft) > input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "ft" && largest_unit == "cm" && (input*cm_per_ft) > largest_input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "ft" && smallest_unit == "cm" && (input*cm_per_ft) < smallest_input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		// in to ft and vise versa
		else if (unit == "in" && largest_unit == "ft" && (largest_input*in_per_ft) < input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "in" && smallest_unit == "ft" && (smallest_input*in_per_ft) > input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "ft" && largest_unit == "in" && (input*in_per_ft) > largest_input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "ft" && smallest_unit == "in" && (input*in_per_ft) < smallest_input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		// in to m and vise versa
		else if (unit == "in" && largest_unit == "m" && (largest_input*in_per_m) < input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "in" && smallest_unit == "m" && (smallest_input*in_per_m) > input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "m" && largest_unit == "in" && (input*in_per_m) > largest_input){
			largest_input = input;
			largest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
		else if (unit == "m" && smallest_unit == "in" && (input*in_per_m) < smallest_input){
			smallest_input = input;
			smallest_unit = unit;
			cout << "Smallest so far is " << smallest_input << smallest_unit << endl;
			cout << "Largest so far is " << largest_input << largest_unit << endl;
			}
					
	return 0;
}
Could you clarify the meaning of the question please. Say the inputs are 100 cm followed by 1 m, are these supposed to be treated as equal (which they clearly are) or not?
Last edited on
Yes. I cut out a bit of the code so it could fit and not obstruct the character limit. I input something like that and it doesn't work at all. Really, nothing works. I start the code and it does nothing.
Thanks for the clarification. It just seemed that you could simplify a lot of the if/else testing if the input was converted to some standard units, such as mm.

Then the testing of the smallest and biggest value so far would be much simpler. You might need to have separate fields to store the converted value, which would be used for comparisons, and the original value+units, which you would use for display purposes.
Last edited on
Topic archived. No new replies allowed.