Menu program with functions

any idea why the compiler says that my const ints are not declared in my code? Or any suggestions on routes to take next to completing the assignment?
The Directions for the assignment are below...

Write a menu‐driven C++ program for food purchases at the baseball stadium’s restaurant. The main
program 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.

Requirements:
1. Functions :
a. Define a void function without parameters to display the food menu and prices
b. Define a void function with 4 parameters (bill, totBill, tax, tip) to display the bill
c. Define a void function with 2 parameters (totBill, amtTendered) to calculate and display
the change due
2. Global variables may not be used. All variables used in functions must be passed by parameters
or declared locally.
3. Program must be menu‐driven
4. Input validation for menu item choice and amount tendered.  
5. Verify that the customer tendered an amount that is equal to or greater than the total bill.
6. Output must be labelled and easy to read as shown in the sample output below. Only display 2
decimal points when displaying Total Amount Due and Change Due.

Sample Output:
Baseball Game Snack Menu
1 – Hamburger    $6.00
2 – Hotdog    $4.50
3 – Peanuts    $3.75
4 – Popcorn    $5.50
5 – Soda    $2.80
6 – Chips    $1.00
7 – Water    $2.00
8 – End order
Enter menu item: 2
Enter menu item: 5
Enter menu item: 6
Enter menu item: 9
Incorrect menu selection, please reenter: 8
Bill = $8.30
Tax = .54
Tip =  $1.66
Total amount due: $10.50
Amount tendered: $10.00
Amount paid is not enough to cover bill, please enter new amount: $15.00
Change due: $4.50


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
//07/08/17
//restaraunt menu
//program calculates food cost


//_______________________________________________________________________________________________Header________________________________

#include <iostream>
#include <iomanip>
using namespace std;
   //prototypes

void noahsMenu();

int main()

//______________________________________________________________________________________________/Header____________________________________________
{
	                                                               
	

	//*************************menu choice # variables*************************
	const int hamburgerPick = 1, hotdogPick = 2, peanutsPick = 3,
		popcornPick = 4, sodaPick = 5, chipsPick = 6,
		waterPick = 7, EndOrderPick = 8;
			
	int pick;



	//**********************// menu price variables******************************
	 
         const double hamburger = 6.00, hotdog = 4.50, peanuts = 3.75,              
		   popcorn   = 5.50, soda   = 2.80, chips   = 1.00  ,    
		   water     = 1.00;
	//**********************************************************************
	// function menu call
		   noahsMenu();
	// choose what you want	   
		   cin >> pick;
		   menuSelection();
	
}

                                                                



																	
																	
																	
//******************************************************************************************************************************************
//*    ***    *    *                      function shows the menu for Noahs restaraunt.                                            ***     *
//******************************************************************************************************************************************

void noahsMenu()
	{
	cout << "Noahs Restaraunt Menu ";
		COUT << "________________________";
	cout << 1 << "Hamburger "     << "--$6.00 " << endl ;
	cout << 2 << "Hotdog "        << "--$4.50 " << endl ;
	cout << 3 << "Peanuts"        << "-- 3.75 " << endl ;
	cout << 4 << "Popcorn "       << "--5.50  " << endl ;
	cout << 5 << "Soda "          << "-- 2.80 " << endl ;
	cout << 6 << "Chips "         << "-- 1.00 " << endl ;
	cout << 7 << "Water"          << "-- 1.00 " << endl ;
	cout << 8 << "End Order " ;

	}
//*********************************************************************************************************************************************
//*                                                                                                                  *
//*********************************************************************************************************************************************








//***********************************************************************************************************************************************
//***********************************************************************************************************************************************
//   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        Menu selection function   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  *                                                                                                                  *
//                                   

