Object oriented programming menu troubles

Hello all, i am in a programming 2 class and thought i was understanding quite well, but i am getting stuck with this project my professor assigned. I believe i have all the outline work done but am just in a rut. Any help or guidance would be greatly appreciated!

i have an error where subscript is out of range and not sure how to fix it

Create a menu system that allows users to select an item from the below categories:
a. Drink
b. Appetizer
c. Entree
d. Dessert
2. You will use 4 separate input files (one for each category above) where you read in an unknown number of items from each file
a. Each input file will have the item name (string) and the item price (double)
b. Use a generic function to check to see if the file was found
c. User a generic function to get the data from each of the input files
3. Display the subtotal on each menu screen so the customer knows exactly how much is already owed
4. As the customer selects items for purchase, you will need to record the item name, price and the quantity desired inside of variables (vector or array) for output later
5. The customer should be able to remain in a category menu until they are ready to return to the main menu
6. When the customer is finished ordering, display the subtotal, tax, and total on the screen
7. Ask for a tip (make a suggested tip amount with 18%, 20%, 25%), then update the total with the tip amount entered
8. Get payment from the user and display change due to the customer
9. If the customer does not pay enough money, then the program should continue to ask for money until the total is paid in full

input files will have the name and price on the same line
"name" 9.99
"name2" 8.99


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
 #include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <fstream>
#include <sstream>
using namespace std;

struct Item
{
	string name{};
	double price{};
	int qty{};
};

class menu
{
	vector<Item> apps, drinks, entrees, deserts, sold;
	double total = 0.0;
	ifstream appIn, desIn, entIn, drinkIn;

public:
	menu()		//	Constructor
	{	//	Open the files
		fileCheck(appIn, "app.txt");
		fileCheck(desIn, "des.txt");
		fileCheck(drinkIn, "drink.txt");
		fileCheck(entIn, "ent.txt");
		//	Read the data
		getData(appIn, apps);
		getData(drinkIn, drinks);
		getData(entIn, entrees);
		getData(desIn, deserts);
	}

	void getData(ifstream& iFile, vector<Item>& items)
	{
		string line;
		Item temp;

		while (getline(iFile, line))
		{
			stringstream	ss(line);
			ss >> temp.name;
			ss >> temp.price;
			items.push_back(temp);
		}
	}

	void DisplaySubmenu(vector<Item>& items)
	{
		size_t subchoice;

		cout << "0. Exit this submenu." << endl;
		for (size_t i = 0; i < items.size(); i++)
			cout << i + 1 << ".  " << items[i].name << "  " << items[i].price << endl;
		while (true)
		{
			Item	temp;
			cout << "Choice: ";
			cin >> subchoice;
			if (subchoice == 0)
			{
				cout << "Total so far: " << total << endl;
				return;
			}
			if (subchoice > items.size())
			{
				cout << "Invalid choice" << endl;
				continue;
			}
			cout << "How many? ";
			cin >> temp.qty;
			temp.name = items[subchoice].name;
			temp.price = items[subchoice].price * temp.qty;
			sold.push_back(temp);
		}
	}

	void menuPrint()
	{
		int choice = 0;

		system("cls");
		cout << "0. to finish order." << endl;
		cout << "1. Appetizer Menu " << endl;
		cout << "2. Drink Menu " << endl;
		cout << "3. Entree Menu " << endl;
		cout << "4. Dessert Menu " << endl;
		while (1)
		{
			cout << "Enter Selection: ";
			cin >> choice;
			switch (choice)
			{
			case 0: return;

			case 1:	DisplaySubmenu(apps);
				break;

			case 2:	DisplaySubmenu(drinks);
				break;

			case 3:	DisplaySubmenu(entrees);
				break;

			case 4:	DisplaySubmenu(deserts);
				break;

			default: cout << "Invalid choice" << endl;
				break;
			}
		}
	}

	void FinalCheck()
	{
		cout << "Final Check" << endl;
		for (auto item : sold)
			cout << item.name << "  " << item.price << endl;
		cout << "Total: " << total << endl;
	}

	void fileCheck(ifstream& iFile, string text)
	{
		iFile.open(text);
		if (!iFile.is_open())
		{
			cout << text << "file not found" << endl;
			exit(1);
		}

	}
};

int main()
{
	menu tb;

	tb.menuPrint();
	tb.FinalCheck();
	//  tb.CalculateTax();
	//  tb.GetTip();
	//  tb.Get_Payment();
	return 0;
}

Last edited on
That looks like the code I posted a few days ago to "get you over the hump".
Have you made any effort to code CalculateTax(), GetTip(), or Get_Payment() ?

I have an error where subscript is out of range and not sure how to fix it

What is the exact error message"? What line?

You have a problem on line 67. subchoice == items.size() is also out of bounds. Hence the expression on line 67 needs to be:

1
2
3
4
5
			if (subchoice >= items.size()) // Note: >=
			{
				cout << "Invalid choice" << endl;
				continue;
			}
L74/75 - subchoice isn't zero based - it's one based. You need to subtract 1 before using as an index into items which is zero based.

Where are you updating total?
Last edited on
Topic archived. No new replies allowed.