Well, I was just playing with C++ and made a trivial program:
int user_num;
while(true)
{
cin>>user_num;
cout<<(int)user_num<<'\n';
}
Weird thing is, if the user enters non-integer number, the program keeps repeating the integer number forever.
For example: if user enter 8.1, program will keep repeating 8 in an infinite loop.But if he entered integer number, it will just show the number normally and wait for next number.
Tried to change that, instead (int) made it (bool), and similar thing happenned, if user enter any non-zero non-integer number, the program keeps repeating 1 forever.But if integer number it will converted to 1 normally (or 0 if user entered 0).
I tried also to make it (bool)(int)user_num but same thing.
Got it fixed when I changed user_num type to float, but why the error happened in the first place ? just curious if anybody can explain.
Question
kInG aLeXo
Well, I was just playing with C++ and made a trivial program:
int user_num; while(true) { cin>>user_num; cout<<(int)user_num<<'\n'; }Weird thing is, if the user enters non-integer number, the program keeps repeating the integer number forever.
For example: if user enter 8.1, program will keep repeating 8 in an infinite loop.But if he entered integer number, it will just show the number normally and wait for next number.
Tried to change that, instead (int) made it (bool), and similar thing happenned, if user enter any non-zero non-integer number, the program keeps repeating 1 forever.But if integer number it will converted to 1 normally (or 0 if user entered 0).
I tried also to make it (bool)(int)user_num but same thing.
Got it fixed when I changed user_num type to float, but why the error happened in the first place ? just curious if anybody can explain.
OS: Win XP SP3
Compiler: VC++ 6
Link to comment
Share on other sites
16 answers to this question
Recommended Posts