Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to determine the data type entered by the keyboard
How to determine the data type entered by the keyboard

For your question, because the input is only 0 to 9 and carriage return, if you enter other characters, it can be treated as invalid. The ASC code corresponding to 0 to 9 is 48 to 57 and the ASC code corresponding to carriage return. is 13 then we can write the program as follows. #include

void main()

{char Indata; /*Keyboard input character variable*/

int num; /*Storage Result variable*/

num=0; /*Initialization*/

while(1) /*Infinite loop*/

{

< p>Indata=getch(); /*Get the key value entered by the keyboard*/

if (Indata>47&&Indata<58) /*Judge whether it is 0-9*/

{ /*If it is 0-9, display it and calculate the corresponding integer value*/

printf("%c",Indata);

num=num*10 +Indata-48;

}

if (Indata==13) break; /*If the input is the Enter key, break out of the loop. At this time, what is placed in num is the final result we need*/

}

printf("result:num=%d",num);

getch(); /*Pause after the program ends to see the running results clearly. Press any key to end*/

}

This program is compiled in TC2.0 environment, and the above is just an algorithm. It's up to you to decide how to add it to your program. I can only help you with this. Even so, it can only be judged that the input is an integer, but the size cannot be judged. For example, the maximum integer value can be 65535. If the input exceeds this limit, the program will still error. How to judge the size is another topic. In addition, there is a negative sign missing in my program, because integers also include negative integers, you can add them yourself.