Thread: What does getchar do with the second next character from stdin?

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    82

    What does getchar do with the second next character from stdin?

    What does getchar do with the second next character from stdin?
    Example :

    example = getchar();

    User input :

    ABC

    Output :

    A

    Where does the BC goes?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It was left in the input buffer, waiting to be read by say, another two 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

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by xeon321 View Post
    What does getchar do with the second next character from stdin?
    Example :

    example = getchar();

    User input :

    ABC

    Output :

    A

    Where does the BC goes?
    BC are maintained into the buffer.Try this code in oder to understand how this works.
    Code:
     char a;
        printf("Please type a word\n");
        while(1)
        {
            a=getchar();
            printf("%c\n",a);
        }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Stdin is line buffered, if you type ABC and hit enter getchar() will get 1 char from the buffer, the first one happens to be A.

    Code:
    while( (example = getchar()) ) {
         putchar(example);
    }

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by std10093 View Post
    BC are maintained into the buffer.Try this code in oder to understand how this works.
    Code:
     char a;
        printf("Please type a word\n");
        while(1)
        {
            a=getchar();
            printf("%c\n",a);
        }
    Hmm interesting example.
    Mind to explain why you use while(1) ? What does the 1 do?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by xeon321 View Post
    For this one, it will work if the user's input is YYYYYYYYYYYYYYYYYYYYY. As long as the first character is Y.
    How to make it display "Oops! Wrong entry!" message?

    Code:
    #include <stdio.h>
    
    int main() {
    
    char example;
    
    printf("Enter Y : ");
    example = getchar();
    
    while ( example != 'Y' && example != 'y')
    {
     printf("Oops! Wrong entry!");
     example=getchar();
    }
    
    printf("Not looped");
    
    return 0;
    }
    If you type "sam" for instance,"Oops! Wrong entry!" will be displayed.I have not understand what is your goal though

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by std10093 View Post
    If you type "sam" for instance,"Oops! Wrong entry!" will be displayed.I have not understand what is your goal though

    Yes, sam. It will display the error, of course. Because the first character is 's'.

    My point is, I entered ' YYYYYYYYYYYYYYYYYYYYYYYYYY ' , yes the first character is Y. But from what I see, I am not entering the character 'Y' only, but a string "YYYYYYYYYYYYYYYYYYYYYYYYYYY".
    How do I fix that problem?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by xeon321
    For this one, it will work if the user's input is YYYYYYYYYYYYYYYYYYYYY. As long as the first character is Y.
    How to make it display "Oops! Wrong entry!" message?
    You need to define what is correct input and what is wrong input, i.e., std10093's question about your goal.

    Quote Originally Posted by xeon321
    My point is, I entered ' YYYYYYYYYYYYYYYYYYYYYYYYYY ' , yes the first character is Y. But from what I see, I am not entering the character 'Y' only, but a string "YYYYYYYYYYYYYYYYYYYYYYYYYYY".
    How do I fix that problem?
    Why is that a problem?
    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

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by xeon321 View Post
    Hmm interesting example.
    Mind to explain why you use while(1) ? What does the 1 do?
    In order the loop is infinite.Any nuber different that zero gives as a true statement,so the condition of while is satisfied,so our programm is going to enter the loop infinite times.

    For example
    Code:
    int a=5;
    if(a==5)
    {
       printf("entered the loop\n");
    }
    is equivalent to (the message will be dispalyed
    For example
    Code:
    if(1)
    {
        printf("entered the loop\n");
    }

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by laserlight View Post
    You need to define what is correct input and what is wrong input, i.e., std10093's question about your goal.


    Why is that a problem?
    It is a problem because I am seeing that I am not entering only Y but many Y, which may be Y23123u12839u1238u123uu2. A wrong answer.
    Hmm, define what is correct input & wrong input?


    while( example != 'Y'&& example != 'y')

    I thought this line means , except 'Y' & 'y', the rest of the characters are not accepted?
    Last edited by xeon321; 06-28-2012 at 11:00 AM.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by std10093 View Post
    In order the loop is infinite.Any nuber different that zero gives as a true statement,so the condition of while is satisfied,so our programm is going to enter the loop infinite times.

    For example
    Code:
    int a=5;
    if(a==5)
    {
       printf("entered the loop\n");
    }
    Mmm, and from where the computer knows the number is different than zero?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by xeon321
    I thought this line means , except 'Y' & 'y', the rest of the characters are not accepted?
    Yes, but "the rest" refers to the range of characters that are not accepted. It has nothing to do with the characters that you have yet to read. If you want to discard them, then you need to read and ignore them until you read a new line (i.e., use a loop).

    Quote Originally Posted by xeon321
    Mmm, and from where the computer knows the number is different than zero?
    a==5 evaluates to 1, and 1 is obviously not 0.
    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

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by xeon321 View Post
    Hmm, define what is correct input & wrong input?

    while( example != 'Y'&& example != 'y')

    I thought this line means , except 'Y' & 'y', the rest of the characters are not accepted?
    The code that you wrote is going to check only the first letter of the word.If the 1st letter is different than Y or y ,then user inputs another(or the same word).Your code is going to ckeck again only the first letter of the word.

    Quote Originally Posted by xeon321 View Post
    Mmm, and from where the computer knows the number is different than zero?
    a is equal to 5
    so a==5 is a true statement.True statements are equivalent as 1(boolean algebra) and false statements as 0.
    a==6 is a false statement,it is equivalent to zero

  14. #14
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by laserlight View Post
    Yes, but "the rest" refers to the range of characters that are not accepted. It has nothing to do with the characters that you have yet to read. If you want to discard them, then you need to read and ignore them until you read a new line (i.e., use a loop).


    a==5 evaluates to 1, and 1 is obviously not 0.
    So I can't really prevent the user from inputting ridiculous answer and give him an error message?

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Users are free to input anything(most times by mistake they give false inputs ) .Of course you can,by writing suitable code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in using getchar() insted of fflush(stdin)
    By fredsilvester93 in forum C Programming
    Replies: 1
    Last Post: 12-24-2011, 08:43 AM
  2. Reading a character from the keyboard getchar()
    By mnml in forum C Programming
    Replies: 6
    Last Post: 10-21-2011, 09:05 AM
  3. Using getc(stdin) or getchar(c)
    By daedenilus in forum C Programming
    Replies: 11
    Last Post: 11-13-2005, 01:00 AM
  4. Moving a character to stdin?
    By Geolingo in forum C Programming
    Replies: 2
    Last Post: 09-27-2003, 06:37 PM
  5. Replies: 2
    Last Post: 01-10-2002, 07:42 PM