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

  1. #31
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    If char is signed and EOF is not in the range of signed char, the behaviour is implementation defined. Besides, there is no harm is declaring example to be an int, and the code might be changed to handle EOF anyway.
    This will give our code more flexibility.Good idea

  2. #32
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by laserlight View Post
    Test your version with a string of many characters and see what happens.

    Incidentally, my example can be slightly modified to print an error message as well. The point is, I'm demonstrating the concept, not handing out a code example that is intended for your exact purpose. The same goes for std10093's example (but note that example should be an int, not a char, since getchar() returns an int).

    EDIT:

    I explicitly commented the code. Did you not read the comment?

    Yeah, I tested it with a string of random characters. It will still proceed like the input's all correct.
    What I mean by "difference" is, why his is working while mine not. I don't know the reason within it. Brain's all messed up now ~.~

    So, I should have never used char at all? The correct way is , using int?

    I did, but "Intentionally empty" ? Which means the user only press enter? I kinda got lost in here about "Am I correct about it being as while the user input is not enter, do <add action yourself> "
    I tried adding the do while loop like Subsonics suggested. And I first pressed enter , it does nothing. I have to press the 2nd time in order for the do while loop to comes into play.

    For "while the user input is not enter, do <add action yourself> " , if I change to " while the user input is not Y or y, do <add action yourself> " , it doesn't work again...

    So when you wrote the above piece of code you used getchar once (in case your input was Y313).Then this example != 'Y' && example != 'y' statement is going to be a false statement (as example holds 'Y'),so you will not enter the loop and skip it.


    Hmm, so which means, you know it will be a true statement as getchar reads the first character in my case which is 'Y' and will skip the "Invalid entry" loop. So you play smart and assign 1 to flag, and while the statement is true ( which is actually false ) , it will print the error message? Damn, I like this trick ><

  3. #33
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by laserlight View Post
    If char is signed and EOF is not in the range of signed char, the behaviour is implementation defined. Besides, there is no harm is declaring example to be an int, and the code might be changed to handle EOF anyway.
    Thanks for this! Will change it from now on.

  4. #34
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If EOF handling is required then you must declare your variable as int.
    Else i suggest declaring it as char(if that fits you or int,there is no problem.

  5. #35
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by std10093 View Post
    If EOF handling is required then you must declare your variable as int.
    Else i suggest declaring it as char(if that fits you or int,there is no problem.
    Hmm, it is not required but still, I will stick to int, to prevent any nasty problems in the future

  6. #36
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    That is ok my friend

  7. #37
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by std10093 View Post
    That is ok my friend
    Haha, still have hard time understanding what is laserlight's concept in this line of code...So blur nooooooooow
    Code:
    while((c = getchar()) != '\n') /* intentionally empty */;
    Code:
    #include <stdio.h> int main(void) { int c; while (1) { printf("Y/N? "); c = getchar(); if (c == 'Y' || c == 'y') { break; } else { /* Discard any characters left on the line. */ while ((c = getchar()) != '\n') /* intentionally empty */; } } return 0; }

  8. #38
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It reads up to new line ! If i were you i would run it without this line in order to see why this line is required into our program

  9. #39
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Uhh..............

    Well...It prints Y/N:Y/N: _ without that line...
    with that line , it prints Y/N: _

    x.x

    Guess I should go and get some sleep and figure it out when my brain's clear enough ==

  10. #40
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah, I tested it with a string of random characters. It will still proceed like the input's all correct.
    What I mean by "difference" is, why his is working while mine not. I don't know the reason within it. Brain's all messed up now ~.~
    Well the logic of his is completely different. I mean, his code sets a flag variable to true once the input goes wrong, and doesn't change it after that. Once the loop is broke, he checks the flag variable. If true, he prints the message. So to understand his example, you have to understand how getchar() works: why the input will eventually be '\n' and such.

    I did, but "Intentionally empty" ? Which means the user only press enter? I kinda got lost in here about "Am I correct about it being as while the user input is not enter, do <add action yourself> "
    Uh, all of the work in laserlight's loop is done in the condition part of the loop. To read it properly, you should understand the part in parentheses first, then do the comparisons. Since there is nothing for the loop body to do, it is "intentionally empty". The body is terminated with a semicolon, which is valid syntax. Mind you it is not always good for the person who writes the loop to leave off the braces. This is really a rather special case.

    If EOF handling is required
    EOF handling is always required. Don't get under the impression that EOF is an optional state. Eventually you will reach the end of a file. It might be ugly, but a user could actually choose to not sit through your prompts, design a file with proper inputs, and direct the program to take input from that file. If you don't handle EOF, your code has a problem.

  11. #41
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by whiteflags View Post
    Well the logic of his is completely different. I mean, his code sets a flag variable to true once the input goes wrong, and doesn't change it after that. Once the loop is broke, he checks the flag variable. If true, he prints the message. So to understand his example, you have to understand how getchar() works: why the input will eventually be '\n' and such.


    Uh, all of the work in laserlight's loop is done in the condition part of the loop. To read it properly, you should understand the part in parentheses first, then do the comparisons. Since there is nothing for the loop body to do, it is "intentionally empty". The body is terminated with a semicolon, which is valid syntax. Mind you it is not always good for the person who writes the loop to leave off the braces. This is really a rather special case.


    EOF handling is always required. Don't get under the impression that EOF is an optional state. Eventually you will reach the end of a file. It might be ugly, but a user could actually choose to not sit through your prompts, design a file with proper inputs, and direct the program to take input from that file. If you don't handle EOF, your code has a problem.

    Ah...got what you mean there...........Thanks mate . . .

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