Restrict user to input real number only in C++

前端 未结 10 1932
旧时难觅i
旧时难觅i 2021-01-15 06:40

How can I restrict the user to input real numbers only in C++ program?

Example:

double number; cin >> number;

and it won\'t accept t

10条回答
  •  逝去的感伤
    2021-01-15 07:02

    You cannot restrict what user types on the keyboard. You can accept it as std::string and use boost::lexical_cast to convert it to your expected number type and catch and process boost::bad_lexical_cast exception.

提交回复
热议问题