Like Tree1Likes
  • 1 Post By CommonTater

simple scanf() problem

This is a discussion on simple scanf() problem within the C Programming forums, part of the General Programming Boards category; [tag] Dear Experts- Here is my very simple code where I am trying to input each lettre of user's name ...

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

    simple scanf() problem

    [tag]
    Dear Experts-
    Here is my very simple code where I am trying to input each lettre of user's name and then finally display the name:-

    Code:
    void main()
    {
    char a, b, c;
    clrscr();
    
    printf("Entre the first letter: ");
    scanf("%c", &a);
    printf("\n Entre the sec letter: ");
    scanf("%c", &b);
    printf("\n Entre the third letter: ");
    scanf("%c", &c);
    
    printf("\n Name: %c%c%c", a,b ,c);
    
    getch();
    
    }
    // so when this prog. is executed then it asks for first lettre [first printf statement is executed.]
    // Then after entering the first lettre rather than just asking for the second letter it directly displays the second and third printf() statements.[two printf() statements are being displayed simulteniously, without getting any input]
    // Rather it should display the second printf() first then after getting the second input it should display the third printf() statement.
    //
    // Help please.
    [/tag]

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try putting a space in between the " and the % symbol...

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    2
    Quote Originally Posted by CommonTater View Post
    Try putting a space in between the " and the % symbol...
    Hey Tater;
    thanx buddy, that worked.....but would you be king enough in explaining the reason behind this ?? plzzz.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The leading space tells it to ignore nonprinting characters in the input buffer.

    When you're doing letter by letter (or word by word with %s) the newline character from pressing Enter doesn't get consumed by the %c so it's still in the keyboard buffer when the next scanf() comes up... and it thinks you sent it a blank line. If you add a space first you're telling it to ignore whatever's left in the buffer before waiting for new input.

    Yeah... I know, way less than optimal, but that's how it works.
    *al likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Scanf question for beginner
    By somekid413 in forum C Programming
    Replies: 1
    Last Post: 12-15-2009, 04:56 PM
  2. scanf problem!!!
    By s3ng in forum C Programming
    Replies: 12
    Last Post: 02-24-2006, 05:38 AM
  3. Simple Scanf and printf
    By xamlit in forum C Programming
    Replies: 9
    Last Post: 08-31-2005, 06:20 PM
  4. scanf problem
    By jerrysmith in forum C Programming
    Replies: 1
    Last Post: 11-23-2003, 03:07 PM
  5. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 10:41 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21