Problems with user input functions

Hey guys, I am using scanf() to input values for a structure and it does this weird thing where it will "skip" input lines. I have no idea why this is happening. Here is my code:

printf("Name: ");

I have about 9 input statements just like this one. They are one after another with combination of string and integer inputs. I don't feel like typing them all out. It's supposed to be input for a patient being admitted to a hospital.

Heres my output:

Name: Lawrence Aiello
Date of birth: Date of your last physical:
December 3
Height (in inches): Weight (be honest!):

See how it skips date of birth and height? I need help fixing this I have no idea what to do. Thanks in advance.


  • Probably the format string is wrong. The best thing you can do is just copy the relevant code here and put it between code tags.
  • scanf is somewhat problematic in this type of thing, especially where you are mixing different types of inputs. But the first thing you have to realize is that scanf STOPS on white space. So if you have

    scanf("%s", name); where 'name' is a string and in response you put
    Douglas White

    then name will only be Douglas.
    White would then go to feed other inputs later on.

    You should use fgets instead of scanf. It does NOT stop on white space.
    But it only inputs into a string. Use it like

    fgets(string, 100, stdin);

    This reads chars into 'string' up to a max of 100 characters, or until EOL or EOF (end of line or end of file). If you need integer
    or floating point values from this use
    double x;
    int n;
    n = atoi(string);
    x = atof(string);

    Be sure to #include and for these.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!