Thread: help with getchar lol

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Angry help with getchar lol

    can someone tell me what the heck is with getchar? it skips over the getchars and its REALLY annoying. also, should i use scanf to hold screen?? i was using getchar until all the time until i started using it for input aswell, then this error started happening



    i just copy pasted a bunch of headers cause i was lazy and whipped this up fast, dont try running it without looking at the code.



    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>

    int main()
    {
    int bob,fred,joe, hold;
    char c;

    printf("enter bob\n");
    scanf("%d", &bob);
    printf("paused\n");
    getchar(); /*why does it skip?*/
    printf("enter fred\n");
    scanf("%d", &fred);
    printf("paused\n");
    getchar(); /*why does it skip?*/
    printf("enter joe\n");
    scanf("%d", &joe);
    printf("paused\n");
    getchar(); /*why does it skip?*/
    printf("bob %d fred %d joe %d\n",bob,fred,joe);
    getchar(); /*why DOESN'T it skip?*/

    printf("Problem child 2\n");
    c = getchar();
    printf("%c \n", c);
    getchar(); /*why does it skip second getchar*/

    printf("Problem child 3\n");
    scanf("%d", &bob);
    scanf("%d", &fred);
    scanf("%d", &joe);
    printf("bob %d fred %d joe %d\n",bob,fred,joe);
    scanf("%d",hold);

    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The input buffer has data left in it. Try flushing the buffer

    and use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    thanks, ill try that out. also, im really new to this forum, is there anywhere that posts all the html tags or is it just normal hyper text?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    i really hate to be a mindless boob that just copies someones code and thinks nothing of it so, can some please explain how

    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    clears the input buffer? why is ch an integar? does ch serve any real purpose?
    Last edited by Taco Grande; 03-18-2003 at 09:07 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >clears the input buffer?
    It reads characters until there's supposedly nothing left. This code assumes that a newline marks the end of input. In buffered systems taking user input, that assumption is usually correct.

    >why is ch an integar?
    The value of EOF may not be representable by a char.

    >does ch serve any real purpose?
    Yes, it lets you test for both EOF and a newline with only one call to getchar.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    thanks alot, and also, in your sig you should throw in
    /*that, is executable*/ to substitute for "that is the question"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By daghenningsorbo in forum C Programming
    Replies: 9
    Last Post: 12-17-2006, 05:25 AM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  4. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 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