Quote:
Originally Posted by hardikgohil1988
Sorry mistaken code posted
Code:
char c[10];
printf("Enter Your Choice :");
scanf("%s",c);
i = atoi(c);
switch(i){
}
example I type
Enter your choice:123backspace
should be 12 but prints invalid option as defined at default switch case.
|
My test code:
Code:
$ cat readNum.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int i;
char c[10];
printf("Enter Your Choice :");
scanf("%s",c);
i = atoi(c);
printf("You entered %d\n",i);
}
$ gcc -o readNum readNum.c
$ ./readNum
Enter Your Choice :784 // entered 789<backspace>4
You entered 784
$ ./readNum
Enter Your Choice :12 // entered 123<backspace>
You entered 12
$ _
Clearly, you deviated from the script somewhere.