Thread: Char variables - more than 1?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Char variables - more than 1?

    Hello, I am trying to write a program to take a character, then take another character and print both of them with a - in between.

    However I can't seem to get the second scanf to work. The first one works fine

    It just skips it and goes straight to the last printf function.

    Code:
    #include <stdio.h>
    char a,b;
    int main(){
    printf("First character: ");
    scanf("%c",&a);
    printf("Second character: ");
    scanf("%c",&b);
    printf("%c, %c",a,b);
    getchar();
    }
    I can't see where my program is going wrong, or could it be the compiler? Any help appreciated.

    thanks
    ollie

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Here is the answer.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2
    Thank you, that helped.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Code:
    #include <stdio.h>
    char a,b;
    int main(){
    printf("First character: ");
    scanf("%c",&a);
    getchar();
    printf("Second character: ");
    scanf("%c",&b);
    printf("%c, %c",a,b);
    getchar();
    }
    Remember, all you need to do is add a funtion.Because there a newline character in the buffer after you entered the first character .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help :(
    By ramenen in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2010, 04:31 PM
  2. Char Help! "Packing " bits to a signle unsigned char
    By xxrexdartxx in forum C Programming
    Replies: 7
    Last Post: 10-11-2009, 04:45 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM