Runtime error with scanf

I am new to programming. I tried to implement the sample program, but it gives me the runtime error.but height is a floating point type.

format '% f expects an argument of type float *, but argument 2 is of type' double

#include<stdio.h>
#include<string.h>

struct user
{
    char name[30];
    float height;
    /*float weight;
    int age;
    char hand[9];
    char position[10];
    char expectation[10];*/
};

struct user get_user_data()
{
    struct user u;
    printf("\nEnter your name: ");
    scanf("%c", u.name);

    printf("\nEnter your height: ");
    scanf("%f", u.height);

    return u;

};
int height_ratings(struct user u)
{
  int heightrt = 0;

    if (u.height > 70)
    {
       heightrt =70/10;

    }
    return heightrt;
};

int main(int argc, char* argv[])
{

    struct  user user1 = get_user_data();

    int heighRate = height_ratings(user1);

    printf("your height is  ", heighRate);

    return 0;

}

      

+3


source to share


2 answers


The scanf () calls have format mismatch issues:

  • scanf("%c", u.name);

    it should be scanf("%s", u.name);

%s

to scan a string whereas %c

used to scan a char.



and

  1. scanf("%f", u.height);

    it should be scanf("%f", &u.height);

Pay attention to the added &

. You need to pass the address of the float variable.

+2


source


Oppss .. you can try this



struct user *get_user_data()
{
   /* you have to use dynamic allocation if you want to return it
      (don't forget to free) */
   struct user *u = (struct user *)malloc(sizeof(struct user));
   printf("\nEnter your name: ");
   /* use %s for string instead of %c */
   scanf("%s", u.name);

   printf("\nEnter your height: ");
   /* don't forget to use & (reference operator) */
   scanf("%f", &u.height);

   return u;
};

      

-1


source







All Articles