Infinite loop in C ++

I learn C ++ and write small programs as I go. The following is one such program:

// This program is intended to take any integer and convert to the // corresponding signed char. #include <iostream> int main() { signed char sch = 0; int n = 0; while(true){ std::cin >> n; sch = n; std::cout << n << " --> " << sch << std::endl; } } 

When I run this program and save the inputs with sufficiently small absolute values, it behaves as expected. But when I enter the larger inputs, for example, 1,000,000,000, the program repeatedly splashes out the same output. Some input combinations cause erratic behavior. For example:

 #: ./int2ch 10 10 --> 10000000000 10 --> 10 --> 10 --> 10 --> 

The program spits out "10 β†’" until it is killed. (With this particular sequence of inputs, the program output changes speed randomly.) I also noticed that the output of large values ​​is determined by the previous legal input, as well as the value of the current illegal input.

What's happening? (I don't care about fixing the program, it's simple. I want to understand that.)

+6
c ++ stream biginteger infinite-loop largenumber
source share
4 answers

Basically, your cin thread is in a cin state and thus returns immediately when you try to read it. Rewrite your example as follows:

 #include <iostream> int main() { signed char sch = 0; int n = 0; while(std::cin >> n){ sch = n; std::cout << n << " --> " << sch << std::endl; } } 

cin >> n will return a link to cin , which you can check for "quality factor" in a conditional expression. So basically " while(std::cin >> n) " basically says "while I could still read standard input, do the following"

EDIT: the reason it re-displays the last good entered value is because it is the last value successfully read in n, reading failures will not change the value of n

EDIT: as noted in the comment, you can clear the error state and try again, something like this will probably work and just ignore the bad numbers:

 #include <iostream> #include <climits> int main() { signed char sch = 0; int n = 0; while(true) { if(std::cin >> n) { sch = n; std::cout << n << " --> " << sch << std::endl; } else { std::cin.clear(); // clear error state std::cin.ignore(INT_MAX, '\n'); // ignore this line we couldn't read it } } } 
+13
source share

Yes, Evan Teran most of all noticed. One thing I want to add (since I cannot comment on his comment :)) is that you must place the call to istream :: clear before calling istream :: ignore. The reason is that istream :: ignore also refuses to do anything if the stream is still in a failed state.

+5
source share

Given that you are on a 32-bit machine, 1,000,000,000 is too much a number that an int needs to be represented. Also converting int to char will give you only 0..255 or -128..127 depending on the compiler.

+3
source share

One of the problems is that a char has a size of one byte and, therefore, can only contain a number from -127 to 128. On the other hand, int is usually 4 bytes, and can take much larger values. The second problem is that you enter too much value even for int .

0
source share

All Articles