Public Group

#### Archived

This topic is now archived and is closed to further replies.

# i dont want the loop to run forever is someone enters a char instead of int

This topic is 5531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how do i fix this so that if the user enters a letter, it wont go nuts? int aNumber; do { cout << "Enter a number between 1 and 3: "; cin >> aNumber; } while (aNumber < 1 || aNumber > 3); if they enter a letter on accidnet, it looks like this: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: Enter a number between 1 and 3: etc. thanks! [edited by - hawflakes on May 3, 2003 3:38:35 PM]

##### Share on other sites
int aNumber;do{    std::cout << "Enter a number between 1 and 3: ";    while (!(std::cin >> aNumber))    {        std::cin.clear();        while (std::cin.get() != '\n')            continue;        std::cout << "Enter a number between 1 and 3: ";    }}while (aNumber < 1 || aNumber > 3);

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 3, 2003 3:53:00 PM]

thanks.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×