Thread: Enter to Continue, Esc to leave

  1. #1
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Question Enter to Continue, Esc to leave

    Hi there. Im having this:

    Code:
    printf("Press Enter to continue or Esc to leave the program.");
    Now, if the user press "Enter" the program starts again from the begining. If the Escape key is pressed, just quit the program.

    Any tips?

    Thx
    July

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I presume you want to be able to read a key without the user having to hit enter first. That's not possible in standard C. Check the FAQ, a couple of system-specific methods are covered.

  3. #3
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19
    I dont know If anyone got it, but its a program to change Celsius to Fahrenheit.

    Code:
    main(){
           float var, opc;
           
           printf("Type the value:");
           scanf("%f", &var);
           
           printf("\n(1)Celsius to Fahrenheit\n(2)Fahrenheit to Celsius\n\nType your number:");
           scanf("%f", &opc);
           
           if (opc == 1)
              {
              var = var * 9 / 5;
              var = var + 32;
              }
                  
           if (opc == 2)
              {
              var = var - 32;
              var = var * 5 / 9;
              }
           
             
           printf("%.1f", var);  
           system ("pause > null");
    
           printf("Press Enter to continue or Esc to leave the program.");
    
           }
    Pressing Enter will start the program again... =P

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here's the first result I found in Google: Getch() Esc Key - C And C++ | Dream.In.Code
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19
    I need something like, "Press Enter do start the program again".
    What do I need to do?

  6. #6
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int calc()
    {
      
        float var, opc;
        int cmd;
        cmd = 0;
        
        printf("Type the temperature: ");
        scanf("%f", &var);
        
        printf("\n(1)Celsius to Fahrenheit\n(2)Fahrenheit to Celsius\n\nType here: ");
        scanf("%f", &opc);
        
        if (opc == 1)
          {
          var = var * 9 / 5;
          var = var + 32;
          }
              
        if (opc == 2)
          {
          var = var - 32;
          var = var * 5 / 9;
          }
        
        printf("%.1f", var);  
        system ("pause > null");
        return 0;
       
    }
    
    int main()
    {
    
    calc();
    
    }
    Now I just need to keep restarting it over and over, till esc is pressed.

    Thx. =)

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Have you learned how to use while loops yet?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Talking Done.

    I used the do/while, and its ok now. thx for the help
    For anyone how ever needs it, here is the code:

    Code:
    /* Celsius to Fahrenheit and vice-versa */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
      
        float var, opc;
        int c;
        
        do{
    
            printf("Type the value: ");
            scanf("%f", &var);
            
            printf("\n(1)Celsius to Fahrenheit\n(2)Fahrenheit to Celsius\n\nOption: ");
            scanf("%f", &opc);
            
            if (opc == 1)
              {
              var = var * 9 / 5;
              var = var + 32;
              }
                  
            if (opc == 2)
              {
              var = var - 32;
              var = var * 5 / 9;
              }
            
            printf("%.1f\n\n", var);
            printf("Type 1 to continue or 0 to exit: ");
            scanf("%d", &c);
            system ("cls");
    
        }while (c != 0);
    }

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    system ("cls");
    That'll only work on DOS/Windows though.
    On UNIX it's "clear" instead of "cls".
    If your teacher tries running it on a different OS, it won't clear the screen and say that it doesn't know what "cls" is.

    One way to make it work on any OS is to just print about 50 '\n' characters instead of clearing the screen (although it won't look at nice, since you can just scroll up in the command window and see all the previous output).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Or just note that almost all actual command line tools (with the possible exception of the "more"-like tools) donīt bother with clearing the screen at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  3. Assignment output help
    By Taka in forum C Programming
    Replies: 13
    Last Post: 09-23-2006, 11:40 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Press enter to continue... beginner code
    By rox in forum C Programming
    Replies: 17
    Last Post: 12-02-2003, 05:32 PM