scanf的运行时错误

时间:2015-06-13 18:55:37

标签: c scanf

我是编程新手。我试图实现一个示例程序,但它给了我一个运行时错误。但是height属性是一个浮点类型。

  

格式'%f'需要'float *'类型的参数,但参数2有   输入'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;

}

2 个答案:

答案 0 :(得分:2)

您的scanf()调用存在格式不匹配问题:

  1. scanf("%c", u.name);应为scanf("%s", u.name);
  2. %s扫描字符串,而%c用于扫描字符

    1. scanf("%f", u.height);应为scanf("%f", &u.height);
    2. 注意添加了&。您需要传递float变量的地址。

答案 1 :(得分:-1)

Oppss ..你可能想尝试这个

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;
};