0

When I enter 0 the program ends. But there is getchar() in if statement and it doesn't work, can you help me?

On case 0 I want it to get char from user. If 'N' or 'n' is entered the program will end but if not the program will start over again. (from sec1).

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;

    sec1:
    printf("Select a number between 4 and 0: ");
    scanf("%d[\n]", &n);

    switch(n)
    {
        case 0:
            puts("Are you sure?");
            puts("Yes(Y) or No(N)");
            if(getchar() == 'N') goto sec1;
            break;

        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;

        default:
            puts("Only numbers between 4 and 1 are accepted!");
            goto sec1;
            break;

    }
    system("pause");
    return 0;
}

working one

int main()
{
    int n, c;

    sec1:
    printf("Select a number between 4 and 0: ");
    scanf("%d/n", &n);

    switch(n)
    {
        case 0:
            puts("Are you sure?");
            puts("Yes(Y) or No(N)");
            fflush(stdin);
            if (getchar() == 'N') goto sec1;
            break;

        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;

        default:
            puts("Only numbers between 4 and 1 are accepted!");
            goto sec1;
            break;

    }
    system("pause");
    return 0;
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
user2588276
  • 15
  • 1
  • 3
  • 2
    why are you using a `goto`? Just use a loop. – clcto Nov 11 '13 at 20:46
  • Hint: Does it behave differently if you press 'N' or 'n'? – mattnz Nov 11 '13 at 20:47
  • actually condition was if(getchar()=='N' || getchar()=='n') but i couldnt do it – user2588276 Nov 11 '13 at 20:48
  • 1
    @user2588276: If that's the actual condition, why are you showing us something different? Show us real code. It doesn't work because there are two calls to `getchar()`. If you're going to look at the result of `getchar()` more than once, assign it to a variable and look at the variable: `int c; c = getchar(); if (c == 'N' || c == 'n') ...` – Keith Thompson Nov 11 '13 at 20:50
  • i said it was* edit:also its not working with the way you said it still ends without asking user char – user2588276 Nov 11 '13 at 20:51
  • "it doesn't work" is not a very good problem description. Tell what output or result you get, and what is expected or wanted output or result, how does it not work... Though I have to add, your code snippet is very good: complete yet short, and properly formatted and indented too :) – hyde Nov 11 '13 at 20:58
  • expected result is if user enters 0 it will ask yes or no, and if user enter Y it will end.If user enter N it will return to beginning(sec1)-problem is when user enters 0 it ends without asking anything. – user2588276 Nov 11 '13 at 21:06

3 Answers3

1

Change

scanf("%d[\n]",&n);

to

scanf("%d\n",&n);

The reason that your original code does not work is because scanf wants to read an integer, a "[", a "\n" and a "]". So when you input an integer followed by a "\n", scanf only takes the integer(because it expects to see a "["). Then the getchar will simply take the remaining "\n". That's why your getchar() seems not working.

Hope it is helpful to you!

ZillGate
  • 1,173
  • 12
  • 22
0

Probably you have some symbols in the input buffer when you are reading "N" or "n". You need to flush an input buffer before new reading. See this question for details.

Briefly just write: while (getchar() != '\n') {} right after scanf(...).

Community
  • 1
  • 1
0

I guess this is the terminal which is in cooked mode, thereby only feeding typed characters to the program when the user completes a line by hitting return. You can test it by feeding it input from a pipe instead of interactively from the terminal.

user2719058
  • 2,183
  • 11
  • 13