### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188`` ``````// While loop that prints smallest and largest numbers so far with units included // Bugs --- smallest always starts off as 0. //Doesn't work. #include #include #include #include #include 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.