Thread: getchar() doesn't wait for input

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Seattle, WA
    Posts
    5

    getchar() doesn't wait for input

    What's going on here? It seems that the program isn't waiting for me to input a character, but just skips right to the 2nd number. It's almost like it's taking the newline from num1, except i am using fflush(stdin) so it shouldn't be doing that. Notice that the 3 printf statements i put in there to show me what is going on, the one for operator comes up with a blank line meaning I think either space or new line... There may be problems with my if/else if, but I won't be able to work on it until my program deals with the operator first. I'd like to concentrate on that, even if you see a problem with my if/else if structure. Here is the program & output follows it:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
    	/* declare variables */
    	/* ***************** */
    	
    	float num1, num2, result;
    	char oper = '+';
    	
    	/* display greeting */
    	/* and get info     */
    	/* **************** */
    	
    	printf("Welcome to the Acme Simple Calculator!\n");
    	printf("Enter a number: ");
    	scanf("%f", &num1);
    	fflush(stdin);
    	printf("num1 = %f\n", num1);
    	printf("Enter a math operation +, -, *, /: \n");
    	oper = getchar();
    	printf("The operator is %c", oper);
    	fflush(stdin);
    	printf("Enter a second number: ");
    	scanf("%f", &num2);
    	fflush(stdin);
    	printf("num2 = %f\n", num2);
    	
    	/* calculate results */
    	/* & display results */
    	/* ***************** */
    	
    	if ( oper = '+' )
    		result = num1 + num2;
    	else if ( oper = '-' )
    		result = num1 - num2;
    	else if ( oper = '*' )
    		result = num1 * num2;
    	else if ( oper = '/' )
    		result = num1 / num2;
    	else
    		printf("Invalid operator.\n");
    
    return 0;
    
    } /* end main */
    
    
    [Session started at 2010-04-26 21:52:34 -0700.]
    Welcome to the Acme Simple Calculator!
    Enter a number: 3.5
    num1 = 3.500000
    Enter a math operation +, -, *, /: 
    The operator is 
    Enter a second number: 9.7
    num2 = 9.700000
    The Debugger has exited with status 0.
    Last edited by Salem; 04-27-2010 at 09:47 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fflush is not for input streams. You are invoking undefined behavior, which in your case, means it doesn't work the way you think it should.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I expect that it's your fflush(stdin) that fails, and your getchar() ends up snatching the new line character left in the buffer.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Seattle, WA
    Posts
    5
    Agreed, it doesn't seem to be working. Any thoughts on how I might go about getting rid of the new line character then?
    Last edited by cantinero74; 04-26-2010 at 11:47 PM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    A pretty standard approach is to use something like this:

    Code:
    while(getchar()!='\n')
    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    Cprogramming.com FAQ > Flush the input buffer

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Not to mention - THIS -> << !! Posting Code? Read this First !! >>
    You've had your one free fix.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I don't understand "while (getchar() != '\n');"
    By valedor in forum C++ Programming
    Replies: 13
    Last Post: 09-08-2009, 05:12 PM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM

Tags for this Thread