Skip to main content
GameDev.net gamedev.net
🔒 Locked

weird loop/casting problems

Started by mrtorrent Mar 13, 2003 at 2:07 PM 3 replies 250+ views
Original Post
mrtorrent
mrtorrent
Okay, a friend put together the following little program and asked me why when the user inputs an invalid number, it loops and waits for input correctly, while when the user inputs a letter, it loops infinitely, without waiting for input. I tried casting student_grade to int, but it doesn''t make a difference. Any ideas? #include <iostream> void main () { int student_grade; //Students number grade while (true) { std::cout << "Enter in your number grade: "; std::cin >> student_grade; if (student_grade >= 0 && student_grade <= 60) //Between 0 and 60 { if ( std::cout << "Your letter grade is a F.\n"; break; } else if (student_grade > 60 && student_grade <= 70) //Between 61 and 70 { std::cout << "Your letter grade is a D.\n"; break; } else if (student_grade > 70 && student_grade <= 80) //Between 71 and 80 { std::cout << "Your letter grade is a C.\n"; break; } else if (student_grade > 80 && student_grade <= 90) //Between 81 and 90 { std::cout << "Your letter grade is a B.\n"; break; } else if (student_grade > 90 && student_grade <= 100) //Between 91 and 100 { std::cout << "Your letter grade is an A.\n"; break; } else //If student enters in a negative number or a number higher than 100 std::cout << "You have an entered an incorrect grade, please enter another one in.\n"; } }
cgoat
cgoat
Told you this was a common problem:

Check Here

Basically, use cin.fail() to check for failure, and if so, then you need to clear the input buffer or else it will just fail again next time.

[edited by - cgoat on March 13, 2003 4:02:43 PM]
mrtorrent
mrtorrent
Okay, thank you, I think I understand now. Here''s my understanding of it, is it correct?

When you enter a letter an input error is generated, because you''re trying to enter a char into an int. So, what happens is that it errors out and stops reading from the input buffer into the variable. As a result, there is still data left in the input buffer, and the buffer doesn''t get cleared. So, you''ve got to check if there was an error, with cin.fail(), then tell it to clear the error (apparently once there''s an error, it goes into an error state, and you have to tell it to move back into a normal state to try again or something? - I get this from page two here) with cin.clear(), then clear the rest of the buffer with cin.ignore().
cgoat
cgoat
Yep, one thing I usually do is just read a string to clear out whatever garbage is messing it up. I don't use cin much so I wasn't aware of cin.clear(). I tend to fall back on C functions (scanf/fgets) for input.

EDIT: By the way, that's a very descriptive document you linked to, it explains it well.

[edited by - cgoat on March 13, 2003 6:02:24 PM]
mrtorrent
mrtorrent
Okay. How would you enter a specific string into the input buffer?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.