gratuity calculator

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.
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
94
// 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>
using namespace 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.
Thanks! I saw that I just had to define some of the variables that was missing. But I think I still need to do some kind of 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
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
// 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>
using namespace 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;
}
I found a few more mistakes that I fixed. I'm still getting an error about the closing brace.
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
// 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>
using namespace 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
1: You have to add a 3rd parameter tax to double Tips::computeTip(double totalbill, double tip rate) and pass this in main

2: Add the closing bracket for main()
When I add another bracket at the end, it doesn't do anything and gives me the same error.
This is the end of the code:
1
2
3
4
5
6
7
8
	// 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 ==========
My mistake. It's the closing '}' for the do while loop instead of main
Where does do while loop end? It looks like they run into each other.
Alright....I fixed the bracket error, but upon doing that I got 3 more errors....
1>c:\users\chris\desktop\cop2000 visual c++\programming challenge6\programming challenge6\gratuity calculator.cpp(53): warning C4101: 'mealCost' : unreferenced local variable
1>c:\users\chris\desktop\cop2000 visual c++\programming challenge6\programming challenge6\gratuity calculator.cpp(54): warning C4101: 'tiprate' : unreferenced local variable
1>c:\users\chris\desktop\cop2000 visual c++\programming challenge6\programming challenge6\gratuity calculator.cpp(83): warning C4700: uninitialized local variable 'tax' used
1> Programming Challenge6.vcxproj -> C:\Users\Chris\Desktop\COP2000 Visual C++\Programming Challenge6\Debug\Programming Challenge6.exe


Here's 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
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
94
95
96
97
// 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>
using namespace 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.
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
94
95
96
// 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>
using namespace 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;

	}
Topic archived. No new replies allowed.