I need to create a gratuity calculator for a class. Here are the instructions:
1 2 3 4 5
Design a Tips class that calculates the gratuity on a restaurant meal. Its only class member variable, taxRate, should be set by a one-parameter constructor to whatever rate is passed to it when a Tips object is created. If no argument is passed, a default tax rate of .065 should be used.
The class should have just one public function, computeTips. This function needs to accept two arguments, the total bill amount and the tip rate. It should use this information to compute what the cost of the meal was before the tax was added. It should then apply the tip rate to just the meal cost portion of the bill to compute and return the tip amount.
Demonstrate the class by creating a program with a single Tips object, then loops multiple times to allow the program user to retrieve the correct tip amount using various bill totals and desired tip rates.
Here's the code that I have so far. I'm missing a lot and don't know what to do.
// Project 6 Gratuity Calculator
// This program utilizes a Tips class to calculate the gratuity on
// a restaurant meal, using whatever tip percent the patron desires.
#include <iostream>
#include <iomanip>
usingnamespace std;
//prototypes go here
class Tips
{
private:
float trate; //variable to hold the tax rate
public:
Tips(float rate) // Constructor that accepts a tax rate
{ // or uses a default value of 6.5%
if (rate >= 0)
taxRate = rate;
else
taxRate = .065;
}
double computeTip(double, double);
};
/***********************************************************
* Tips::computeTip *
* This public class member function computes and returns *
* the correct tip amount based on a restaurant bill and *
* the percentage a patron wishes to tip. *
***********************************************************/
double Tips::computeTip(double totalBill, double tipRate)
{
double mealCost = mealCost;
double tipAmount = tipAmount;
return tipAmount;
}
//********************** Client program *********************
int main()
{
//variables
char yn; // Compute another tip (Y/N)?
float trate; //holds the user input for the tax rate
float tipAmount; //amount of tip to leave
float price; //the total amount of the meal including tax
float tippercent; // % tip the user wants to leave
float mealCost; //amount of the meal before tax
// Input and validate the tax rate
cout << "Enter the tax rate.";
cin >> trate;
// Convert from % to decimal
tippercent = tippercent/100;
// **** Create a Tips object with the correct tax rate ****
Tips tipHelper(taxRate);
// Begin main processing loop
do
{
cout << "\n\n\n************* Tip Helper *********** \n\n";
// Place function call to input and validate the total bill amount
cout << "Enter the total amount of the meal including tax.";
cin >> price;
// Place function call to input and validate the desired tip percentage
cout << "Enter the % tip you want to leave.";
cin >> tippercent;
// Place function call to convert tip rate from percentage to decimal
tippercent;
// Call the Tips class computeTip function to compute the tip amount
tipAmt = tipHelper.computeTip(totalBill, tipRate);
// Place function call to display the result
// Place function call to ask if the user wants to calculate another tip?
cout << "Do you want to compute another tip (Y/N)?";
cin >> yn;
while (yn == 'y' || yn == 'Y');
while (yn != 'n' || 'N');
return 0;
}
At line 62 you create a Tips object using taxRate, but you haven't set taxRate.
At line 81 you compute the tip amount for totalBill and tipRate, but you haven't set totalBill or tipRate.
Lines 35 & 36, sit down with a pencil and paper and work out the formula for computing the meal cost and tip amount. Finally a real use for algebra! :). Once you have the right formula, add it to the code.
// Project 6 Gratuity Calculator
// This program utilizes a Tips class to calculate the gratuity on
// a restaurant meal, using whatever tip percent the patron desires.
#include <iostream>
#include <iomanip>
usingnamespace std;
//prototypes go here
class Tips
{
private:
float taxrate; //variable to hold the tax rate
public:
Tips(float rate) // Constructor that accepts a tax rate
{ // or uses a default value of 6.5%
if (rate >= 0)
taxrate = rate;
else
taxrate = .065;
}
double computeTip(double, double);
};
/***********************************************************
* Tips::computeTip *
* This public class member function computes and returns *
* the correct tip amount based on a restaurant bill and *
* the percentage a patron wishes to tip. *
***********************************************************/
double Tips::computeTip(double totalBill, double tipRate)
{
double mealCost = mealCost;
double tipAmount = tipAmount;
return tipAmount;
}
//********************** Client program *********************
int main()
{
//variables
char yn; // Compute another tip (Y/N)?
float taxrate; //holds the user input for the tax rate
float tipAmount; //amount of tip to leave
float totalbill; //the total amount of the meal including tax
float tippercent; // % tip the user wants to leave
float mealCost; //amount of the meal before tax
float tiprate; //holds the user input for the tip rate
// Input and validate the tax rate
cout << "Enter the tax rate.";
cin >> taxrate;
// Convert from % to decimal
taxrate = taxrate/100;
// **** Create a Tips object with the correct tax rate ****
Tips tipHelper(taxrate);
// Begin main processing loop
do
{
cout << "\n\n\n************* Tip Helper *********** \n\n";
// Place function call to input and validate the total bill amount
cout << "Enter the total amount of the meal including tax.";
cin >> totalbill;
// Place function call to input and validate the desired tip percentage
cout << "Enter the % tip you want to leave.";
cin >> tippercent;
// Place function call to convert tip rate from percentage to decimal
tippercent = tippercent/100;
// Call the Tips class computeTip function to compute the tip amount
tipAmount = tipHelper.computeTip(totalbill, tiprate);
// Place function call to display the result
// Place function call to ask if the user wants to calculate another tip?
cout << "Do you want to compute another tip (Y/N)?";
cin >> yn;
while (yn == 'y' || yn == 'Y');
return 0;
}
// Project 6 Gratuity Calculator
// This program utilizes a Tips class to calculate the gratuity on
// a restaurant meal, using whatever tip percent the patron desires.
#include <iostream>
#include <iomanip>
usingnamespace std;
//prototypes go here
class Tips
{
private:
double taxrate; //variable to hold the tax rate
public:
Tips(double rate) // Constructor that accepts a tax rate
{ // or uses a default value of 6.5%
if (rate >= 0)
taxrate = rate;
else
taxrate = .065;
}
double computeTip(double, double);
};
/***********************************************************
* Tips::computeTip *
* This public class member function computes and returns *
* the correct tip amount based on a restaurant bill and *
* the percentage a patron wishes to tip. *
***********************************************************/
double Tips::computeTip(double totalbill, double tiprate)
{
double mealCost = totalbill - tax;
double tipAmount = mealCost * tiprate;
return tipAmount;
}
//********************** Client program *********************
int main()
{
//variables
char yn; // Compute another tip (Y/N)?
double taxrate; //holds the user input for the tax rate
double tipAmount; //amount of tip to leave
double totalbill; //the total amount of the meal including tax
double tippercent; // % tip the user wants to leave
double mealCost; //amount of the meal before tax
double tiprate; //holds the user input for the tip rate
double tax; //amount of the tax
// Input and validate the tax rate
cout << "Enter the tax rate.";
cin >> taxrate;
// Convert from % to decimal
taxrate = taxrate/100;
// **** Create a Tips object with the correct tax rate ****
Tips tipHelper(taxrate);
// Begin main processing loop
do
{
cout << "\n\n\n************* Tip Helper *********** \n\n";
// Place function call to input and validate the total bill amount
cout << "Enter the total amount of the meal including tax.";
cin >> totalbill;
// Place function call to input and validate the desired tip percentage
cout << "Enter the % tip you want to leave.";
cin >> tippercent;
// Place function call to convert tip rate from percentage to decimal
tippercent = tippercent/100;
// Call the Tips class computeTip function to compute the tip amount
tipAmount = tipHelper.computeTip(totalbill, tippercent);
// Place function call to display the result
// Place function call to ask if the user wants to calculate another tip?
cout << "Do you want to compute another tip (Y/N)?";
cin >> yn;
while (yn == 'y' || yn == 'Y');
return 0;
}
Here are the errors:
1 2 3
1
1> Gratuity Calculator.cpp
1>c:\users\chris\desktop\cop2000 visual c++\programming challenge6\programming challenge6\gratuity calculator.cpp(35): error C2065: 'tax' : undeclared identifier
1>c:\users\chris\desktop\cop2000 visual c++\programming challenge6\programming challenge6\gratuity calculator.cpp(94): fatal error C1075: end of file found before the left brace '{' at 'c:\users\chris\desktop\cop2000 visual c++\programming challenge6\programming challenge6\gratuity calculator.cpp(44)' was matched
// Place function call to ask if the user wants to calculate another tip?
cout << "Do you want to compute another tip (Y/N)?";
cin >> yn;
while (yn == 'y' || yn == 'Y');
return 0;
}
}
and this is the error message I get:
1>------ Build started: Project: Programming Challenge6, Configuration: Debug Win32 ------
1> Gratuity Calculator.cpp
1>c:\users\chris\desktop\cop2000 visual c++\programming challenge6\programming challenge6\gratuity calculator.cpp(95): error C2059: syntax error : '}'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
// Project 6 Gratuity Calculator
// This program utilizes a Tips class to calculate the gratuity on
// a restaurant meal, using whatever tip percent the patron desires.
#include <iostream>
#include <iomanip>
usingnamespace std;
//prototypes go here
class Tips
{
private:
double taxrate; //variable to hold the tax rate
public:
Tips(double rate) // Constructor that accepts a tax rate
{ // or uses a default value of 6.5%
if (rate >= 0)
taxrate = rate;
else
taxrate = .065;
}
double computeTip(double, double, double);
};
/***********************************************************
* Tips::computeTip *
* This public class member function computes and returns *
* the correct tip amount based on a restaurant bill and *
* the percentage a patron wishes to tip. *
***********************************************************/
double Tips::computeTip(double totalbill, double tiprate, double tax)
{
double mealCost = totalbill - tax;
double tipAmount = mealCost * tiprate;
return tipAmount;
}
//********************** Client program *********************
int main()
{
//variables
char yn; // Compute another tip (Y/N)?
double taxrate; //holds the user input for the tax rate
double tipAmount; //amount of tip to leave
double totalbill; //the total amount of the meal including tax
double tippercent; // % tip the user wants to leave
double mealCost; //amount of the meal before tax
double tiprate; //holds the user input for the tip rate
double tax; //amount of the tax
// Input and validate the tax rate
cout << "Enter the tax rate.";
cin >> taxrate;
// Convert from % to decimal
taxrate = taxrate/100;
// **** Create a Tips object with the correct tax rate ****
Tips tipHelper(taxrate);
// Begin main processing loop
do
{
cout << "\n\n\n************* Tip Helper *********** \n\n";
// Place function call to input and validate the total bill amount
cout << "Enter the total amount of the meal including tax.";
cin >> totalbill;
// Place function call to input and validate the desired tip percentage
cout << "Enter the % tip you want to leave.";
cin >> tippercent;
// Place function call to convert tip rate from percentage to decimal
tippercent = tippercent/100;
// Call the Tips class computeTip function to compute the tip amount
tipAmount = tipHelper.computeTip(totalbill, tippercent, tax);
// Place function call to display the result
cout << "Your tip is " << tipAmount << ".";
// Place function call to ask if the user wants to calculate another tip?
cout << "Do you want to compute another tip (Y/N)?";
cin >> yn;
}
while (yn == 'y' || yn == 'Y');
return 0;
}
Ok. I figured it out...I had the wrong formula for double mealCost and a few other things. But it got it to work right. I also had to add a thing to make it only display 2 decimal points.
// Project 6 Gratuity Calculator
// This program utilizes a Tips class to calculate the gratuity on
// a restaurant meal, using whatever tip percent the patron desires.
#include <iostream>
#include <iomanip>
usingnamespace std;
//prototypes go here
class Tips
{
private:
double taxrate; //variable to hold the tax rate
public:
Tips(double rate) // Constructor that accepts a tax rate
{ // or uses a default value of 6.5%
if (rate >= 0)
taxrate = rate;
else
taxrate = .065;
}
double computeTip(double, double);
};
/***********************************************************
* Tips::computeTip *
* This public class member function computes and returns *
* the correct tip amount based on a restaurant bill and *
* the percentage a patron wishes to tip. *
***********************************************************/
double Tips::computeTip(double totalbill, double tiprate)
{
double mealCost = totalbill/ (1 + taxrate);
double tipAmount = mealCost * tiprate;
return tipAmount;
}
//********************** Client program *********************
int main()
{
//variables
char yn; // Compute another tip (Y/N)?
double taxrate; //holds the user input for the tax rate
double tipAmount; //amount of tip to leave
double totalbill; //the total amount of the meal including tax
double tippercent; // % tip the user wants to leave
double mealCost; //amount of the meal before tax
double tiprate; //holds the user input for the tip rate
// Input and validate the tax rate
cout << "Enter the tax rate.";
cin >> taxrate;
// Convert from % to decimal
taxrate = taxrate/100;
// **** Create a Tips object with the correct tax rate ****
Tips tipHelper(taxrate);
// Begin main processing loop
do
{
cout << "\n\n\n************* Tip Helper *********** \n\n";
// Place function call to input and validate the total bill amount
cout << "Enter the total amount of the meal including tax.\n";
cin >> totalbill;
// Place function call to input and validate the desired tip percentage
cout << "Enter the % tip you want to leave.\n";
cin >> tippercent;
// Place function call to convert tip rate from percentage to decimal
tippercent = tippercent/100;
// Call the Tips class computeTip function to compute the tip amount
tipAmount = tipHelper.computeTip(totalbill, tippercent);
// Place function call to display the result
cout << "Your tip is " << setprecision(2) << fixed << tipAmount << ".\n";
// Place function call to ask if the user wants to calculate another tip?
cout << "Do you want to compute another tip (Y/N)?\n";
cin >> yn;
}
while (yn == 'y' || yn == 'Y');
return 0;
}