As a beginner I learn programming currently with Sublime Text on OSX. The code attached is compiled via Sublime Text. The runtime error is, when I input any non-integer data such as 'a' or '9.3', the while-loop will not break.
Below is the instruction of work:
Test a program with only a main() function that repeatedly:
1. requests the user to enter an integer,
2. reads an integer,
3. processes the integer as follows: returns to terminate the program for 0, prints "one" for 1, prints "two" for 2, prints "I don't know that" for any other integer.
e.g. the following is a possible sequence that might appear at the terminal, where the integers are entered by the user:
Please enter an integer
2
two
Please enter an integer
33
I don't know that
Please enter an integer
1
one Please enter an integer
0
Below is my code.
Thanks for help.
#include <stdio.h>
int main(){
int n;
int a = 1;
while (a!=0){
printf("Please enter an integer\n");
scanf("%d", &n);
if (n == 1) printf("one\n");
else if (n==2) printf("two\n");
else if (n==0) return 0;
else if (n<0 || n >2) printf("I don't know that\n");
}
}