| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Input validation
i have written many programs in c++ (some for school, and some just for fun). The problem that i have always had is that if i input a value that i did not account for my screen will get flooded with text. im looking for a way to check input for the wrong type of data.
ex: look at this problem void main() { int Choice; cin >> Choice; switch(Choice) { case 1: // code here for if they pick 1 break; case 2: // code here for if they pick 2 break; default: // code here for if they dont pick 1 or 2 break; } } this code works great if you enter any int value, but if you enter a character you get unexpected results. im looking for a simple code that will check what kind of data type was entered to see if its the kind of data i was looking for. if it is what im looking for i want it to continue, if not i want to enter the data again and again until its of the correct data type. please dont say something like look at this built in function as a reply. please tell me your ideas and some code that i may be able to use. Thank You, Andrew |
|
#2
|
||||
|
||||
|
input the value as a string, then convert it to an integer using atoi().... this will simply ignore any character values it comes across...
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Input validation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|