fix my code, programme doesnt quit when 'q' or 'Q' is inputted

#include <stdio.h>
int main()
{
char inchar, bar;
int quit = 0;



while(!quit)
{
printf("Please input a character and then <Enter>,\n'q' for quit: \n");
scanf("%c%c",&inchar,&bar);

switch (inchar)
{
case 'q' : case 'Q' :
printf("The program quits, as you want...\n");
break ;

default :
printf("The character you types is [%c]\n", inchar);

}
}

return 0;
}
@Limitless98

Try putting quit = 1; right after printf("The program quits, as you want...\n");

You have a while loop, that won't stop UNTIL quit does not equal 0.
You need to change the value of int quit when you want that loop to end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
 
int main()
{
    char inchar;

    int quit = 0;

    while (!quit)
    {
        printf("Please input a character and then <Enter>,\n'q' for quit: \n");

        scanf(" %c", &inchar);

        switch (inchar)
        {
            case 'q' : 
            case 'Q' :
                printf("The program quits, as you want...\n");
                quit = 1;
                break ;

            default :
                printf("The character you typed is [%c]\n", inchar);
        }
    }

    return 0;
}

thanks
Topic archived. No new replies allowed.