new to C, need some easy help

This is a discussion on new to C, need some easy help within the C Programming forums, part of the General Programming Boards category; Hi guys, new to the forums here and liking what I see. I've been going through the tutorial section and ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Illinois
    Posts
    1

    new to C, need some easy help

    Hi guys, new to the forums here and liking what I see. I've been going through the tutorial section and ran into a problem with one of the programs. I'm confused as to why I need to use 2 getchar() funtions to get my program to pause correctly. If I only use one, as the tutorial suggests, it just opens and closes the command window super fast. Thx in advance! Here's the code:

    Code:
    #include <stdio.h>
    int main()
    {
        int myNum;
        
        printf("Enter a number: ");
        scanf("&#37;d", &myNum);
        getchar();
        printf("You entered %d", myNum);
        getchar();
    }
    EDIT: If it helps, I'm using the Bloodshed Dev-C++ IDE
    Last edited by amd84; 04-13-2008 at 12:15 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    When you type the number, and press ENTER, and use scanf to remove the integer, the newline character (from pressing ENTER), is still on the input stack. The first getchar remove the newline character. The second one causes the pause.

    Step through the code in your debugger and you will see it.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  3. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  4. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21