Problem with getchar()

This is a discussion on Problem with getchar() within the C Programming forums, part of the General Programming Boards category; I am writing the following program: Code: //Prints a table of squares using a for statement #include <stdio.h> #include <stdbool.h> ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    Problem with getchar()

    I am writing the following program:

    Code:
    //Prints a table of squares using a for statement
    
    #include <stdio.h>
    #include <stdbool.h>
    
    int main (int argc, const char * argv[]) {
        // insert code here...
        //declarations
    	int i, n;
    	
    	printf("This program prints a table of squares.\n");
    	printf("Enter number of entries in table: ");
    	scanf("%d", &n);
    	
    	for (i = 1; i <= n; i++) {
    		printf("%10d%10d\n", i, i * i);
    		
    		if (i % 24 == 0){
    			printf("Press Enter to continue...");
    			if (getchar() == '\n')
    				continue;
    		}
    					
    	}
    	
    	return 0;
    }
    The problem is that the program does not wait for user to press the enter in the first 24 group of numbers.It waits only after the second group.Probably in the first getchar() execution there is a new line character in the buffer.
    Any ideas on how to handle this?
    Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by skiabox View Post
    Probably in the first getchar() execution there is a new line character in the buffer.
    Any ideas on how to handle this?
    Yeah, that's exactly what's happening - scanf() isn't reading it in, so it's left there for the first getchar() to find.

    Simplest way I can think of is to just add a call to getchar() after your scanf() call.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    I did that and it worked!
    Thank you very much for your help!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Your analysis of the problem is accurate. Just clear out any left over characters in the stream if scanf succeeds:
    Code:
    if (scanf("%d", &n) == 1) {
        while (getchar() != '\n')
            ;
    }
    else {
        /* Input failed, handle the error */
    }
    >Simplest way I can think of is to just add a call to getchar() after your scanf() call.
    Simplest, yes. But a single call to getchar may or may not work depending on the contents of the stream. It's generally not a good idea to assume the next character is always a newline, so I recommend the slightly less simple loop above.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM

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