Hi, I'm new to C++ and ran into a problem trying to validate user input for a double value. This is what I've tried to do so far (after putting together what I've found through google):
The code works correctly when string starting with char (eg. abc or xyz123) is entered, but not when a string starting with numbers (eg. 123xyz). I suppose it's because cin reads 123 into number and stops when a char is encountered. Is there a way to recognise that strings like 123xyz is an invalid input and to disregard the whole input?
Sorry. I meant to call it isdigit(). Its from cctype library, and is sometimes automatically included when compiling CPP, so you don't need to #include the cctype library. If you get an error it doesn't exist, include cctype.
Hey, thanks a lot for your help! Just one more thing: I need to store a double value from the user input, not a int, so I modified your code to convert into double. I'm not familiar with istringstream though, so could u help me take a look to see if I modified it correctly? Seems to work though :D