string guessNo;
Hi! How do I validate the user input in C++ ? I just want the guessNo string to have 4 characters, all of which are numbers.
Does the input have to be a string? If you make it an integer then you just need to check that the value(v) 999 < v < 10000
If the numbers can range from 0000 to 9999, you can do it quite simply.
Get string.
Count number of letters. If not 4, bad. Use string function for this.
Get first letter. Check it is a character from the set '0' to '9'. if not, bad. Use isdigit fucntion for this.
Repeat for letters two to four.
hi Moschops, I don't know how to use isDigit function. Can you give me an example?
The isdigit() function is a standard C/C++ function that will tell you (true or false) if a character is a digit. Every C/C++ compiler should support this. In reality, usually these are macros that operate on ascii data.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.