0

If the input is a number that is not 1 or 2, than it makes the correct input(default). If the ipnut is a letter like "a" than it starts to spam the default case.

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

int main()
{
    int v, kapcs = 0;
    printf("Szerinted Szaki Trisztan egy kocsog?\n1 Igen\n2 Nem\n");

    do
    {
        kapcs = 0;

        scanf("%d", &v);

        switch (v)
        {
            case 1:
                printf("Igen, o egy kocsog!");
                break;
            case 2:
                printf("Helytelen... O egy kocsog!");
                break;
            default:
                printf("NEM VALASZLEHETOSEG, VALASSZ: 1 VAGY 2\n");
                kapcs = 1;
                break;
        }

    }
    while (kapcs == 1);

    return 0;
}
Martin
  • 7
  • 4

2 Answers2

0

scanf %d reads decimal digits. 'a', being not a digit, is left on the input stream for the next loop - forever. If you want the 'a' to be consumed, you have to add a line of code to your default case to consume it.

But how do you distinguish an 'a' from a '3'? The answer is scanf returns the number of arguments converted. So, for an 'a', scanf will return 0, and for a '3', scanf will return 1.

int main()
{
    int v, kapcs = 0;
    printf("Szerinted Szaki Trisztan egy kocsog?\n1 Igen\n2 Nem\n");

    do
    {
        kapcs = 0;

        int cnt = scanf("%d", &v);

        switch (v)
        {
            case 1:
                printf("Igen, o egy kocsog!");
                break;
            case 2:
                printf("Helytelen... O egy kocsog!");
                break;
            default:
                printf("NEM VALASZLEHETOSEG, VALASSZ: 1 VAGY 2\n");
                kapcs = 1;
                if (cnt == 0)     /* if input was not a number */
                    getchar();    /* skip a character of input */
                break;
        }

    }
    while (kapcs == 1);

    return 0;
}
CAB
  • 1,106
  • 9
  • 23
0

When you input something that doesn't matches the required format in the scanf(), that value stays in the input buffer. So what you can do is add a getchar() in your default case.

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

int main()
{
    int v, kapcs = 0;
    printf("Szerinted Szaki Trisztan egy kocsog?\n1 Igen\n2 Nem\n");

    do
   {
    kapcs = 0;

    scanf("%d", &v);

    switch (v)
    {
        case 1:
            printf("Igen, o egy kocsog!");
            break;
        case 2:
            printf("Helytelen... O egy kocsog!");
            break;
        default:
            printf("NEM VALASZLEHETOSEG, VALASSZ: 1 VAGY 2\n");
            kapcs = 1;
            getchar();
            break;
    }

}while(kapcs == 1);

return 0;
}

Also to support my argument you could do the following: #include #include

int main()
{
    int v, kapcs = 0;
    printf("Szerinted Szaki Trisztan egy kocsog?\n1 Igen\n2 Nem\n");

    do
   {
    kapcs = 0;

    scanf("%d", &v);

    switch (v)
    {
        case 1:
            printf("Igen, o egy kocsog!");
            break;
        case 2:
            printf("Helytelen... O egy kocsog!");
            break;
        default:
            printf("NEM VALASZLEHETOSEG, VALASSZ: 1 VAGY 2\n");
            kapcs = 1;
            printf("%c",getchar());
            break;
    }

}while(kapcs == 1);

return 0;
}

By adding this little printf() you will notice that the getchar doesn't seem to wait for the input. Instead it simply outputs that character you just provided in the input stream.

P.S. :Please note that this answer was 'just' made for the clarification on that unmatched input part. You NEED to be careful if you input a number that doesn't match your case. In that case simply check if the scanf() conversion was successful or not by taking a count of it as following:

count=scanf("%d",&v);

and then checking in the default case as:

if(count==0){
 getchar();
}
Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
  • Be careful, as `getchar()` might block indefinitely if `scanf()` popped a decimal character that was neither 1 or 2. – Jazzwave06 Feb 10 '17 at 18:46