没有正确指向的指针和比较警告

时间:2017-04-04 17:31:31

标签: c

以下简短程序要求用户输入医院病房数量(1-5之间),并询问鲜花数量,不能为负数。

然后,它根据hospitalRoomsPrices_Array[5]的价格选择多少房间,并且还显示总花数乘以每个2.50美元。

最后,增加了房间价格+花费的总成本

继承人代码:

#include<stdio.h>    

void getUserInput(int *numHospitalRooms, int *numFlowers);

int main()
{
    float hospitalRoomsPrices_Array[5]={300.00,350.00,400.00,450.00,500.00};
    int numHospitalRooms = 0;
    int numFlowers = 0;
    float flowerPricing = 2.50;

    getUserInput(numHospitalRooms, numFlowers);

    float flowerCost = numFlowers*flowerPricing;

    float totalCost = (flowerCost + hospitalRoomsPrices_Array[numHospitalRooms]);

    //

    printf("\nCost for %d room(s): $%.2f", numHospitalRooms, hospitalRoomsPrices_Array[numHospitalRooms]);
    printf("\nFlower(s) Cost: $%.2f \n", flowerCost);

    printf("\nTotal cost:  $%.2f", totalCost);

    return 0;
}

void getUserInput(int *numHospitalRooms, int *numFlowers)
{
    do {
        printf("\nHow many hospital rooms: ");
        scanf("%d", &numHospitalRooms);
        if (numHospitalRooms < 1 || numHospitalRooms > 5)
        {
            printf("\nInvalid number of rooms, room number must be between 1-5!\n");
        }

    }while((numHospitalRooms < 1 || numHospitalRooms > 5));

    do {
        printf("\nEnter number of flowers: ");
        scanf("%d", &numFlowers);

        if (numFlowers < 0)
        {
            printf("\nInvalid number of flowers, negative values are not accepted!\n");
        }

    }while((numFlowers < 0));
}

我编译时会收到很多警告:

  • [警告]传递'getUserInput'的参数1使得指针来自 没有强制转换的整数

    [注意]预期'int *'但参数的类型为'int'

    [警告]传递'getUserInput'的参数2使得指针来自 没有强制转换的整数

    [警告]指针和整数之间的比较

不仅如此,在getUserInput()条件陈述中根本没有考虑输入负数。

这是一个输出示例:

  

多少个病房:6个

     

无效的房间数,房间号必须在1-5之间!

     

多少个病房:-1

     

无效的房间数,房间号必须在1-5之间!

     

多少个病房:5个

     

输入鲜花数:-1

     

0个房间的成本:$ 300.00花费用:$ 0.00

     

总费用:$ 300.00

我错过了什么?为什么这样的警告会出现并搞乱程序?

1 个答案:

答案 0 :(得分:0)

经过深思熟虑后,我终于明白了:

#include<stdio.h>

void getUserInput(int *numHospitalRooms, int *numFlowers);

int main()
{
    float hospitalRoomsPrices_Array[5]={300.00,350.00,400.00,450.00,500.00};
    int numHospitalRooms = 0;
    int numFlowers = 0;
    float flowerPricing = 2.50;

    getUserInput(&numHospitalRooms, &numFlowers);

    float flowerCost = numFlowers*flowerPricing;

    float totalCost = (flowerCost + hospitalRoomsPrices_Array[numHospitalRooms - 1]);

    printf("\nCost for %d room(s): $%.2f", numHospitalRooms, hospitalRoomsPrices_Array[numHospitalRooms - 1]);
    printf("\nFlower(s) Cost: $%.2f \n", flowerCost);

    printf("\nTotal cost:  $%.2f", totalCost);

    return 0;
}

void getUserInput(int *numHospitalRooms, int *numFlowers)
{
    do {
        printf("\nHow many hospital rooms: ");
        scanf("%d", numHospitalRooms);
        if (*numHospitalRooms < 1 || *numHospitalRooms > 5)
        {
            printf("\nInvalid number of rooms, room number must be between 1-5!\n");
        }
    }while((*numHospitalRooms < 1 || *numHospitalRooms > 5));

    do {
        printf("\nEnter number of flowers: ");
        scanf("%d", numFlowers);
        if (*numFlowers < 0)
        {
            printf("\nInvalid number of flowers, negative values are not accepted!\n");
        }
    }while((*numFlowers < 0));
}

这应该是这样的:getUserInput(&numHospitalRooms, &numFlowers);

这应该是这样的:

scanf("%d", numHospitalRooms); //no appresand

对于任何人都可以从这里指出的这么简单的错误,没有必要来回反复。作为一个想要学习的学生,这对于学习我的错误而斗争是非常令人沮丧的。像我现在发布的简单的两行代码推荐会在这里照亮一个光明的一天,而不是负面的氛围。