Restrict user to input real number only in C++

前端 未结 10 1936
旧时难觅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:15

    I would recommend using this option:

    • Firstly, do #include .
    • While true:
    • Store getch() in a character variable (Will not be displayed on screen).
    • Validate the character variable in the previous step.
    • If there are any errors, do nothing.
    • Else if the character variable is '\r' ('\r\n' normally), break out of the loop.
    • Else, output it on the screen.

提交回复
热议问题