Thread: HELP! Program shuts down when I press Enter.

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    HELP! Program shuts down when I press Enter.

    Hi there,

    I'm new to programming and i'm having problems when it comes to using programs I code. For example:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char me[20];
    
        printf("what is your name?");
        scanf("%s",me);
        printf("Darn glad to meet you, %s!\n",me);
    
        return(0);
    }
    Whenever I try to use this program (Typing in name then pressing enter) the program shuts down when I press enter...
    Are there any ways to fix this?

    Cheers,
    Jack

  2. #2
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    you can put

    Code:
    system("pause");
    before return 0. it will pause the program.

    jannr

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User Timon's Avatar
    Join Date
    Jul 2011
    Location
    Bangalore
    Posts
    4
    If your are running the code in windows then use the function getch() after the print statement.

    Code:
    #include <stdio.h>
    #include<conio.h>
    int main()
    {
        char me[20];
    
        printf("what is your name?");
        scanf("%s",me);
        printf("Darn glad to meet you, %s!\n",me);
        getch();
        return(0);
    }
    If you are running on other platforms use this simple method for checking.

    Code:
    #include <stdio.h>
    int main()
    {
        char me[20];
        char ch;
    
        printf("what is your name?");
        scanf("%s",me);
        printf("Darn glad to meet you, %s!\n",me);
    
        printf("Press Enter to quit...");
        scanf("%c",&ch);
        return(0);
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jrap123 View Post
    Hi there,

    I'm new to programming and i'm having problems when it comes to using programs I code. For example:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char me[20];
    
        printf("what is your name?");
        scanf("%s",me);
        printf("Darn glad to meet you, %s!\n",me);
    
        return(0);
    }
    Whenever I try to use this program (Typing in name then pressing enter) the program shuts down when I press enter...
    Are there any ways to fix this?

    Cheers,
    Jack
    What the others have said.... Plus... If you are running the program by clicking on it with your mouse, you are opening a console window that automatically closes when the program finishes. The better way to run console mode programs is to open a command prompt and then run the program by typing it's name. That way the console will stay open and you can still see your program...

    Introduction to the Windows Command Prompt

  6. #6
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    While you're at it, you don't need to put () around the return value in this case

    Code:
    return 0;
    Will work just wonderful.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by \007 View Post
    While you're at it, you don't need to put () around the return value in this case

    Code:
    return 0;
    Will work just wonderful.
    This is true... but the brackets are harmless. Personally I think bracketing after return is more consistent with the rest of the language...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-02-2011, 11:18 AM
  2. Press Enter Twice
    By Alone882 in forum C++ Programming
    Replies: 14
    Last Post: 01-04-2011, 01:45 PM
  3. Press "Enter" key to quit the program
    By Vitamin_C in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2005, 08:25 PM
  4. press enter to continue
    By Klinerr1 in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2002, 11:43 AM
  5. if i press enter
    By abbynormal87 in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2002, 05:37 PM