0

I am trying to make build a little program that would loop as long as the user presses a specific key but for a reason I can't see, it exits the loop Here's what it looks like

int main()
{
    char choice;

    do{
        printf("Do you want to start ? \nPress y to begin\n");
        scanf("%c",&choice);
        if(choice=='y' || choice=='Y'){
                
        printf("LETS GO \n");

        }

    }while(choice=='y' || choice =='Y');

        printf("END\n");


    return 0;
    }
Laspeed
  • 791
  • 1
  • 5
  • 27
  • 1
    Print the value of `choice` with `printf("%d", choice);` right after the loop. The value printed should ring a bell – Jabberwocky Mar 25 '22 at 08:37
  • Let me guess, you press the `Enter` key after the input? That will be added to the `stdin` input buffer as a newline `'\n'`. Which will be read by the next `scanf` call. – Some programmer dude Mar 25 '22 at 08:38
  • @Jabberwocky Seeing the magic number 10 might not ring a bell for beginners. (The magic number 7 will however literally ring a bell :) ) – Lundin Mar 25 '22 at 08:41
  • @Lundin that's a good one. But even if it does not ring a bell directly, the fact that `scanf` is obviously returning the value 10 "for no apparent reason", might trigger for some further research – Jabberwocky Mar 25 '22 at 08:42

0 Answers0