Help needed quick

hey would like some help in this error the compiler is giving me.

cannot convert `float*' to `float' for argument `2' to `void calcSalesTax(int, float)'

Here is the code for the program the problem is located at line 31 at least that's what the compiler says.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <ctype.h>

using namespace std;

// Declaring functions.
void calcSalesTax(int,float);
void compSweetDish(float);

main(){
    // Declaring variables of type float because there is a chance that the meal price can be in floating point.
    float totalAmount = 0, salesTax = 0, grandTotal = 0;
    int restartProg = 1, totalCustomers = 0, mealPrice = 0;
    char x; // This is used to store charactar 'Y' or 'N' in it.

    cout << "**** Welcome To The Virtual Restaurant **** \n \n";

    // Starting a while loop and setting the restartProg variable to 1 so that the program keeps running until it is set to 0.
    while(restartProg == 1){
        cout << "Please enter the meal price: ";
        cin >> mealPrice;
        cout << "\n\n";
        
        // Checking for errors in the user input i.e. if input is equal to "0" or in negetive.
        while(mealPrice <= 0){
            cout << "Input cannot be '0' or negetive please re-enter: ";
            cin >> mealPrice;
            cout << "\n";
        }
        
        calcSalesTax(mealPrice,&salesTax); // Calling the function to calculate sales tax on meal price.

        cout << "The meal price is: " << mealPrice << endl << "The sales tax is: " << salesTax << endl; 

        totalAmount = mealPrice + salesTax; // calculating the total amount.

        cout << "---------------------------------\n" << "The total amount is: " << totalAmount << "\n\n"; // displaying the total amount.
        
        compSweetDish(totalAmount); // Calling function to check for the required complement sweet dish based on the total amount and displaying it.

        cout << "\n\n" << "Do you want to process another customer?\n\n" << "Enter 'Y' for yes 'N' for no: "; // Asking if the user wants to exit or process another customer.
        cin >> x;
        cout << "\n\n";
        
        // This is to check for errors such as a wrong entry i.e. something other than Y y or N n.
        while(x != 'N' && x != 'n' && x != 'Y' && x != 'y'){
            cout << "Invalid entry NOTE: Enter either 'Y' 'y' or 'N' 'n': ";
            cin >> x;
            cout << endl;
        }
        if(x == 'N' || x == 'n'){ // if x == n or N the variable restartProg will be set to 0 and the loop will exit in other words the program will end.
            restartProg = 0;
        }
        
        totalCustomers++; // increment by 1 in every iteration.
        grandTotal = grandTotal + totalAmount; // calculating the total amount of all bills.
    }
    
    // Displaying the grand total and total customers.
    cout << endl << "Grand Totals: \n\n"; 
    cout << "Total Customers: " << totalCustomers << "\n" << "Total Amount of all Bills: "<< grandTotal << "\n\n";

    system("PAUSE");
}

// Function to calculate sales tax.
void calcSalesTax(int mealPrice,float *salesTax){ // NOTE: the variable salesTax is call by refference. 
    if(mealPrice > 1000 && mealPrice <= 2000){
        *salesTax = 0.01 * mealPrice;
    }
    else if(mealPrice > 2000){
        *salesTax = 0.02 * mealPrice;
    }
}

// Function to check for the required complement dish based on the total amount.
void compSweetDish(float totalAmount){
    if(totalAmount < 1000){
        cout << "This customer should be served with candies.";
    }
    else if(totalAmount >= 1000 && totalAmount < 2000){
        cout << "This customer should be served with Sweet Bread.";
    }
    else if(totalAmount >= 2000 && totalAmount < 3000){
        cout << "This customer should be served with Pudding.";
    }
    else if(totalAmount >= 3000 && totalAmount < 4000){
        cout << "This customer should be served with Cake.";
    }
    else if(totalAmount > 4000){
        cout << "This customer should be served with Trifle.";
    }
}
Last edited on
Your declaration on line 7 doesn't match the function you're using on line 31. Are you missing a * somewhere on line 7?

(Also, have you learned about C++ references? They could save you a minor amount of typing, and maybe headache).

-Albatross
Last edited on
hmm the problem got fixed when i changed the code at line 7 to

void calcSalesTax(int,float *salesTax);

but i kinda did that at line 67 when i was defining the function why do i have to do that at line 7 also?

EDIT: scratch that the problem did not get fixed lol.
Last edited on
closed account (1vRz3TCk)
side note: main(){ is not standard.
The problem still seems to be there i cant seem to understand what i am doing wrong at line 31 i am sending the address of salestax to the function and at line 67 i have added a * to tell the function that pointer is being sent.
If it's the same error, then... are you sure you saved your file? :/

(Additional side note: you're not #including <cstdlib>, which is needed for system().)

-Albatross
I think the problem with your code is on line 7

void calcSalesTax(int,float);

the function prototype clearly states that the second parameter is a float and not a pointer to a float. However in your function declaration it suddenly is taking in a pointer to a float:

1
2
3
4
5
6
7
8
9
// Function to calculate sales tax.
void calcSalesTax(int mealPrice,float *salesTax){ // NOTE: the variable salesTax is call by refference. 
    if(mealPrice > 1000 && mealPrice <= 2000){
        *salesTax = 0.01 * mealPrice;
    }
    else if(mealPrice > 2000){
        *salesTax = 0.02 * mealPrice;
    }
}


Since that function actually has different parameters than the function prototype your compiler would treat it as overloading instead of matching it with the function protoype you declared earlier. Since the declaration with the pass by reference comes after main and doesn't have a prototype, your code in main wouldn't have any knowledge about it, and would try to match it with the prototype instead. It will then see the second parameter and give you the error cause a pointer to a float isn't the same as a float.

I hope that helps in explaining what is happening and why you're getting that compiler error. =)
Last edited on
nice thanks for the help Dacster13 fixed the problem i forgot to put the * sign at line 7 i.e. void calcSalesTax(int,float*);

geez such a little mistake lol.

and Albatross thanks for telling about cstdlib was trying to figure that out for quite some time.

thanks for the help guys or girls.....
Last edited on
Topic archived. No new replies allowed.