Thread: Cprogramming if statement tutorial and getchar

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    1

    Cprogramming if statement tutorial and getchar

    After a review of on-line resources and a review of the tutorial material, I don't entirely understand what getchar () is meant to do. I am only a few sections into the tutorial, so I'm not expecting to really... The way I'm using it is to hold my output screen, as mentioned by the tutorial. My XP ver 5.1 SP3 OS is shutting down output screens after programs execute faster than I can review. So as instructed by the tutorial, I am forced to use this command.

    That said, I copied the Cprogramming tutorial (Else IF Section) found here:

    If Statements in C - Cprogramming.com

    In my version of the code, I had to insert more getchar () commands to hold the window:

    Code:
    #include <stdio.h>
    
    int main()
    /* Code to work out how the if statement functions*/
    
    {
        int age;
        
        printf( "Please enter your age: " );
        scanf( "%d", &age );
        if (age < 100){
                printf("You are pretty young! \n");
                getchar(); 
                
        }         
        else if (age==100) {
                printf("You are pretty old! \n" );
                getchar(); 
                
        }        
        else 
                printf( "You are really old! \n");
                getchar();
                         
    
    getchar(); 
    return 0;
     }
    If I remove the getchar() at the if, else if, else statements, or the final position... or any combination thereof, the compiled code will not hold the window open. I'm not sure why I need to write this seemingly redundant code to hold the window. Seems like I should be able to hold the window with each conditional statement and no final getchar... Any help would be appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem that you have is presumably that when you ran your program from within your IDE, the command prompt window that it opened only stays open while the program is running. As a workaround, you made an additional, otherwise unnecessary, final request for input from the user so that the program remains running after it is effectively done.

    The thing is, when you entered input for your scanf call, the scanf read input up to the whitespace encountered, i.e., the newline entered remained on the input buffer. Therefore, if you only have one getchar call, it will consume that newline, thus your program ends and your problem of the prematurely closing window resurfaces. As yet another workaround, you have to add a second getchar call (or perhaps use a loop).

    Instead of this workaround, I suggest that you either configure your IDE to pause the window for you, or you open a separate command prompt window and run your program from there. This way, you won't need any of these extra getchar calls.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2013, 08:50 AM
  2. trying if statement in the tutorial and stumble a problem
    By abrofunky in forum C++ Programming
    Replies: 7
    Last Post: 02-12-2012, 03:50 PM
  3. Incorrect statement in the tutorial
    By Okiesmokie in forum C Programming
    Replies: 1
    Last Post: 08-25-2006, 03:42 AM
  4. merge sort tutorial from cprogramming.com
    By Micko in forum C Programming
    Replies: 0
    Last Post: 03-15-2005, 09:43 AM

Tags for this Thread