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
I would recommend using this option:
#include <conio.h>
.getch()
in a character variable (Will not be displayed on screen).'\r'
('\r\n'
normally), break out of the loop.Unfortunately you cannot avoid it... You can accept a string as input and parse the string (maybe with regex) for correctness.
You can retrieve your data as a std::string
then use one of the standard string conversion function to see if the content matches your expectations.
Check out the sscanf function.