简体   繁体   中英

how to use cin.ignore in a loop

the readMe.ingredients is a pointer to an array of Ingredient objects that has an overloaded >> operator. The delimiter for the >> operator is a newline character.

Suppose the list of ingredients is formatted like this:

  • 4 gal chocolate ice cream
  • 3 gal butterscotch syrup
  • 12 pint sliced bananas

for (int count = 0; count < readMe.numIngredients; count++)
    {
        in >> readMe.ingredients[count];
        // the loop continues until you finish with all of the ingredients
    }
    in.ignore(10000,'\n');

That's some example code for parsing the ingredient list above. But wouldn't you want in.ignore(10000,'\\n') inside the loop instead of outside in order for this to work? If not, then why should it be outside?

nevermind I think I got it. There's a blank line after the ingredient list

But wouldn't you still want in.ignore(10000,'\\n') in the loop in order to go to the next line? Wouldn't it just keep assigning 4 gal chocolate ice cream to each item in the array?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM