How to validate numeric input C++

后端 未结 7 1980
忘了有多久
忘了有多久 2020-11-29 11:26

I\'d like to know how to limit an input value to signed decimals using std::cin.

7条回答
  •  隐瞒了意图╮
    2020-11-29 12:25

    If the backing variable of the cin is a number, and the string provided is not a number, the return value is false, so you need a loop:

    int someVal;
    
    while(!(cin >> someVal)) {
       cin.reset();
       cout << "Invalid value, try again.";
    }
    

提交回复
热议问题