Could someone show me how to provide validation of input in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Could someone show me how to provide validation of input in C++?

Like if you write a program to accept a 10 digit account number as input, if the user enters 9 or lesser digits, or an alphabet, it gives an error...

16th Aug 2017, 11:35 AM
Thomas Sankara
Thomas Sankara - avatar
3 Answers
+ 4
Following block is Not exactly what you want but give you the idea. int num = 0; int n = 1; do{ std::cout << "Enter number #" << n << " : "; std::cin >> num; if (std::cin.fail()) { std::cin.clear(); std::cin.ignore(200, '\n'); std::cout << "Invalid input!\n"; continue; } ++n; } while (n<=10);
16th Aug 2017, 12:01 PM
Babak
Babak - avatar
+ 1
Thank you all...
17th Aug 2017, 11:21 AM
Thomas Sankara
Thomas Sankara - avatar
0
in c# I would count the variable and equate to 10, for alphabets I would use a function isinteger to ensure he/she entered numbers. example string numb = input numb.Count ==10; just look at a function on isinteger if it exists and how to use it.
16th Aug 2017, 11:58 AM
Tuchy
Tuchy - avatar