Thread: getchar() just doesn't get it.

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Question getchar() just doesn't get it.

    What is wrong with the following code, that getchar() won't wait for input?
    Please help, thank you.

    Code:
    #include <stdio.h>
    
    main()
    {
    	float cel, fah;
    	char r;
    	do 
    	{
    		printf("Input the Fahrenheit temperature you want to convert: ");
    		scanf("%f", &fah);
    		cel = (5.0/9.0)*(fah - 32);
    		printf("%5.1f degree F is %5.1f degree C. ", fah, cel);
    		printf("Do you want to continue? ");
    	}
    	while ((r = getchar()) == 'y');
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Because scanf() left the '\n' (newline) char still in the keyboard buffer (aka input stream in this case).

    So when getchar() looks at the buffer, it see's the newline, pulls it off the buffer, and says "Tally Ho!". "I'm all set!".

    put a getchar() after the scanf(), to pull the newline off the keyboard buffer, and all will be well.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Adak View Post
    put a getchar() after the scanf(), to pull the newline off the keyboard buffer, and all will be well.
    Unless of course the user inputted something like "51.6X" in which case the getchar() will retrieve the "X" and the newline still sits in the buffer, creating the same problem all over again.

    There's no substitute for input validation.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    There's no substitute for input validation.
    What is "input validation"?

    And here is another exercise that I don't know how to solve:
    Write a program to prove that "something == EOF" has only 2 values.

    Here is my answer, it didn't work out, though.

    Code:
    #include <stdio.h>
    
    main()
    {
    	char r;
    	int i;
    
    	do 
    	{
    		i = (getchar() == EOF);
    		printf("%d\n", i);
    		getchar();
    	}
    	while ((r = getchar()) == 'y');
    }
    Please help me out.
    Thank you.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    that would never work.

    simply do:

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("%d\n", EOF);
        getchar(); //pause program
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    That only prints out EOF.
    And could you please tell me why that will not work?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kindlychung View Post
    What is "input validation"?

    And here is another exercise that I don't know how to solve:
    Write a program to prove that "something == EOF" has only 2 values.

    Here is my answer, it didn't work out, though.

    Code:
    #include <stdio.h>
    
    main()
    {
    	char r;
    	int i;
    
    	do 
    	{
    		i = (getchar() == EOF);
    		printf("%d\n", i);
    		getchar();
    	}
    	while ((r = getchar()) == 'y');
    }
    Please help me out.
    Thank you.
    It looks about right - what are you seeing, and how do you "feed" your program?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by Elysia View Post
    Authorization Required

    This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    It looks about right - what are you seeing, and how do you "feed" your program?
    It will give the value once and then terminate, in spite of the do...while... loop.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kindlychung View Post
    It will give the value once and then terminate, in spite of the do...while... loop.
    Ah, yes, of course. If you make it terminate when you hit 'n' (or when you get back EOF?) rather than terminate on anything but 'y', it may work better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Code:
                    i = (getchar() == EOF);
    		printf("%d\n", i);
    		
    	}
    	while ((r = getchar()) != 'n');
    }
    mats,

    I just changed it a little bit, and it seems to work. But I guess "!='n'" and "=='y'" are essentially the same thing, aka the condition of the loop, why are the effects so different?

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by kindlychung View Post
    Code:
                    i = (getchar() == EOF);
    		printf("%d\n", i);
    		
    	}
    	while ((r = getchar()) != 'n');
    }
    mats,

    I just changed it a little bit, and it seems to work. But I guess "!='n'" and "=='y'" are essentially the same thing, aka the condition of the loop, why are the effects so different?
    because when you do '==y' you are only accepting the character 'y', and since when the user hits 'enter', it inputs a '\n' to the stream, your program will exit right after the first character you entered. what you really should be doing for your program to work is something like "=='y' || == '\n'"

    because when you type 'y' in the console and hit enter, your input is actually: "y\n"

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kindlychung View Post
    Code:
                    i = (getchar() == EOF);
    		printf("%d\n", i);
    		
    	}
    	while ((r = getchar()) != 'n');
    }
    mats,

    I just changed it a little bit, and it seems to work. But I guess "!='n'" and "=='y'" are essentially the same thing, aka the condition of the loop, why are the effects so different?
    abraham sort of answers this, but I would put it this way: Your initial example exits whenever the reply is NOT 'y' - so anything OTHER than 'y' at that point will exit, the new code exits ONLY when the reply is 'n' - all other replies (including 'y', newline, etc) continues.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kindlychung View Post
    Authorization Required

    This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.
    Dang thing.
    Strip the "s" in https:
    http://apps.sourceforge.net/mediawik...n_input_buffer

    I swear it. This new server is an annoying one.
    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. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 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