Beginner Question - Exiting while loop, input type double as condition, C++ -


I have just started learning C ++ and when there is a situation for the loop, then with a loop a little The problem is getting an input, type double, with the user, I understand that if the user does not enter the corresponding value with double type the loop is automatically broken. The point is that my console application exits when entering anything other than double.

The current practice I am working on is to use the loop of that time and if many measurements are recorded in the statements and the total production user enters a value And then measurement system (25 meters at 25 meters). The highest and lower values ​​recorded in this also have to be recorded and output.

  Double value = 0; Double total = 0; Double high = 0; Double less = 0; String unit = ""; Where (cin> gtc: gt; value & gt; & gt; unit & amp; unit! = "Convert") {if (unit == "in") {total = total + (value * 2.54); // in = inches converted cm if (value is * 2.54 & gt; high) high = value; If (value is * 2.54 & lt; less) less = value; } And if (unit == "m") {total = total + (value * 100); // m = meters converted to cm (value * 100 & gt; high) high = value; If (value is * 100 & lt; less) less = value; } And if (unit == "FT") {total = total + (value * 30.48); // changed to ft = ft, ft (value * 30.48 & gt; high) high = value; If (value is * 30.48 & lt; less) less = value; } And if (unit == "cm") {total = total + value; // cm = centered if (value is * 2.54> high) high = value; If (value is * 2.54 & lt; less) less = value; } And cout & lt; & Lt; "Unable to calculate unit type -" & lt; & Lt; Unit & lt; & Lt; Endl; Value = 0; } Cout & lt; & Lt; "Center-meter total length:" & lt; & Lt; Total & lt; & Lt; Andal & lt; & Lt; "Total length of the meter:" & lt; & Lt; Total / 100 & lt; & Lt; Andal & lt; & Lt; "Total length of the leg:" & lt; & Lt; Total / 30.48 & lt; & Lt; Andal & lt; & Lt; "Inches total length:" & lt; & Lt; Total / 2.54 & lt; & Lt; Andal & lt; & Lt; "\ N Highest Value:" & lt; & Lt; Higher & lt; & Lt; ", Minimum value:" & lt; & Lt; Less & lt; & Lt; Endl; Keep_window_open (); Return 0;  

keep_window_open () is a part of the custom header file that came with my C ++ book, it does all to ask a user to enter a character to leave.

I know that the current state of the program is a bit bloated, but I wanted to work it out for everyone correctly. I had to use a work around, if the user enters as a unit in 'Convert' then the loop is killed so I could actually see that it was working correctly. Completes the program / completes its work when a price is entered in addition to a double but the result is flashed on the console window and then it immediately exits.

Any help is appreciated.

Your problem is that when you say:

 < Code> cin & gt; & Gt; Value  

Enter more than double, the stream gets damaged, because value is double than expected, your keep_window_open () function also fails, because the stream is still poor .

There are two ways in this phase:

  • Run your program from the current command line Quick window - The program will be open after it is over. This is the best general purpose solution.

  • Clear the stream before using your "pause" code. Unfortunately, the interval you write will still be in buffer, replace your "stop" code like this:

_

  String line; If (! Cin) {cin.clear (); Gateline (pin, line); Gateline (pin, line);  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -