Is there a way to restrict input to integer/double in this format in c++ -
i trying make simple calculator in c++. in first version, had menu based system , asked input 1 after other easy validate integer. however, realised can user enter question in format enter on real calculator.
double first; char choice; double second; cin >> first >> choice >> second;
however stuck on how can check because of solution have looked @ follow format, not know how can implement because cin takes in 3 pieces of data.
int x; cin >> x; if (!cin) { // not number }
you cannot restrict user types @ terminal. best thing can detect input not appropriate, , ask input again.
while ( !(cin >> first) ) { cout << "please enter floating point number: " cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); }
do same thing other numbers.
Comments
Post a Comment