Patrick
Patrick

Reputation: 93

c++ while loop repeats

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);

Upvotes: 1

Views: 126

Answers (1)

perreal
perreal

Reputation: 98108

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);

Upvotes: 1

Related Questions