| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Detecting Char Input
Hi,
I am currently writing a prog to show the use of exception handlers. Suppose you input a number such as: Code:
int a; cout << "Enter size of array: "; cin >> a; if (a < 0) throw "Size Must Be Positive!"; How can I detect if a entered was a character? I tried converting to ASCII but then suppose the user enters 400 as their input, then my ASCII code wont work. How can I detect if a character was entered so I can throw an error message before the prog crashes? Hope I explained myself. Thanks! |
|
#2
|
|||
|
|||
|
maybe you could use something like:
if(typeof(i)!='int') ... don't know if it works though |
|
#3
|
||||
|
||||
|
make "a" a character array, then use the atoi() function to convert whatever is entered into an integer value. That way if they enter a number, you get the actual number's value, but if you enter any characters, they are simply ignored....
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Detecting Char Input |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|