Thread: Using getchar to keep program running

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    15

    Using getchar to keep program running

    In the beginner tutorial on this site, it mentioned that adding the line getchar(); right before return 0; at the end of the main function would keep the program running until the user hit enter.

    Well, this works for me on some programs, and not on others, and I really can't see the difference between them. Following is the main function from a program I wrote based off of one of the tutorial examples. Adding getchar(); doesn't keep the program from closing, either in my program or in the tutorial example (which did not have it included, I added it).

    Code:
    {
    
       ask();
    
       if(age < 0) {
       	printf("You can't have a negative age!\n\n");
          ask();
       }
       else if(age > 0 && age < 90) {
       	printf("You are less than 90 years old.\n");
       }
       else {
       	printf("Wow, you're pretty old!\n");
       }
    
       getchar();
    
       return 0;
    
    }
    Any help in explaining this would be greatly appreciated.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well i suspect because of the scanf function usage in some where in your code. Could you please show some more snap of your code.

    Hint: Try placing two getchar's one after the other.

    ssharish

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    15
    Ok, here's my entire program. It runs perfectly fine besides automatically closing after it runs, but if you have any other suggestions those would be greatly appreciated as well.

    Code:
    #include <stdio.h>
    
    int ask(void);
    int main(void);
    
    int age;
    
    int ask(void)
    
    {
    		printf("Please enter your age.\n");
       	scanf("%d",&age);
    
    	return 0;
    
    }
    
    
    int main(void)
    
    {
    
       ask();
    
       if(age < 0) {
       	printf("You can't have a negative age!\n\n");
          ask();
       }
       else if(age > 0 && age < 90) {
       	printf("You are less than 90 years old.\n");
       }
       else {
       	printf("Wow, you're pretty old!\n");
       }
    
       getchar();
    
       return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have to look at the stream of char's in your keyboard buffer (or input stream).

    You have an ask() function, and at some part in that function, the user is entering number (say 30), and hitting enter.

    The enter key puts a \n newline char after the 30. Your program displays or does something with the 30 the user enters, but the \n char is still there.

    Now your program goes to get a char from the stream with getchar(), and sure enough, a char is already there - the \n, so it gets it, and instantly move on.

    Every user entry will have at least one \n char. What you can do is either add a getchar() after every user entry, (which will pull off the newline char), or do something like:

    Code:
    while((c = getchar()) != '\n');
    getchar();

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    15
    Ok, adding getchar right after my scanf made it work fine. Thanks for your help!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'd say the best place to place them is directly underneath your scanf lines, to eat up anything remaining in the buffer, as to not cause any more problems for later lines.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    15
    Oh, now that I've already posted my entire code, I'm having another issue and I don't quite know how to resolve it. After adding getchar after the scanf in my ask function, it works fine. Another thing that works fine is where I told it to run the ask function again after scolding the user if the age entered is less than 0.

    The problem is when I combine the two. If I enter a number less than 0, it runs the ask function again, but then it never prints my message. I think I know why: the ask function is inside the if statement, but the value of the ask function is evaluated outside of it in the main function. How do I get it to re-run my ask function for values less than 0 but still send the age value back to the main function to be evaluated again?

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well thats going to be a bit of work you then, since you will have to return the local variable of ask function to the main back again. There are many ways to resolve that problem. What I would suggest to start with is to bring that scanf statement down to the main function itself.

    And that should resolve the problem easily. Or you will have to send variable as an argument to the ask function as a reference parameter and read the value in the function and then use that read value in the main function by dereferencing it. You will have to be familiarized the pointers to do that!

    ssharish

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make the ask function return the age instead of setting it to a global variable.
    Use a loop in main to loop while age == 0.
    That's all there is to it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program running long
    By smarta_982002 in forum Windows Programming
    Replies: 3
    Last Post: 03-27-2008, 05:24 AM
  2. Running my program in the background
    By shoobsie in forum Windows Programming
    Replies: 4
    Last Post: 10-09-2005, 02:38 AM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM
  5. Running program
    By muffin in forum C Programming
    Replies: 5
    Last Post: 08-30-2001, 10:57 AM