How does one force a C++ program only to accept numbers(example: "12#"or"7&5"or"@12"or"a12"or"a"or"A"or"#" is invalid) ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How does one force a C++ program only to accept numbers(example: "12#"or"7&5"or"@12"or"a12"or"a"or"A"or"#" is invalid) ?

C++

11th Apr 2018, 8:48 PM
Mr Doom
3 Answers
+ 2
get the string creat a function, send string to it, check it with for-loop if any character is out of ‘1’ to ‘9’ return false break, after loop return true... thats it
11th Apr 2018, 9:34 PM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 2
A quick method: std::string s; int c; while (1) { c = getchar(); if (c == 13 || c == 10) break; if (c > 47 && c < 58) s.push_back(c); } You'll still see typing letters, but the string, s, will only have numbers.
11th Apr 2018, 11:24 PM
non
0
Accept numbers from where? User input? Call to a function?
11th Apr 2018, 9:14 PM
Emma