Thread: Need some explanation here...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    Need some explanation here...

    This program is for "Guessing a secret number"

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[])
    {
    char i;
    printf("Guess the secret number!\n");
    while(i!='8'){
    i=getchar();
    getchar();
    printf("You've entered %c\n",i);
    }
    printf("You finally guessed!\n");
    system("PAUSE");
    return 0;
    }


    I understand the whole code except for 1 part.

    i=getchar();
    getchar(); <-why is this written again here?

    Can anyone explain me that part please

    thanks

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I'm assuming it's to eat the newline character ('\n')

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's not the best input method since it will get confused if you input more than once character for your guess. You're better off getting the input as a string and then converting that string to an integer.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    Quote Originally Posted by itsme86 View Post
    It's not the best input method since it will get confused if you input more than once character for your guess. You're better off getting the input as a string and then converting that string to an integer.
    I know that but still...

    I need to know why

    getchar(); is written 2 times =/

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    See memcpy's response (post #2). What do you press after your guess to transmit it to the program? That's right, "enter" or "newline". That puts a newline character ('\n') into the input buffer. That's a total of two characters, your guess and the newline. It's a good idea to get rid of that newline before moving on. One (not very robust) way is to use another call to getchar().

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    Quote Originally Posted by anduril462 View Post
    See memcpy's response (post #2). What do you press after your guess to transmit it to the program? That's right, "enter" or "newline". That puts a newline character ('\n') into the input buffer. That's a total of two characters, your guess and the newline. It's a good idea to get rid of that newline before moving on. One (not very robust) way is to use another call to getchar().
    Thanks a lot for explaining to me it's more clear now to me

  7. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Just a suggestion, even though your program technically works like it's supposed to. Make i an integer, use while (i != 8) for the while condition, and replace the two getchars with scanf("%d", &i); You're only looking for numbers, so int would be the appropriate data type to use.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explanation
    By aromash in forum C Programming
    Replies: 3
    Last Post: 12-04-2010, 04:22 PM
  2. need explanation
    By Ssfccsh in forum C++ Programming
    Replies: 6
    Last Post: 03-01-2004, 01:40 PM
  3. Explanation Please
    By atari400 in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2003, 08:39 AM
  4. Explanation
    By fallonides in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 01:41 PM
  5. Further Explanation
    By sketchit in forum C Programming
    Replies: 1
    Last Post: 09-24-2001, 12:23 PM