Original Post
🔒 Locked
i
Advertisement
After you use operator >>() on cin, check the fail bit (cin.fail()). If the fail bit is set then the extraction operation couldn''t be performed, and probably because the input wasn''t actually an integer.
I''m guessing what''s happening in your code is that when you perform extraction on the stream, it fails the operation and sets the variable you used to a negative number. When you enter your loop it continues to fail and thus you go into an infinite loop.
I''m guessing what''s happening in your code is that when you perform extraction on the stream, it fails the operation and sets the variable you used to a negative number. When you enter your loop it continues to fail and thus you go into an infinite loop.
quote:That would be the infinite loop he talked about.
Original post by aeleneski
but if at any time I put in something other than a negative or positive number the program would freeze.
Read your input into a string and use a stringstream to extract the number. If it fails then you can admonish the user to enter a number without hanging.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement