pointers not pointing correctly and comparison warnings

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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));
    }


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?
Last edited on
Line 12: getUserInput takes two pointers to ints, but your passing ints. Why not pass these by reference instead? That will solve all of compilation errors.

Line 32: What if the user enters something like "birthday?" You need to check the return value of scanf(). Better yet, use stream input with cin instead of the error-prone scanf().
Make sure you know how a pointer works. A pointer holds an adress of a variable, so when using it to compare things, etc, you should dereference it first by using the dereference operator *
Ex.
1
2
3
int *x,*y;
std::cin >> x >> y;
(*x) > (*y) ? std::cout << *x << " is the highest number." : *y << " is the highest number.";
@dhayden

no pass by reference in c thts why. also, i am not too concerned with validation

@goldenchicken

yes, ive realized i need to dereference the comparisons like that. Thanks!
also, i am not too concerned with validation


Well beware then ....

Prefer scanf_s it's more secure.

http://en.cppreference.com/w/c/io/fscanf

scanf_s returns a value - you should check it to see that it worked. One can use a switch statement to do various things depending on the return value from scanf_s.

If a value can't be negative, make it's type unsigned. It helps because the scanf_s will fail.

With your do loops, notice how you have the condition twice. Change it to a while loop with that condition (once)

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


That's because you are comparing pointers, which are never negative. And the OR condition means the second part is never evaluated because the first part is always true. This is called short circuit evaluation.

Avoid using float, the default type is double - stick with that, otherwise it will bite you on the ass one day. :+)

Good Luck !!
Topic archived. No new replies allowed.