How to reset std::cin when using it?

前端 未结 1 1598
轮回少年
轮回少年 2020-12-06 22:47

I have some problem with following code. I use it in Xcode (OS X).

[removed my first try of that code]

How to input reset std::cin? I try to input another va

相关标签:
1条回答
  • 2020-12-06 23:08

    You could use, when the condition, std::cin.fail() happens:

    std::cin.clear();
    std::cin.ignore();
    

    And then continue with the loop, with a continue; statement. std::cin.clear() clears the error flags, and sets new ones, and std::cin.ignore() effectively ignores them (by extracting and discarding them).

    Sources:

    1. cin.ignore()
    2. cin.clear()
    0 讨论(0)
提交回复
热议问题