How to validate numeric input C++

后端 未结 7 1974
忘了有多久
忘了有多久 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:29

    cin's >> operator works by reading one character at a time until it hits whitespace. That will slurp the whole string -1a2.0, which is obviously not a number so the operation fails. It looks like you actually have three fields there, -1, a, and 2.0. If you separate the data by whitespace, cin will be able to read each one without problem. Just remember to read a char for the second field.

    0 讨论(0)
提交回复
热议问题