Exit the program when I press a key.

tomy2904 -  
Doctor C Posted messages 630 Registration date   Status Member Last intervention   -
Bonjour,

Here is the translation you requested:

Hello,

I would like to know which code to use to exit the program if the user presses a key on their keyboard.

Actually, I created a small menu:

1. Play
2. Exit

And I would like the program to close after displaying a message (for example) when the user presses key 2 (for instance).

Thank you very much for your help!!! :D

Configuration: Windows 7 / Firefox 3.6.12

6 answers

  1. tomy2904
     
    Here is the translation: --- There you go, I just tested your last proposal..

    When I choose the second option that is "Quit", the program does not display the "printf": S It goes directly to the next step that is the exit of the program, so "press any key..."

    Here is how my program looks with the menu and my selection:

    --------------------------------------------------------------------------------------------

    (1): Play
    (2): Game Rules
    (3): Quit

    What would you like to do? 3

    Process returned 4207747 (0x403483) execution time: 1.963 s
    Press any key to continue.

    -------------------------------------------------------------------------------------------

    --> There is no prompt before quitting.. I suppose therefore that it does not take into account "case 3" (in this case):/
    1
  2. Doctor C Posted messages 630 Registration date   Status Member Last intervention   400
     
    I assume you program in C?

    You can create a condition where if the user presses 2, you display a message like 'Do you really want to exit the program? (y/n)'.

    Then, you use a scanf to get the user's choice (yes or no) and if they type 'y', you execute the following code:

    exit(EXIT_SUCCESS);

    --

    Echo "Lima Mike Alfa";
    0
  3. tomy2904
     
    Ok, thanks a lot! :D

    Here's what I've done for case2:

    printf ("Do you really want to quit? Yes(Y)/No(N)");
    scanf ("%s", &letter); //variable declared earlier: ' char letter; '
    if (letter=='Y')
    {
    exit(EXIT_SUCCESS);
    }
    else
    {
    //How to return to the very beginning of the program to redisplay the menu??
    }

    Is this correct? And could you tell me what I could put in the "else" to return to my menu (do the loop thing)?

    Also, I tried your function: exit(EXIT_SUCCESS); but when I compile, I get error messages:
    - "warning: incompatible implicit declaration of built-in function 'exit' "
    - "error: 'EXIT_SUCCESS' undeclared (first use in function)
    - ...

    Thanks for your information and for your help of course!! ;)
    0
  4. Doctor C Posted messages 630 Registration date   Status Member Last intervention   400
     
    For the constant EXIT_SUCCESS, you might need to do an import, I don’t remember. But you can replace it with a 0, it will do the same thing.

    exit(0);


    Otherwise, for the menu, you can put it in a loop like this:

    do { printf("1. Play"); printf("2. Quit"); scanf("%d", &choice); switch(choice) { case 1: // game code break; case 2: printf("Do you really want to quit? Yes(Y)/No(N)"); scanf ("%s", &letter); if (letter=='Y') { exit(0); } break; } }while(true);


    If you don’t want to ask a question before quitting the program, you could replace the last line with:

    while(choice!=2);


    --

    Echo "Lima Mike Alfa";
    0
  5. Doctor C Posted messages 630 Registration date   Status Member Last intervention   400
     
    Maybe your choice value isn't correct.

    Add a 'case default' to your switch to check if your choice variable has the correct assigned value: https://www.commentcamarche.net/contents/111-langage-c-les-structures-conditionnelles

    If the switch goes to default, it means your choice variable is neither 1, nor 2, nor 3, which could be your problem.

    --

    Echo "Lima Mike Alfa";
    0
  6. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
     
    Hello,

    I'm jumping into the conversation.
    The error comes from the code.
     char lettre; scanf ("%s", &lettre); 

    lettre is a char, so you should use scanf("%c",&lettre); or more simply lettre=getchar();
    But don't forget to clear the keyboard buffer which will contain the '\n' character (enter key) after the previous scanf.
    To clear it, you can simply use getchar() (or more cleanly with a while loop).

    In conclusion, replace scanf("%s",&lettre); with :
     getchar(); lettre=getchar(); 

    Best regards,

    Google is your friend
    0
    1. Doctor C Posted messages 630 Registration date   Status Member Last intervention   400
       
      Thank you for intruding, I admit that my C is quite distant and I have forgotten some specific notions of the language.
      0