0

the following short program asks user to enter # of hospital rooms (between 1-5), and asks for number of flowers, which cannot be a negative number.

Then, it picks based on how many rooms the price from the hospitalRoomsPrices_Array[5], and it also displays the total flowers multiplied by $2.50 each.

finally, total cost of room(s) price + flower costs is added

heres the code:

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

i am getting a lot of warnings when i compile:

  • [Warning] passing argument 1 of 'getUserInput' makes pointer from
    integer without a cast

    [Note] expected 'int *' but argument is of type 'int'

    [Warning] passing argument 2 of 'getUserInput' makes pointer from
    integer without a cast

    [Warning] comparison between pointer and integer

not only that, but entering a negative number isnt being taken into account at all in the getUserInput() conditional statements.

here is an example of the output:

How many hospital rooms: 6

Invalid number of rooms, room number must be between 1-5!

How many hospital rooms: -1

Invalid number of rooms, room number must be between 1-5!

How many hospital rooms: 5

Enter number of flowers: -1

Cost for 0 room(s): $300.00 Flower(s) Cost: $0.00

Total cost: $300.00

what am i missing? why are the warnings coming up like that and messing with the program?

Edited by happygeek: moved

2
Contributors
3
Replies
29
Views
6 Months
Discussion Span
Last Post by Reverend Jim
0

Your declaration is

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

but you are calling it with

getUserInput(numHospitalRooms, numFlowers);

You define the parameters as pointer to int but you are passing int. You'' have to change your call to

getUserInput(&numHospitalRooms, &numFlowers);

Likewise, within getUserInput, you will have to change all variable references to reflect the fact that the parameters are pointers. Thus instead of

if (numHospitalRooms < 1 || numHospitalRooms > 5)

you'll have to use

if (*numHospitalRooms < 1 || *numHospitalRooms > 5)
0

@Reverend Jim

That helped alleviate the warnings, i had to comment out the loops for now because i think it was going to infinite loop so it was crashing. regardless, even though the warnings are eliminated and no errors, the output is still not taking the user input at all. its like the pointer is disconnected...

How many hospital rooms: 2

Enter number of flowers: 3

Cost for 0 room(s): $300.00
Flower(s) Cost: $0.00

Total cost: $300.00

1

RE: PM. Your input routine should look like

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

Compare the placement of the * and & operators with your original code. You'll also have to make sure that you account for the fact that numHospitalRooms ranges from 1-5 but the array index must be from 0-4. Your calc would then be

float totalCost = (flowerCost + hospitalRoomsPrices_Array[numHospitalRooms] - 1);
Votes + Comments
OMG you are a life saver!!! thanks a lot! i suspected & in scanf but i only used it because thats how syntax should be as i learned for c
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.