int menuSelection()
{

	if (pick > 0 && pick < 8)
		cout << pick;
	else if (pick == 8)
		cout << " Sorry, please choose a valid option.";

	else if (pick = 1)
	{
		price += 6.0
	}
	else if (pick = 2)
	{
		price += 4.5
	}
	else if (pick += 3)
	{
		price += 3.75
	}
	else if (pick += 4)
	{
		price += 5.5
	}
	else if (pick += 5)
	{
		price + = 2.8
	}
	else if (pick += 6)
	{
		price += 1.0
	}
	else if (pick = 7)
	{
		price += 1.0
	}
	else if (pick == 8)
		cout << " Sorry, please choose a valid option.";

}
//                                                                                                                                              *
//***********************************************************************************************************************************************
//***********************************************************************************************************************************************


}
Last edited on
format your code as [code] your code [/code] and this [http://www.cplusplus.com/forum/beginner/20108/] might be better than the if ... else

Look at line 89. What is pick? Where is it defined?
hi TheArk, a tip on how the program can be

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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>  // std::accumulate

//================= CONSTANTS ========================================
// ===================================================================
// constant define prices so that it can be easy to change later
const double HAMBURG_PRICE = 6.0;
const double HOT_DOG_PRICE = 4.5;
const double PEANUTS_PRICE = 3.75;
const double POPCORN_PRICE = 5.5;
const double SODA_PRICE	   = 2.8;
const double CHIPS_PRICE   = 1.0;
const double WATER_PRICE   = 2.0;

const double TAX_RATE    = 0.065;
const double TIP_PERCENT = 0.2;
const char DOLLAR = '$';
// ===================================================================
enum FoodItems {HAMBURGER = 1, HOTDOGS, PEANUTS, POPCORN, SODA, CHIPS, WATER, NONE};

int getInput(int min, int max);
void foodMenu(std::vector<double>&prices);

//void displayBill(std::vector<double> billVec, double &totBill, double tax, double tip);
// billVec is taken from foodMenu i.e prices is passed in here. totalbill, tax and tip calculated 

//void calculateChange(double totalBill, double amtTendered);
// here you get total bill from displayBill i.e totBill. You get amtTendered from user and do the 
// calculations

int main()
{
	std::cout << std::setprecision(2) << std::fixed; // two decimals
	std::vector<double> prices{};
	foodMenu(prices);
	return 0;
}
void foodMenu(std::vector<double>&prices)
{
	std::cout << "\tNoahs Restaraunt Menu\n";
	std::cout << "\t________________________\n";
	std::cout <<'\t'<< 1 << "  Hamburger " << DOLLAR << HAMBURG_PRICE << std::endl;
	std::cout << '\t' << 2 << "  Hotdog    " << DOLLAR << HOT_DOG_PRICE << std::endl;
	std::cout << '\t' << 3 << "  Peanuts   " << DOLLAR << PEANUTS_PRICE << std::endl;
	std::cout << '\t' << 4 << "  Popcorn   " << DOLLAR << POPCORN_PRICE << std::endl;
	std::cout << '\t' << 5 << "  Soda      " << DOLLAR << SODA_PRICE << std::endl;
	std::cout << '\t' << 6 << "  Chips     " << DOLLAR << SODA_PRICE << std::endl;
	std::cout << '\t' << 7 << "  Water     " << DOLLAR << WATER_PRICE << std::endl;
	std::cout << '\t' << 8 << "  End Order \n\n";
		
		// repeat choices till user ends order with 8
	bool again = true;
	do
	{
		std::cout << "\tYour choice: ";
		int choice = getInput(1, 8);
		FoodItems item = static_cast<FoodItems>(choice);

		switch (item)
		{
		case HAMBURGER:
			prices.push_back(HAMBURG_PRICE);
			again = true;
			break;
		case HOTDOGS:
			prices.push_back(HOT_DOG_PRICE);
			again = true;
			break;
		case PEANUTS:
			prices.push_back(PEANUTS_PRICE);
			again = true;
			break;
		case POPCORN:
			prices.push_back(POPCORN_PRICE);
			again = true;
			break;
		case SODA:
			prices.push_back(SODA_PRICE);
			again = true;
			break;
		case CHIPS:
			prices.push_back(CHIPS_PRICE);
			again = true;
			break;
		case WATER:
			prices.push_back(WATER_PRICE);
			again = true;
			break;
		case NONE:
			again = false;
			break;
		default:
			std::cout << "\tOops! Wrong menu choice. Exiting...";
			break;
		}
	} while (again);
}

int getInput(int min, int max)
{
	int value = 0;
	std::cin >> value;
	
	bool badValue = std::cin.fail() == 1;

	while (badValue || value< min || value > max)
	{
		std::cout << "\n\tYou must enter a number that lies between " << min << " and " << max << "\n";
		std::cin.clear();
		std::cin.ignore(250, '\n');
		std::cout << "\tTry again: ";
		std::cin >> value;
		badValue = std::cin.fail() == 1;
	}
	std::cin.get();
	return value;
}


This actually is almost all you need. Do the other two functions to claim authorship
any idea why the compiler says that my const ints are not declared in my code?
They are defined inside main(). No function will see variables/constants that are defined in another function.
I think you need some clarification from the prof:
a. Define a void function without parameters to display the food menu and prices

If this function takes no parameters and you can't have globals, then the menu itself must be local to this function.

If the function returns no value, then the code that inputs the order must be a separate function. Since that function needs to know the prices (at a minimum), then the prices must be duplicated between the two functions. It's usually a better idea to have data in just one place.

Without the restrictions, this would be a good example of how it's often better to store a menu in data instead of code. That makes it easier to change and ultimately, a real system would store the menu in a data file rather than hard coding it in the program. Here's an example that shows the menu as data. I've used a raw array with a sentinel value to indicate the end. A more appropriate method would be an std::array. Also, some people will balk at the use of const char* for the menu names but since they never change, std:string is unnecessary and wasteful.
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
#include <iostream>

struct Item {
    Item(const char*n, double v) :
	name(n), value(v)
    {}
    const char *name;
    double value;
};

// Print the menu and then get the order until the user selects "End Order",
// which is printed at the end of the menu. Return the total order amount.
// Note that in a real menu system you'd return the order itself (a
// collection of item numbers and quantities)
double getOrder(Item menu[])
{
    size_t i;
    double result = 0.0;	// the total of the order
    for (i=0; menu[i].name; ++i) {
	std::cout << i+1 << " - " << menu[i].name
		  << "  " << '$' << menu[i].value << '\n';
    }
    std::cout << i+1 << " - " << "End Order\n";
    
    while (true) {
	unsigned num;
	std::cout << "Enter menu item: " << std::flush;
	std::cin >> num;
	--num;			// convert from user-friendly 1 to i+1
				// to computer friendly 0 to i
	if (num > i) {
	    std::cout << "Invalid selection. Please enter a number between 1 and " << i << '\n';
	} else if (num == i) {
	    break;
	} else {
	    result += menu[num].value;
	}
    }
    return result;
}

// No need to pass total since total == bill+tax+tip
void showBill(double bill, double tax, double tip)
{
    std::cout << "Subtotal : $" << bill << '\n';
    std::cout << "     tax : $" << tax << '\n';
    std::cout << "     tip : $" << tip << '\n';
    std::cout << "   total : $"<< tax+tip+bill << '\n';
}

void changeDue(double totBill, double amtTendered)
{
    std::cout << "Your change is $" << amtTendered - totBill << '\n';
}

double getPmt(double total)
{
    while (true) {
	double amount;
	std::cout << "Enter payment amount: " << std::flush;
	std::cin >> amount;
	if (amount >= total) return amount;
	std::cout << "You must enter at least $" << total << '\n';
    }
}

int
main()
{
    constexpr double taxRate = 0.065; // tax rate on food
    constexpr double tipRate = 0.20;  // tip on pre-tax food bill

    // I'll use an array with a sentinel to indicate the end.
    // std::array would be more appropriate if you've learned about it.
    Item menu[] {
	{ "Hamburger", 6.0 },
	{ "Hot dog", 4.5 },
	{ "Peanuts", 3.75 },
	{ nullptr, 0} // MUST END WITH THIS ENTRY
    };

    std::cout.setf(std::ios::fixed, std::ios::floatfield);
    std::cout.precision(2);

    double subTotal = getOrder(menu);
    double tax = subTotal * taxRate;
    double tip = subTotal * tipRate;

    showBill(subTotal, tax, tip);
    double tendered = getPmt(subTotal+tax+tip);
    changeDue(subTotal+tax+tip, tendered);
}

Hello TheArk,

Just an observation here. Looking at your program you assume that every order will be for one. May not always be the case.

Andy
Topic archived. No new replies allowed.