1

I have the following code. When something like jackpot is inputted, it prints out the cout 8 times, once for each character. Why is it doing this? Information is a structure and number is an integer.

do { cout <<"Please input a valid number."<< endl; cin>>information.number; if (!cin) { cin.clear(); cin.ignore(); } } while(information.number> 12 || information.number< 1); 
1

1 Answer 1

1

You can specify a maximum ignore length length and an ignore delimiter:

do { cout <<"Please input a valid number."<< endl; cin>>information.number; if (!cin) { cin.clear(); cin.ignore(1024, '\n'); // ignore up to 1024 chars. until '\n' } } while(information.number> 12 || information.number< 1); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.