Thread: Need help with simple program that uses printf, scanf, and characters

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Need help with simple program that uses printf, scanf, and characters

    Hello. I am writing a program that prompts the user to enter 5 characters and then prints them in a wedge shaped like ">". This is my code:

    Code:
    #include <stdio.h>
    int main()
    {
    char first, second, third, fourth, fifth;
    
    printf("First character: ");
    scanf("%c", &first);
    printf("Second character: ");
    scanf("%c", &second);
    printf("Third character: ");
    scanf("%c", &third);
    printf("Fourth character: ");
    scanf("%c", &fourth);
    printf("Fifth character: ");
    scanf("%c", &fifth);
    
    printf("%c", first);
    printf("%3c", second);
    printf("%5c", third);
    printf("%3c", fourth);
    printf("%c\n", fifth);
    
    return 0;
    }
    When I compile and run the program, however, after inputting the first character and pressing enter, "Second character: Third character: " prints all at once, and then "Fourth character: Fifth character: " prints all at once, as well. Can someone help me understand why it is not going along one command at a time?

    Thanks,
    Steve
    Last edited by Schank; 10-02-2011 at 07:36 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes. It's a little trick of scanf() ... scanf() leaves unprocessed characters in the input buffer... so the next scanf() sees them...

    Think about what happens when you press in your first character ... you type A <enter> ... the enter key stays in the buffer.

    There is a documented ( look up scanf() in your C Library Docs and/or help files ) trick; you can put a leading space in the formatter string for scanf() and it will ignore whitespace looking for the first visible character...

    Code:
    scanf(" %c", &first); // etc.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Thank you so much! I suspected something along those lines, but the leading space trick is great. Kudos

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Schank View Post
    Thank you so much! I suspected something along those lines, but the leading space trick is great. Kudos
    Now... did you actually look up scanf() and see the full detail of how it works? You will find that very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help. printf() and scanf().
    By HBK in forum C Programming
    Replies: 19
    Last Post: 07-17-2011, 04:04 PM
  2. Help with printf and scanf
    By Teiji in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 11:27 PM
  3. printf() and scanf()
    By homeyg in forum C Programming
    Replies: 1
    Last Post: 02-06-2006, 10:43 PM
  4. Simple Scanf and printf
    By xamlit in forum C Programming
    Replies: 9
    Last Post: 08-31-2005, 05:20 PM
  5. printf and scanf
    By studentc in forum C Programming
    Replies: 3
    Last Post: 06-11-2004, 03:07 PM