警告:格式'%d'需要类型'int *'的参数,但参数2的类型为'int'

时间:2018-03-27 00:18:49

标签: c

这是我进入C类的,我无法弄清楚为什么我会收到这个错误:

struct vitalInformation {
    float temperature;
    unsigned int systolicPressure;
    unsigned int diastolicPressure;
};

struct activityInformation {
    unsigned int stepCount;
    unsigned int sleepHours;
};

union patientHealth{
    struct vitalInformation vi;
    struct activityInformation ai;
} ph[100]; 

int i = 0;

int menu(){
    int option;

    printf ("Please enter the number for the desired action (1, 2, 3):\n");
    printf ("1 - Enter some patient vital information\n");
    printf ("2 - Enter some patient activity information\n");
    printf ("3 - Print summary information on the patient information and exit the program\n");

    scanf ("%d", &option);

    while (scanf("%d", &option) || option<1 || option>3) {
        printf ("Please enter 1, 2, or 3\n\n");
        printf ("Please enter the number for the desired action (1, 2, 3):\n");
        printf ("1 - Enter some patient vital information\n");
        printf ("2 - Enter some patient activity information\n)");
        printf ("3 - Print summary information on the patient information and exit the program\n");

        fflush (stdin);
        scanf ("%d", &option);

    }

    return option;
}

void patientVitalInfo(int *countP, float *minT, float *maxT, int *minS, int *maxS, int *minD, int *maxD) {
    printf ("Enter the temperature: ");
    scanf ("%f", &ph[i].vi.temperature);

    while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
        printf ("Please enter an integral unsigned number\n");
        printf ("Enter the temperature: ");

        fflush (stdin);
        scanf ("%f", &ph[i].vi.temperature);
    }
}

代码:

{{1}}

1 个答案:

答案 0 :(得分:2)

报告的错误来自您的行

while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {

ph[i].vi.temperature(在本例中为float)的任何内容转换为int,而scanf需要指向int的指针}。

现在,在您的情况下,您似乎需要温度为int,而 ph[i].vi.temperature 持有float,所以你宁愿使用另一个int变量,比如说

int itemp;
scanf ("%d", &itemp);

输入然后

ph[i].vi.temperature = (float) itemp;

用于铸造。 或者,您可以简单地scanf ("%f", &ph[i].vi.temperature);然后保留不可或缺的部分。 我不知道你的需求,也不知道代码背后的逻辑。

注意:我不确定您是否以符合您需求的方式使用scanf的返回值。 在您的情况下,scanf可以返回01EOF