Thread: New student

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    8

    New student

    I wanted the help with the getchar() function.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    There is an FAQ on this on this site.
    Double Helix STL

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    int c;
    
    c = getchar();
    if(c != EOF)
    {
        /* Do something with char.... */
    }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There is one thing to note when mixing getchar() with other input functions. scanf() often leaves extra characters in stdin: for example, if you typed "10\n" and scanf() was looking for a number, it would take the "10" but leave "\n" in the input stream. Then if you used getchar() immediately afterwards to get a character, you'd end up with the newline, which was probably not what you intended.

    If you use scanf() and then getchar(), you can eliminate any characters that scanf() may have left behind like so:
    Code:
    int c;
    
    while((c = getchar()) != '\n' && c != EOF);  /* flush the input stream */
    
    c = getchar();  /* the actual character */
    [edit]
    I wanted the help with the getchar() function.
    Do you still want help? [/edit]
    Last edited by dwks; 05-24-2007 at 03:04 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    What are you trying to do?

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Quote Originally Posted by dwks View Post
    There is one thing to note when mixing getchar() with other input functions. scanf() often leaves extra characters in stdin: for example, if you typed "10\n" and scanf() was looking for a number, it would take the "10" but leave "\n" in the input stream. Then if you used getchar() immediately afterwards to get a character, you'd end up with the newline, which was probably not what you intended.

    If you use scanf() and then getchar(), you can eliminate any characters that scanf() may have left behind like so:
    Code:
    int c;
    
    while((c = getchar()) != '\n' && c != EOF);  /* flush the input stream */
    
    c = getchar();  /* the actual character */
    [edit]
    Do you still want help? [/edit]
    does that work when the input stream is empty?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    No, it doesn't in the sense that it will block for input and accept new input just to throw it away.

    Only call it when you are aware that the input stream should contain at least one excess char.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    but how to solve it when it is unknown wether the stdin buffer is empty or not empty?

    my solution is to set buffering to NULL before getchar(), but I don't think that is a nice solution

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You should really read the FAQ entry on this subject.

    Here. I'll do the work for you :
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    and
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by taisao View Post
    but how to solve it when it is unknown wether the stdin buffer is empty or not empty?
    It requires an OS/compiler specific method.

    Quote Originally Posted by taisao View Post
    my solution is to set buffering to NULL before getchar(), but I don't think that is a nice solution
    I don't believe that's a portable method. You are better off writing an OS/compiler specific method imo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. A few tips please...
    By gems in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 02:28 PM