Need help with restaurant homework

So I'm cluelessly lost. I'm not asking for someone to do my homework for me, I just kinda need a pointer in the right direction. The homework assignment I'm working on prompts:

"Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:

Show the customer the different breakfast items offered by the restaurant (You may be as creative as you like....it doesn't have to be breakfast items and you can name your restaurant anything you like).
Allow the customer to select more than one item from the menu and multiple items of the same type (e.g., the customer may desire a Bacon and Egg sandwich, 2 Muffins, and a cup of Coffee).
Calculate and print the bill.
Assume that the restaurant offers the following items (the price of each item is shown to the right of the item)"

And my requirements include:

"A struct menuItemType with two components:
menuItem of type string and menuPrice of type double.
Function getData:
This function loads the data from a text file into the array menuList.
Function showMenu:
This function shows the different items offered by the restaurant and tells the user how to select the items.
Function printCheck:
This function calculates and prints the check. (Note that the billing amount should include a 5% tax.)"

So the part that I'm stuck at is reading in the information. It seems I have everything else fine (I could be horribly wrong though), but I cannot for the life of me figure out how to read in strings from the .txt files and input them into the menu items. Any help would be appreciated.

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
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

//struct for menuItemType
struct menuItemType
{
	string menuItem;
	double menuPrice;
};
void getData(menuItemType menuList[]);
void showMenu(menuItemType menuList[], int x);
void printCheck(menuItemType menuList[], int menuOrder[], int x);

int main()
{

	// Declare functions and variables
	const int menuItems = 8;
	menuItemType menuList[menuItems];
	int menuOrder[menuItems] = {0};
	int orderChoice = 0;
	bool ordering = true;
	int count = 0;
	// Set the menu
	getData(menuList);
	// Order
	showMenu(menuList, menuItems);
	while(ordering) {
		
		cout << "Please enter the number for the item you would\n"
		     << "like to order, or enter [0] to complete your order."
		     << endl;
		cin >> orderChoice;
		if (orderChoice > 0 && orderChoice <= menuItems) {
			
			menuOrder[orderChoice - 1] += 1;
			
		}
		else
			ordering = false;
	}
	printCheck(menuList, menuOrder, menuItems);
	cin.ignore(2);
	return 0;
}

void getData(menuItemType menuList[]) {
	
	string item1, item2, item3, item4, item5, item6, item7, item8;
	
	string menuData[8] = {item1, item2, item3, item4, item5, item6
	                      item7, item8};
	
	ifstream menuText;
	menuText.open("menu.txt");
	if (!menuText) {
		
		cerr << "Unable to open file menuText.txt";
    	exit(1);
    	
	}
	
	for (int i = 0; i <= 8; i++) {
		
		menuText >> menuData[i];
		
	}
	
	menuText.close();
	
	// Declare menuItemTypes
	menuItemType plainEgg;
	menuItemType baconEgg;
	menuItemType muffin;
	menuItemType frenchToast;
	menuItemType fruitBasket;
	menuItemType cereal;
	menuItemType coffee;
	menuItemType tea;

	// Initialize menuItemTypes variables
	plainEgg.menuItem = menuData[0];
	plainEgg.menuPrice = 1.45;
	baconEgg.menuItem = menuData[1];
	baconEgg.menuPrice = 2.45;
	muffin.menuItem = menuData[2];
	muffin.menuPrice = 0.99;
	frenchToast.menuItem = menuData[3];
	frenchToast.menuPrice = 1.99;
	fruitBasket.menuItem = menuData[4];
	fruitBasket.menuPrice = 2.49;
	cereal.menuItem = menuData[5];
	cereal.menuPrice = 0.69;
	coffee.menuItem = menuData[6];
	coffee.menuPrice = 0.50;
	tea.menuItem = menuData[7];
	tea.menuPrice = 0.75;

	menuList[0] = plainEgg;
	menuList[1] = baconEgg;
	menuList[2] = muffin;
	menuList[3] = frenchToast;
	menuList[4] = fruitBasket;
	menuList[5] = cereal;
	menuList[6] = coffee;
	menuList[7] = tea;
	
}

void showMenu(menuItemType menuList[], int x) {
	
	// Function variables
	int count;

	cout << "Welcome to Bob Evans!" << endl;
	cout << "What would you to order?" << endl;
	for(count = 0; count < x; count++) {
		
		cout << setw(1) << left << "[" << count + 1 << "]"
		     << menuList[count].menuItem << "- $"
		     << menuList[count].menuPrice << endl;
		
	}
}

void printCheck(menuItemType menuList[], int menuOrder[], int menuItems) {
	
	double checkTotal = 0;
	double checkTax = 0;
	const double TAX = .05;
	cout << "Thanks for eating at Bob Evans! "
	     << "Customer check: " << endl << endl;
	for(int i = 0; i < menuItems; i++) {
		
		if(menuOrder[i] > 0) {
			cout << setprecision(3) << setw(20) << left << menuList[i].menuItem
			     << " $" << (menuList[i].menuPrice * menuOrder[i]) << endl;
			checkTotal += (menuList[i].menuPrice * menuOrder[i]);
		}
	}
	checkTax = checkTotal * TAX;
	checkTotal += checkTax;
	cout << setw(21) << left << "Tax" << checkTax << endl
	     << setw(21) << left << "Total" << checkTotal << endl;
}


The .txt file:

1
2
3
4
5
6
7
8
Plain Egg
Bacon and Eggs
Muffin
French Toast
Fruit Basket
Cereal
Coffee
Tea
Hello PhoenixBlain,

When reading the file replace the for loop with:
1
2
while (std::getline(inFile, menuData[i]))
	i++;
This will allow you you to read any size file.

In the "printCheck" function you might like this:
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
void printCheck(menuItemType menuList[], int menuOrder[], int menuItems)
{

	double checkTotal = 0;
	double checkTax = 0;
	const double TAX = .05;

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

	cout << "Thanks for eating at Bob Evans! "
		<< "Customer check: " << endl << endl;

	for (int i = 0; i < menuItems; i++)
	{
		if (menuOrder[i] > 0)
		{
			cout << /*setprecision(3) <<*/ setw(20) << left << menuList[i].menuItem
				<< " $" << right << std::setw(5) << (menuList[i].menuPrice * menuOrder[i]) << endl;
			checkTotal += (menuList[i].menuPrice * menuOrder[i]);
		}
	}

	checkTax = checkTotal * TAX;
	checkTotal += checkTax;

	cout << setw(21) << left << "Tax" << '$' << right << std::setw(5) << checkTax << endl
		<< setw(21) << left << "Total" << '$' << right << std::setw(5) << checkTotal << endl;
}
The "showpoint" in line 8 will print ".00" if it shows up.

I also put lin 8 in the "showMenu" function.

Hope that helps,

Andy
Thanks Andy, you were a huge help! Also, I figured out the major problem. I had forgot to put a comma between item6 and item7

1
2
string menuData[8] = {item1, item2, item3, item4, item5, item6
	                      item7, item8};


Very annoyed with myself lol.
Hello PhoenixBlain,

You are welcome.

Your missing comma was a problem, but while I was working on the program I did this:
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
void getData(menuItemType menuList[])
{
	size_t i{};

	//string item1, item2, item3, item4, item5, item6, item7, item8;  // <--- Not needed.

	string menuData[8]; // { item1, item2, item3, item4, item5, item6, item7, item8 };

	ifstream inFile;

	inFile.open("menu.txt");

	if (!inFile)
	{
		cerr << "Unable to open file menuText.txt";
		exit(1);
	}

	while (std::getline(inFile, menuData[i]))
		i++;

//for (int i = 0; i <= 8; i++)
	//{
	//	menuText >> menuData[i];
	//}

	inFile.close();
You do not need line 5 and the uncommented part of line 7 will create an array of eight empty strings. There is no need to initialize line 7.

Hope That helps,

Andy
Topic archived. No new replies allowed.