Thread: scanf getchar

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    11

    scanf getchar

    Code:
    char input = getchar();
     printf("%c",&input);

    Code:
    char input = 0;
    scanf("%c",&input);
     printf("%c",&input);
    how come i dont get the same output when
    i enter the input? im trying to get one 1 char.
    i come from c++

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    Code:
    char input = 0;
    while(1)
    {
     printMap(Level1);
     input = getchar();
     switch(input)
     {
     case '1': case 1:
      printf("You entered 1\n");
      break;
     default:
      printf("No.\n");
      break;
     }
    }
    i enter one and it prints out
    both 1 and default

    whats the deal here, c++ made it simple with cin
    Last edited by hit; 08-18-2012 at 10:42 PM.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This is another one of those "if I had a dollar for every time it was asked" questions. When you enter the number 1 and press enter, the getchar reads the 1 but leaves the newline behind. Your loop goes around and the getchar reads the newline the next time, leading to the default case.

    And technically, getchar returns an int, not a char, but that's not the cause of your problem.

    In your first post you shouldn't be using an ampersand in front of input in the printf function. That will pass the address of input to printf, which then tries to print that as a char.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    thanks for your help.

    Code:
    int main()
    {
        struct Map Level1 = MapInit();
        struct Creature User = {100,20,2,"User",0,0};
        char input = 0;
    
        while(1)
        {
            printMap(Level1);
            scanf("%c",&input); //or input = getchar();
            move(input,&User,&Level1);
        }
        return 0;
    }
    how do i get it to stop reading the null character etc, next time around?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    One solution would be to add one more "getchar()" after receiving the user input to eat up that newline floating in the input buffer.

    You can also put a space before the %c in the "scanf()" call.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Why are you put and ampersand in front of your variable in the printf statement? Remove that, and your problems will be resolved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on getchar to flush scanf
    By lotrfan in forum C Programming
    Replies: 1
    Last Post: 03-15-2012, 04:59 PM
  2. getchar and scanf
    By begin in forum C Programming
    Replies: 9
    Last Post: 06-20-2010, 07:51 PM
  3. getchar won't take any input after scanf
    By pshirishreddy in forum C Programming
    Replies: 2
    Last Post: 08-02-2009, 11:46 AM
  4. scanf, getchar, gets, etc. are ignored.
    By daltore in forum C Programming
    Replies: 19
    Last Post: 12-27-2007, 10:47 PM
  5. scanf and getchar
    By axe in forum C Programming
    Replies: 7
    Last Post: 01-11-2006, 04:45 AM