#include <iostream>
#include <ctype.h>
usingnamespace 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;
}
elseif(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.";
}
elseif(totalAmount >= 1000 && totalAmount < 2000){
cout << "This customer should be served with Sweet Bread.";
}
elseif(totalAmount >= 2000 && totalAmount < 3000){
cout << "This customer should be served with Pudding.";
}
elseif(totalAmount >= 3000 && totalAmount < 4000){
cout << "This customer should be served with Cake.";
}
elseif(totalAmount > 4000){
cout << "This customer should be served with Trifle.";
}
}
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.
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;
}
elseif(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. =)