Getting Rid of Global Variables

I created this program that outputs a menu for a baseball game, allows the user to choose form the menu, calculates sub-total, tax, tip, and then asks for tender before calculating change. However, my instruction was to use functions throughout, and not use any global variables(meaning a variable that is accessible throughout the program)...but I'm not sure if my code now contains any global variables? I feel as though it does (i.e. bill, amtTendered, totBill)...
If so, how do I go about getting rid of those global variables?
Every time I try to mess with variables, the code will break and say that the variable is undeclared.



Here is my 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* This program is a menu-driven program that will loop allowing the user to select food items from the menu adding each item to the bill, 
terminating when the user selects the “End order” item. The program will 
then calculate and display the total bill with tax (.065) and tip (.20); 
tip is calculated on the pretax bill. The program also needs to collect payment and calculate change due. */

#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 

//Function prototypes
void showMenu();
void showBill(double, double, double, double);
void showChange(double, double);
double bill = 0.0;
int main()
{
	int choice; 

	//constants for the menu choices
	const int HAMBURGER_CHOICE = 1,
			  HOTDOG_CHOICE = 2,
			  PEANUTS_CHOICE = 3,
			  POPCORN_CHOICE = 4, 
			  SODA_CHOICE = 5, 
			  CHIPS_CHOICE = 6, 
			  WATER_CHOICE = 7,
			  QUIT_CHOICE = 8;
	//constants for prices
	const double HAMBURGER = 6.0,
				 HOTDOG = 4.5,
				 PEANUTS = 3.75, 
				 POPCORN = 5.5, 
				 SODA = 2.8, 
				 CHIPS = 1.0, 
				 WATER = 1.0; 
	// set up numeric out put formatting
	cout << fixed << showpoint << setprecision(2);
	showMenu();
	do 
	{
		//Display the menu and get the user's choice. 
		cout << "Enter menu item: ";
		cin >> choice; 
		//Validate the menu selection. 
		while (choice < HAMBURGER_CHOICE || choice > QUIT_CHOICE)
		{
			cout << "Please enter a valid menu chioce: ";
			cin >> choice; 
		}

		//if the user does not want to quit, proceed. 
		if (choice == 1)
		{
			bill += 6.0;
		}
		else if (choice == 2)
		{
			bill += 4.5;
		}
		else if (choice == 3)
		{
			bill += 3.75;
		}
		else if (choice == 4)
		{
			bill += 5.50;
		}
		else if (choice == 5)
		{
			bill +=2.8;
		}
		else if (choice == 6)
		{
			bill += 1.0;
		}
		else if (choice == 7)
		{
			bill += 2.0;
		}
		
	} 

	while (choice != QUIT_CHOICE);
	double amtTendered = 0;
	double totBill = 0;
	double const tax = bill * .065;
	double tip = bill * .20;
	showBill(bill, totBill, tax, tip);
	totBill = bill + tax + tip;

	

	showChange (totBill, amtTendered); 

	system ("pause");
	return 0;
}

//***********************************************************
// Definition of function showMenu which displays the menu. *
//***********************************************************

void showMenu()
{
	cout << "\n\tBaseball Game Snack Menu\n"
		 << "1. Hamburger $6.00\n"
		 << "2. Hotdog $4.50\n"
		 << "3. Peanuts $3.75n"
		 << "4. Popcorn $5.50\n"
		 << "5. Soda $2.80\n"
		 << "6. Chips $1.00\n"
		 << "7. Water $2.00\n"
		 << "8. End order\n";
}

//***********************************************************
// Definition of function showBill which displays the bill. *
//***********************************************************

void showBill(double bill, double totBill, double tax, double tip)
{
	cout << "Bill = $"
		 << bill
		 << "\nTax = "
		 << tax
		 << "\nTip = "
		 << tip
		 << "\n";
	 
}

//*************************************************************************
// Definition of function showChange which displays the total and change. *
//*************************************************************************

void showChange(double totBill, double amtTendered)
{
	cout << "Total amount due: $"
		 << totBill
		 << "\n";
	cout << "Enter amount tendtered: "; 
	cin >> amtTendered; 
	if (amtTendered < totBill) {
		cout << "Amount paid is not enough to cover bill, please enter new amount: ";
		cin >> amtTendered;
	}

	cout << "Change due = $"
		 << (amtTendered - totBill)
		 << "\n";

}
Last edited on
1
2
3
4
5
//Function prototypes
void showMenu();
void showBill(double, double, double, double);
void showChange(double, double);
double bill = 0.0;        // <-- Your only global variable. 


And it's not a function prototype as your comment and grouping seems to suggest.
Topic archived. No new replies allowed.