Thread: C Programming: Simple application closes after input.

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    5

    C Programming: Simple application closes after input.

    Hey guys, I'm just starting to learn C, and I was trying some of the tutorials. I started experimenting in CodeBlocks. When I build and run it inside of CodeBlocks, it's fine. It will come up with a command prompt screen, then at the end, it will say "Press any key to continue."

    I tried getchar(); but it still won't work. Here is what I have.

    Also, I've searched for other solutions, but all of them are for C++.

    Code:
    #include <stdio.h>int main()
    
    
    {
        int pizza;
    
    
        printf("How many slices of pizza do you have right now?\n");
        scanf("%d", &pizza);
    
    
    if (pizza <=2) {
        printf("Get more pizza!\n");
        }
    
    
    else if (pizza <=8) {
        printf("You should have enough pizza...\n");
        }
    else {
        printf("YOU HAVE WAY TO MUCH PIZZA. GIVE ME SOME I'LL GET RID OF IT FOR YOU.\n");
        }
        getchar();
        return 0;
    }
    So how do I make it so that when I put in a number, and press enter, it doesn't close automatically?

    Please explain this to me like I'm 5 years old.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Please explain this to me like I'm 5 years old.
    To a child: I asked for one slice of pizza, but you gave me two and I'm going to bed as a result.

    To an adult: Your "scanf()" will read an integer from the input buffer, but when you press 'enter', there are actually two things in the input buffer; the number you entered, and a newline. For instance:

    Code:
    5\n
    There are several ways to deal with this. A quick fix would be to place a "getchar()" directly after the "scanf()" to eat up the newline. Keep the other "getchar()" at the end of your code if you want to wait until you press 'enter' to close the program.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, DevSun!

    Lots of ways to do this, but the simplest may be to just add another getchar(), right after the scanf().

    That will clear out the newline which is left behind by scanf(), so that the SECOND getchar() will stop as you want it to. There is a while() that is more robust and pulls off several char's from the input buffer, if present, but it's a bit more difficult to grok.

    You've been reading my mind again, Matticus - that's uncanny!

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    5
    Oh great thanks! And for future reference, whenever I have a scanf(), do I put a getchar(); after it?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You've almost given me a heart Adak!
    But seriously, I love your never ending patience with these things. You're definitely a great asset to this community!

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by DevSun View Post
    Oh great thanks! And for future reference, whenever I have a scanf(), do I put a getchar(); after it?
    The greatest thing about programming is that you can see for yourself.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x;
    
        printf("Enter an integer:  ");
        scanf("%d",&x);
        printf("You entered %d\n",x);
    
        getchar();  // <--- does it work as you expect?
    
        return 0;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char x;
    
        printf("Enter a character:  ");
        scanf("%c",&x);
        printf("You entered %c\n",x);
    
        getchar();  // <--- does it work as you expect?
    
        return 0;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float x;
    
        printf("Enter a floating point number:  ");
        scanf("%f",&x);
        printf("You entered %f\n",x);
    
        getchar();  // <--- does it work as you expect?
    
        return 0;
    }

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You can ask to read another character (i.e. '\n') and do nothing with it by using "%*c"

    i.e.
    Code:
    scanf("%d%*c", &pizza);

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    scanf just sucks for that reason.
    I would use the following:
    Code:
    char buf[100];
    ...
    printf("How many slices of pizza do you have right now?\n");
    gets(buf);
    sscanf(buf, "%d", &pizza);
    ... but people will point out that 'buf' can be overflowed by malicious intent if the user types in too many characters that exceed its allotted length. There are other functions that read a maximum amount.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Of course you mean fgets and not gets right nonoob?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Possibly. but if Kernighan and Ritchie hadn't intended for us to use gets() they wouldn't have invented it.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    If gets() is such a good function why did they remove it from the current standard (C11)?

    Bye, Andreas

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by AndiPersti View Post
    If gets() is such a good function why did they remove it from the current standard (C11)?

    Bye, Andreas
    I believe that it's because it that it has no checks with the string size - This leads to a situation where code is susceptible to buffer overflows
    Last edited by Click_here; 08-30-2012 at 05:52 PM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Click_here
    I believe that it's because it that it has no checks with the string size - This leads to a situation where code is susceptible to buffer overflows
    Yeah, but I'm fairly certain AndiPersti knew that: the question was rhetorical. That said, I suspect nonoob was just being silly to begin with
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Sorry, I processed that sentence differently in my head the first time around - I completely missed the tone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-18-2012, 04:20 AM
  2. Console application - input handling
    By Something in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2007, 02:19 PM
  3. Program closes when startup form closes
    By dcboy in forum C# Programming
    Replies: 1
    Last Post: 07-01-2006, 02:40 AM
  4. Keyboard input in Windows NT/2K/XP native application
    By kcahcn in forum Windows Programming
    Replies: 3
    Last Post: 11-18-2002, 02:24 AM
  5. Simple Application Help
    By Psyke2600 in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 03:04 PM