Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problems with user input functions

FuzedmindFuzedmind Posts: 1Member
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: ");
scanf("%s,new_patient.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.

Comments

  • anthrax11anthrax11 Posts: 511Member
    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.
  • freedom_docfreedom_doc Posts: 17Member
    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);
    or
    x = atof(string);

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