i want to write some code that can input only number or only character ?
plz help me ???

Recommended Answers

All 6 Replies

Use if-else structure to check your conditional statement and apply loop for prompting the user to enter valid input.

Check out the header <cctype>, as it has isalpha(), isdigit(), etc.

why not use cin object as a test condition? That way it can get type mismatch and it won't write data to that type and we can easily get rid of bad input and ask user to reenter with proper data. Would this be a bad approach?

@Crutoy : Please show an example code. :P
I'm not getting exactly what you're telling him to do. So please give an example...
Thanks.

i never know that we can use cin as condition but like @Crutory i don't understand also

why not use cin object as a test condition? That way it can get type mismatch and it won't write data to that type and we can easily get rid of bad input and ask user to reenter with proper data. Would this be a bad approach?

That will work, too. OP, you'll need to use either the .fail() or the .good() method of the input stream for that (see http://www.cplusplus.com/reference/iostream/ios/good/ etc)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.