Thread: Beginner practice, scanf

  1. #1
    Registered User
    Join Date
    Apr 2022
    Posts
    4

    Beginner practice, scanf

    Hello,

    I cannot figure out why, whenever I use the scanf function more than once, it never "executes" more than once. I will have three questions that require input from the user, but only the first question allows for input from the keyboard. Once I hit enter, the rest of the code just executes with me not able to input anything. Can someone tell me why?

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        char name[20];
        int DOB;
        int phoneNumber;
        printf("Please tell us your name: ");
        scanf("%s", name);
    
    
        printf("When were you born? ");
        scanf("%d", &DOB);
    
    
        printf("What is your phone number? ");
        scanf("%d", &phoneNumber);
    
    
        printf("Here's your information:\n Name: %s\n Date of Birth: %d\n Phone Number: %d", name, DOB, phoneNumber);
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Are you considering the newline character?

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Code:
    scanf("%s", name);
    If the user inputs "John Doe", then name would only contain "John". '%s' as is, only inputs the first word, not the entire string. Then the other scanf() calls will fail as the remainder of the input buffer do not match the format strings for numerical strings.

    fgets() would be preferred to input text strings. Yes, an alternative version of the format string in scanf() could be used but fgets() is simpler, although it does also bring in the newline that needs to be dealt with.

    Phone numbers should be input as a text string, and input using fgets(), not as a number because of the many styles of phone numbers. "1234567890" "(123) 456-7890", ...

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by G4143 View Post
    Are you considering the newline character?
    It won't be the newline after the initial string since the %d format will skip leading whitespace. Instead he's probably entering two (or more) space-separated "words" for the %s, which will only read the first word leaving the second word for the %d formats to choke on.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by rstanley View Post
    Phone numbers should be input as a text string, and input using fgets(), not as a number because of the many styles of phone numbers. "1234567890" "(123) 456-7890", ...
    Plus a plain numeric-only phone number might not even fit in an int, such as one in the 408 area code (4081234567). With 32-bit int, that may get stored as a large negative number (4081234567 may be stored/interpreted as -213732729) (though it's probably actually undefined behavior to try storing a larger value into an int than it will hold, so I'd avoid doing that and instead read/store it as a string).

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by christop View Post
    Plus a plain numeric-only phone number might not even fit in an int, such as one in the 408 area code (4081234567). With 32-bit int, that may get stored as a large negative number (4081234567 may be stored/interpreted as -213732729) (though it's probably actually undefined behavior to try storing a larger value into an int than it will hold, so I'd avoid doing that and instead read/store it as a string).
    It's a moot point as no phone number is ever a simple numerical number, in any integer type!

    Please see: "National conventions for writing telephone numbers" for more information. It should always be input as a string , then processed as needed.

  7. #7
    Registered User
    Join Date
    Apr 2022
    Posts
    4
    Hello,

    So are you saying that when the printf prompt comes up, asking me my name and I type in John Doe and hit Enter, the computer is essentially doing the following:

    %s = John
    1st %d = ' ' (a blank space)
    2nd %d = Doe

    and therefore nothing else in the code executes because it already has the input from my first entry it just doesn't make any sense?

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Arzakon12589 View Post
    Hello,

    So are you saying that when the printf prompt comes up, asking me my name and I type in John Doe and hit Enter, the computer is essentially doing the following:

    %s = John
    1st %d = ' ' (a blank space)
    2nd %d = Doe

    and therefore nothing else in the code executes because it already has the input from my first entry it just doesn't make any sense?
    First of all, you do not initialize the local variables, "name", "DOB", and "phoneNumber". They contain whatever garbage values are in the variables when the program starts.

    Initialize ALL local variables!

    Yes, the first scanf() inputs one word, John.

    The second scanf() ignores the space and attempts to convert "Doe" to an int, and fails, leaving the text in the input buffer, and assigns nothing to DOB, and leaves whatever garbage value was in DOB when called.

    The third scanf() also attempts to convert "Doe" to an int and fails, as with the second scanf().

    Within a while loop, if you capture the return value from scanf(), you will see when the scanf() fails. You can clear the input buffer, but NOT with fflush(stdin)!!! fflush() is only for use with output buffers. You then can prompt the user for the value again, until you get the correct value.

    You need to study a good up to date book on the C Programming Language! Please see my other post, here.

    You need to read the scanf manpage for more information, or run "man scanf" on a Linux system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with scanf (I'm a beginner)
    By Leandroafm21 in forum C Programming
    Replies: 3
    Last Post: 03-23-2015, 07:10 PM
  2. Beginner scanf
    By mutleymatt in forum C Programming
    Replies: 1
    Last Post: 06-11-2012, 12:41 PM
  3. Beginner and scanf
    By Chleborad in forum C Programming
    Replies: 12
    Last Post: 02-17-2011, 01:11 AM
  4. Simple Scanf question for beginner
    By somekid413 in forum C Programming
    Replies: 1
    Last Post: 12-15-2009, 04:56 PM

Tags for this Thread