0

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");
    }

}
green_claws
  • 13
  • 1
  • 6

3 Answers3

1

Your loop never terminates because you never tell it to terminate. It continues to execute as long as a != 0. You initialize a to 1 and you never change it, so naturally you have an infinite loop.

You're also not checking whether scanf succeeded. If you enter some garbage value, it will leave n with its previous value and return a result indicating that it failed to read a new value, but your program doesn't distinguish between success and failure.

scanf returns an int result indicating how many items it was able to read (or EOF, a negative value, if there was an error). You should at least compare the value to 1 to see whether it succeeded.

It appears that you intend to use a to determine whether the loop should continue executing. You should give it a more meaningful name, like keep_going. And you can make it a bool (if you have #include <stdbool.h>, if your compiler supports it), but using an int with 0 for false and 1 for true is also ok.

int keep_going = 1;
while (keep_going) {
    /* ... */
    if (...) {
        keep_going = 0;
    }
}

Or you can write an infinite loop and use break to terminate it:

while (1) {
    /* ... */
    if (...) {
        break;
    }
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thank you. In C language, how to make scanf tell the type of data input, integer, float or char? – green_claws Mar 04 '14 at 21:07
  • You need to read the documentation for `scanf`, either by running `man scanf` if your system has man pages) or by [finding it on the web](http://linux.die.net/man/3/scanf). `scanf` format strings are similar (but not identical) to `printf` format strings; they're effectively small programs in a tiny special-purpose language. – Keith Thompson Mar 04 '14 at 21:25
0

Instead of using if else, I would suggest using a switch statement

If you need to use only if else, then include another else statement mentioning.

else
    exit(0); 

for your program to terminate in such a case.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
user3256147
  • 378
  • 2
  • 9
0
#include <stdio.h>

int main(int argc, char **argv)
{
    int looper = 1;
    while(looper){
        printf("Please enter an integer:\n");
        int i;
        scanf("%d", &i);
        if(i==0){
            looper = 0;
        }
        else if(i==1){
            printf("one\n");

        }
        else if(i==2){
            printf("two\n");
        }
        else{
            printf("I don't know that\n");
        }
    }

    return 0; 
}