Menu project using arrays

Hello all,
I have been working on this project for some time where i take items from text files and output them to the console as if it were an interactive restaurant menu, and i have made some progress. However, I am stuck on trying to figure out how to store the items that they choose from each different menu into an array to store the item and quantity of what they choose. I need to store the item and quantity so that at the end the price can be added, any tips 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

// Global variable declarations

string mainMenu;
const double salesTax = 0.1; // This restaurant has a 10% sales tax

// Parallel arrays to store menu items and prices
string drinks[4], appetizers[4], entrees[5], desserts[3];
double drinkPrices[4], appetizerPrices[4], entreePrices[5], dessertPrices[3];

// Make more parallel arrays below this comment to store a customers order

string drinkOrder[4], appOrder[4], entreeOrder[5], dessertOrder[3];



const string WHITESPACE = " \n\r\t\f\v|";

string ltrim(const string& s) {
    size_t start = s.find_first_not_of(WHITESPACE);
    return (start == string::npos) ? "" : s.substr(start);
}

string rtrim(const string& s) {
    size_t end = s.find_last_not_of(WHITESPACE);
    return (end == string::npos) ? "" : s.substr(0, end + 1);
}

string trim(const string& s) {
    return rtrim(ltrim(s));
}

void readMenu(string[]);

// Will print a specific menu when called
void printMenu(int);

void makeOrder();

void makePayment();

int main() {
    // Each file name for the menus
    string fNames[] = { "Main.txt", "Drinks.txt", "Appetizers.txt", "Entrees.txt", "Desserts.txt" };

    readMenu(fNames); // Read from the files and store info into appropriate arrays
    makeOrder();  // Have the user make an order
    makePayment(); // Have the user pay for their meal

    return 0;
}

void readMenu(string fileNames[]) {
    int counter = 0;
    ifstream iFile;

    for (int i = 0; i < 5; ++i) {
        iFile.open(fileNames[i]); // Open the file

        if (i == 0) {
            // Read entire Main.txt file and store contents in the mainMenu string
            mainMenu.assign(istreambuf_iterator<char>(iFile), istreambuf_iterator<char>());
        }
        else {
            iFile.ignore(256, '\n');
            iFile.ignore(256, '\n');

            while (!iFile.eof()) {
                if (i == 1) {
                    // Assign values to drinks
                    getline(iFile, drinks[counter], '$');
                    drinks[counter] = trim(drinks[counter]);
                    iFile >> drinkPrices[counter];
                }
                else if (i == 2) {
                    // Assign values to appetizers
                    getline(iFile, appetizers[counter], '$');
                    appetizers[counter] = trim(appetizers[counter]);
                    iFile >> appetizerPrices[counter];
                }
                else if (i == 3) {
                    // Assign values to entrees
                    getline(iFile, entrees[counter], '$');
                    entrees[counter] = trim(entrees[counter]);
                    iFile >> entreePrices[counter];
                }
                else if (i == 4) {
                    // Assign values to desserts
                    getline(iFile, desserts[counter], '$');
                    desserts[counter] = trim(desserts[counter]);
                    iFile >> dessertPrices[counter];
                }

                iFile.ignore(256, '\n');
                ++counter;
            }

            counter = 0;
        }

        iFile.close();
    }
}

void printMenu(int menu) {
    switch (menu) {
    case 1:
        cout << "Drinks Menu" << endl;
        cout << "Please choose an item from the list below." << endl << endl;
        cout << "0. Back to Main Menu" << endl;
        for (int i = 0; i < 4; ++i) {
            cout << i + 1 << ". ";
            cout << left << setw(25) << fixed << setprecision(2);
            cout << drinks[i] << "$" << drinkPrices[i] << endl;
        }
        cout << endl;
        break;
    case 2:
        cout << "Appetizers Menu" << endl;
        cout << "Please choose an item from the list below." << endl << endl;
        cout << "0. Back to Main Menu" << endl;
        for (int i = 0; i < 3; ++i) {
            cout << i + 1 << ". ";
            cout << left << setw(25) << fixed << setprecision(2);
            cout << appetizers[i] << "$" << appetizerPrices[i] << endl;
        }
        cout << endl;
        break;
    case 3:
        cout << "Entrees Menu" << endl;
        cout << "Please choose an item from the list below." << endl << endl;
        cout << "0. Back to Main Menu" << endl;
        for (int i = 0; i < 5; ++i) {
            cout << i + 1 << ". ";
            cout << left << setw(25) << fixed << setprecision(2);
            cout << entrees[i] << "$" << entreePrices[i] << endl;
        }
        cout << endl;
        break;
    case 4:
        cout << "Desserts Menu" << endl;
        cout << "Please choose an item from the list below." << endl << endl;
        cout << "0. Back to Main Menu" << endl;
        for (int i = 0; i < 3; ++i) {
            cout << i + 1 << ". ";
            cout << left << setw(25) << fixed << setprecision(2);
            cout << desserts[i] << "$" << dessertPrices[i] << endl;
        }
        cout << endl;
        break;
    }
}

void makeOrder() {
    enum menuType { MAIN, DRINK, APPETIZER, ENTREE, DESSERT };

    bool done = false;
    int selection = -1;
    int quantity;
    menuType curMenu = MAIN;

    while (!done) {

        if (curMenu == MAIN) {
            cout << mainMenu << endl;
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                done = true;
                break;
            case 1:
                curMenu = DRINK;
                break;
            case 2:
                curMenu = APPETIZER;
                break;
            case 3:
                curMenu = ENTREE;
                break;
            case 4:
                curMenu = DESSERT;
                break;
            }
        }
        else if (curMenu == DRINK) {
            printMenu(1);
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                curMenu = MAIN;
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            }
        }
        else if (curMenu == APPETIZER) {
            printMenu(2);
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                curMenu = MAIN;
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            }
        }
        else if (curMenu == ENTREE) {
            printMenu(3);
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                curMenu = MAIN;
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            }
        }
        else if (curMenu == DESSERT) {
            printMenu(4);
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                curMenu = MAIN;
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            }
        }

        system("cls");
    }
}

void makePayment() {

}
Last edited on
Hello ccorkran,

I really would like to test the program, but without the input files it can not be done. Any files that I make may not match what you are using or contain enough information, so It is kind of pointless to try.

Your global variables that start with "const" are OK, but the rest are not. You are just asking for trouble because any line of code that follows can change those global variables. After you have spent several hours or a day + trying to track down the problem you will understand. On the up side you will have to learn how to use the IDE to track down where the problem is.

Your non "const" global variables should be defined in "main" and passed to the function(s) that need them.

In your read function while (!iFile.eof()) does not work the way that you think it does. You are checking for "eof" before you read the file that might be the read that sets "eof". Even though you can not read anything from the file you will still process as if you did.

It is better to use: while (getline(iFile, drinks[counter], '$')). This is just an example. The way your if/else if statements are set up you could use either (!done) or move the while loop inside each if/else if so that the first example would work on each array when reading the different files.

You may want to consider a struct to hold information that is ordered and create an array of structs to work with.

I will load up the program and give it a compile to see if there is anything else.

Andy
Thank you, Andy!
That all makes sense. My professor told me it is probably not a good idea to use global variables so I will work on that. The text files literally just have a food item followed by spaces with a price then the next item is on the next line.
Thank you, again!
Hello ccorkran,

My point is that it is better for you to post the files that you are using, so that everyone will be using the same information and no one has to guess.

What you describe might be:
item 2.00
But your code looks like it should be:
item $2.00

If you do not want to post the files then give a proper layout of the file with at least 1 example.

Andy

Edit: typo
Last edited on
L75 etc is probably not right. C++ sets eof after an attempt has been made to read past the end of data - not when a successful read has read the last item of data. Therefore you'll get an extra loop around the while.

Something like (not tried):

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
string food;
double price {};

while (getline(iFile, food, '$') && (iFile >> price)) {
	if (i == 1) {
		// Assign values to drinks
		drinks[counter] = trim(food);
		drinkPrices[counter] = price
	} else if (i == 2) {
		// Assign values to appetizer
		appetizers[counter] = trim(food);
		appetizerPrices[counter] = price;
	} else if (i == 3) {
		// Assign values to entrees
		entrees[counter] = trim(food);
		entreesPrices[counter] = price;
	} else if (i == 4) {
		// Assign values to desserts
		desserts[counter] = trim(food);
		dessertsPrices[counter] = price;
	}

	iFile.ignore(256, '\n');
	++counter;
}

Last edited on
I need to store the item and quantity so that at the end the price can be added, any tips would be appreciated!


One easy way is to have a struct with the name/quantity/price and then use a vector of this struct to which chosen items are added. It's easy to print the bill etc.

I don't know how far you're reached in learning C++, but this code contains a lot of repetition and could be simplified/reduced quite a lot.

With a program like this, it's important to get the design right - and done before coding. As part of the design, the data structures need to be devised. Once you have these, then much of the coding falls into place and is relatively simple with appropriate functions etc.

For one simple way to define data structures for this program, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Item {
	std::string name;
	double cost {};
};

struct Menu {
	std::string filename;
	std::string description;
	std::vector<Item> items;
};

struct Bought {
	Item item;
	unsigned qty {};
};

enum menuType { MAIN, DRINK, APPETIZER, ENTREE, DESSERT, NOENTRIES };

Menu menus[NOENTRIES] {{"Main.txt", "Main"}, {"Drinks.txt", "Drinks"}, {"Appetizers.txt", "Appetizers"},
	{"Entrees.txt", "Entrees"}, {"Desserts.txt", "Desserts"}};

std::vector<Bought> ItemsBought;


Then code a function to read a menu from a file, called in a loop over menuType, then a function to display a menu etc etc etc. For one part of the program (read file, display menu, order item etc etc), the code is only written once (in a function?) and used with parameters to access the required part(s) of the struct(s) etc.
Thank you seeplus and Andy,
I have had another plan using structs but got stuck fairly early, but I will look back into it. This is the formatting for the files.

The main menu file looks like:

Welcome to Randos Restaurant!
Please choose a category below.

0. Done
1. Drinks
2. Appetizers
3. Entrees
4. Desserts

the Drinks file:

Item Price InStock

Coke $1.99 50
Pepsi $1.99 50
Sprite $1.99 40
Water $1.50 80

Appetizers file:

Item Price InStock

Cheese Sticks $3.50 30
Fried Pickles $4.99 25
Calamari $6.50 40

The Entree file:

Item Price InStock

Steak $17.50 30
Steak and Shrimp $18.50 25
Chicken Fried Rice $13.00 40
Seafood Feast $20.00 20
Pork $16.00 15

The dessert File:

Item Price InStock

Tiramisu $3.50 30
Lava Cake $4.99 25
Smores Cake $6.50 40
Can you change the layout of the main.txt file? IMO a better layout would be:


Drinks Drinks.txt
Appetizers Appetizers.txt
Entree Entee.txt
Desserts Desserts.txt


Then to add/change different menus, only this main.txt file is changed - not the program.

Then basically main.txt is used to initialise menus array from my post above and then the specified files in the main.txt file are processsed etc etc. Simples!

The other files just need the item/price/stock lines. The header line isn't needed (if present in the actual file).

As InStock element is present, then my Item struct can be expanded to also include this field.
Last edited on
Hello ccorkran,

Thank you for the input files.

The first thing I see is the the input files contain 3 fields, but when you read the file you are only reading 2 fields. After the first read it troughs everything off.

You need to account for every field that is in the input file. Which means that you are missing some arrays to handle the quantity field.

I do agree with seeplus that a struct(s) is a good idea over all those arrays.

Andy
Following on from my previous post re using data structures and now the format of the files, consider as a starter (as C++20 - but easily converted back as only initialiser within for-range):

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

struct Item {
	std::string name;
	double cost {};
	unsigned qty {};
};

struct Menu {
	std::string filename;
	std::string description;
	std::vector<Item> items;
};

struct Bought {
	Item item;
	unsigned qty {};
};

std::vector<Menu> menus;
std::vector<Bought> ItemsBought;

std::istream& operator>>(std::istream& ifs, Menu& men) {
	std::getline(ifs >> std::ws, men.description, ',');
	return ifs >> men.filename;
}

std::istream& operator>>(std::istream& ifs, Item& itm) {
	std::getline(ifs >> std::ws, itm.name, '$');
	for (; itm.name.back() == ' '; itm.name.pop_back());
	return ifs >> itm.cost >> itm.qty;
}

int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

bool readMain(const std::string& fnam)
{
	std::ifstream mainf(fnam);

	if (!mainf)
		return (std::cout << "Cannot open " << fnam << '\n'), false;

	for (Menu men; mainf >> men; menus.push_back(std::move(men))) {
		std::ifstream foodf(men.filename);

		if (!foodf)
			return (std::cout << "Cannot open " << men.filename << '\n'), false;

		for (Item itm; foodf >> itm; men.items.push_back(std::move(itm)));
	}

	return true;
}

int getOpt(int mx)
{
	int opt {};

	do {
		opt = getInt("\nEnter option number: ");
	} while ((opt < 0 || opt > mx) && (std::cout << "Invalid option number\n"));

	return opt;
}

bool addItem(int itm, Menu& men)
{
	auto& item {men.items[itm]};

	if (item.qty <= 0)
		return false;

	int bou {};

	do {
		bou = getInt("\nHow many? ");
	} while ((bou < 0 || bou > item.qty) && (std::cout << "Invalid quantity\n"));

	std::cout << "Adding " << item.name << '\n';

	ItemsBought.emplace_back(item, bou);
	item.qty -= bou;
	return true;
}

void processMenu(int mno)
{
	auto &menu {menus[mno]};

	for (int opt {1}; opt; ) {
		std::cout << '\n' << menu.description << "\n\n";

		for (size_t i {}; const auto & elem : menu.items) {
			++i;

			if (elem.qty > 0)
				std::cout << i << ". " << std::left << std::setw(20) << elem.name << "  " << elem.cost << " (" << elem.qty << ")\n";
		}

		std::cout << "0. Exit\n";

		if ((opt = getOpt(menu.items.size())) > 0)
			if (!addItem(opt - 1, menu))
				std::cout << "Invalid item chosen\n";
	}
}

int main()
{
	const std::string mainfile {"main.txt"};

	if (!readMain(mainfile))
		return 1;

	for (int opt {1}; opt; ) {
		std::cout << "\nWelcome to Randos Restaurant!\n\n";

		for (size_t i {}; const auto & m : menus)
			std::cout << ++i << ". " << m.description << '\n';

		std::cout << "0. Produce invoice\n";

		if ((opt = getOpt(menus.size())) > 0)
			processMenu(opt - 1);
	}

	std::cout << "\nItems bought\n\n";

	double total {};

	for (const auto& b : ItemsBought) {
		const auto bgt {b.item.cost * b.qty};

		total += bgt;
		std::cout << std::left << std::setw(20) << b.item.name << std::setw(7) << b.item.cost << std::setw(7) << b.qty << std::setw(7) << bgt << '\n';
	}

	std::cout << "\nTotal is " << total << '\n';
}



main.txt

Drinks,Drinks.txt
Appetizers,Appetizers.txt
Entree,Entree.txt
Desserts,Desserts.txt

Drinks.txt

Coke $1.99 50
Pepsi $1.99 50
Sprite $1.99 40
Water $1.50 80

Appetizers.txt

Cheese Sticks $3.50 30
Fried Pickles $4.99 25
Calamari $6.50 40

Entree.txt

Steak $17.50 30
Steak and Shrimp $18.50 25
Chicken Fried Rice $13.00 40
Seafood Feast $20.00 20
Pork $16.00 15

Desserts.txt

Tiramisu $3.50 30
Lava Cake $4.99 25
Smores Cake $6.50 40


Last edited on
Hello ccorkran,

I am not sure where you are at now, but to go over your original code it would help in reworking.

At the beginning you have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const string WHITESPACE = " \n\r\t\f\v|";
string ltrim(const string& s)
{
    size_t start = s.find_first_not_of(WHITESPACE);

    return (start == string::npos) ? "" : s.substr(start);
}

string rtrim(const string& s)
{
    size_t end = s.find_last_not_of(WHITESPACE);

    return (end == string::npos) ? "" : s.substr(0, end + 1);
}

string trim(string& s)
{
    return rtrim(ltrim(s));
}

This is overkill big time just to remove a space at the end of the string. You can simply use:
1
2
3
while (getline(iFile, food, '$') && (iFile >> price) && (iFile >> qty))
{
    food.pop_back();  // <--- Removes the last element of the string. 


Another change I made at the beginning is:
1
2
3
4
5
constexpr double SALES_TAX{ 0.1 }; // This restaurant has a 10% sales tax
constexpr int MAXSIZE{ 5 };
constexpr int THREE{ 3 };
constexpr int FOUR { 4 };
constexpr int FIVE { 5 };

"constexpr" along with the {}s, the uniform initializer, available from the C++11 standards on is very useful. The "constexpr" and {}s do have other advantages when used to define other non constant variables.

This is where using an array of structs would have an advantage. The prototype or forward declaration:
1
2
3
4
int readMenu(std::string fileNames[], std::string& mainMenu, std::string drinks[], double drinkPrices[],
             std::string appetizers[], double appetizerPrices[], std::string entrees[], double entreePrices[],
             std::string desserts[], double dessertPrices[], int drinksQty[], int appetizersQty[],
             int entreesQty[], int dessertsQty[]);

The function would be the same without the semi-colon. The function call is not quite as bad:
1
2
3
4
5
6
if (result = readMenu(fNames, mainMenu, drinks, drinkPrices, appetizers, appetizerPrices,
                        entrees, entreePrices, desserts, dessertPrices,
                        drinksQty, appetizersQty, entreesQty, dessertsQty)) // Read from the files and store info into appropriate arrays
{
    return result;
}

Mostly the same for "makeOrder" except you do not need to pass "fNames".

When reading the files 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{
    int idx{}, qty{};  // <--- ALWAYS initialize all your variables.
    double price{};
    std::string food;

    ifstream iFile;

    for (int i = 0; i < MAXSIZE; ++i)
    {
        iFile.open(fileNames); // Open the file

        if (!iFile)  // <--- [b][i]ALWAYS check the file streams.[/b]
        {
            std::cout << "\n File " << std::quoted(fileNames[i]) << " did not open.\n";
            //std::cout << "\n File \"" << fileNames[i] << "\" did not open.\n";

            return 1;
        }

        if (i == 0)
        {
            // Read entire Main.txt file and store contents in the mainMenu string
            mainMenu.assign(istreambuf_iterator<char>(iFile), istreambuf_iterator<char>());

            //std::cout << '\n' << mainMenu;
            //std::cout << '\n';
        }
        else
        {
            //iFile.ignore(256, '\n');
            //iFile.ignore(256, '\n');
            std::getline(iFile, food);  // <--- Just an alternative. Eithe set works.
            std::getline(iFile, food);

        while (getline(iFile, food, '$') && (iFile >> price) && (iFile >> qty))
        {
            food.pop_back();  // <--- Removes the last element of the string.

            if (i == 1)
            {
                 // Assign values to drinks
                 drinks[idx] = food;

                 drinkPrices[idx] = price;

                 drinksQty[idx] = qty;
             }

I did the same in the else if statements.

In:
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
void printMenu(int menuType, std::string drinks[], double drinkPrices[],
               std::string appetizers[], double appetizerPrices[], std::string entrees[], double entreePrices[],
               std::string desserts[], double dessertPrices[])
{
    switch (menuType)
    {
        case 1:
            cout <<
                "\n"
                "           Drinks Menu\n"
                << std::string(33, '-') << "\n\n" <<
                //"Please choose an item from the list below.\n\n"
                "0. Back to Main Menu\n";

            for (int i = 0; i < FOUR; ++i)
            {
                cout
                    << i + 1 << ". "
                    << left << setw(24)
                    << drinks[i] << "$" << std::right << std::setw(5) << drinkPrices[i] << '\n';
            }

            cout << '\n';

            break;

All the parameters came from "makeOrder". I did change the name of the 1st parameter, but that is not required. It is your choice. I also changed the appearance of the output, but you do not have to. See what you think when I add the output.

Also in "makeOrder":
1
2
3
4
5
6
7
else if (curMenu == DRINK)
{
    printMenu(DRINK, drinks, drinkPrices, appetizers, appetizerPrices,
                entrees, entreePrices, desserts, dessertPrices);

    cout << "Enter one of the options above: ";
    cin >> selection;

You have defined an enum. You can use it for more than just the if statement.

This is the output I get:

  Welcome to Randos Restaurant!
---------------------------------

0. Done
1. Drinks
2. Appetizers
3. Entrees
4. Desserts

Enter one of the options above: 1

Clear screen

           Drinks Menu
---------------------------------

0. Back to Main Menu
1. Coke                    $ 1.99
2. Pepsi                   $ 1.99
3. Sprite                  $ 1.99
4. Water                   $ 1.50

Enter one of the options above: 0

Clear screen

Appetizers Menu
Please choose an item from the list below.

0. Back to Main Menu
1. Cheese Sticks       $ 3.50
2. Fried Pickles       $ 4.99
3. Calamari            $ 6.50

Please enter one of the options above: 0

Clear screen

Entrees Menu
Please choose an item from the list below.

0. Back to Main Menu
1. Steak               $17.50
2. Steak and Shrimp    $18.50
3. Chicken Fried Rice  $13.00
4. Seafood Feast       $20.00
5. Pork                $16.00

Please enter one of the options above: 0

Clear screen
Desserts Menu
Please choose an item from the list below.

0. Back to Main Menu
1. Tiramisu            $ 3.50
2. Lava Cake           $ 4.99
3. Smores Cake         $ 6.50

Please enter one of the options above: 0


After changing the "Drinks menu" I have not finished changing the rest.

I do hope that this will give you some ideas.

Andy
Last edited on
Thank you Andy and Seeplus,
I have made some progress. Thank you for the help. I will upload it when i am finished! Thank you again.
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
154
155
156
157
158
159
160
161
162
163
164
165
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

// Global variable declarations

string mainMenu;
const double salesTax = 0.1; // This restaurant has a 10% sales tax

// Parallel arrays to store menu items and prices
string drinks[4], appetizers[4], entrees[5], desserts[3];
double drinkPrices[4], appetizerPrices[4], entreePrices[5], dessertPrices[3];

// Make more parallel arrays below this comment to store a customers order

double drinkOrder[4], appetizerOrder[3], entreeOrder[5], dessertOrder[3];
double totalPrice[4];
double total = 0.00;



const string WHITESPACE = " \n\r\t\f\v|";

string ltrim(const string& s) {
    size_t start = s.find_first_not_of(WHITESPACE);
    return (start == string::npos) ? "" : s.substr(start);
}

string rtrim(const string& s) {
    size_t end = s.find_last_not_of(WHITESPACE);
    return (end == string::npos) ? "" : s.substr(0, end + 1);
}

string trim(const string& s) {
    return rtrim(ltrim(s));
}

void readMenu(string[]);

// Will print a specific menu when called
void printMenu(int);

void makeOrder();

void makePayment();

int main() {
    // Each file name for the menus
    string fNames[] = { "Main.txt", "Drinks.txt", "Appetizers.txt", "Entrees.txt", "Desserts.txt" };

    readMenu(fNames); // Read from the files and store info into appropriate arrays
    makeOrder();  // Have the user make an order
    makePayment(); // Have the user pay for their meal

    return 0;
}

void readMenu(string fileNames[]) {
    int counter = 0;
    ifstream iFile;

    for (int i = 0; i < 5; i++) {
        iFile.open(fileNames[i]); // Open the file

        if (i == 0) {
            // Read entire Main.txt file and store contents in the mainMenu string
            mainMenu.assign(istreambuf_iterator<char>(iFile), istreambuf_iterator<char>());

        }
        else {
            iFile.ignore(256, '\n');
            iFile.ignore(256, '\n');

            while (!iFile.eof()) {
                if (i == 1) {
                    // Assign values to drinks
                    getline(iFile, drinks[counter], '$');
                    drinks[counter] = trim(drinks[counter]);
                    iFile >> drinkPrices[counter];
                }
                else if (i == 2) {
                    // Assign values to appetizers
                    getline(iFile, appetizers[counter], '$');
                    appetizers[counter] = trim(appetizers[counter]);
                    iFile >> appetizerPrices[counter];
                }
                else if (i == 3) {
                    // Assign values to entrees
                    getline(iFile, entrees[counter], '$');
                    entrees[counter] = trim(entrees[counter]);
                    iFile >> entreePrices[counter];
                }
                else if (i == 4) {
                    // Assign values to desserts
                    getline(iFile, desserts[counter], '$');
                    desserts[counter] = trim(desserts[counter]);
                    iFile >> dessertPrices[counter];
                }

                iFile.ignore(256, '\n');
                ++counter;
            }

            counter = 0;
        }

        iFile.close();
    }
}

void printMenu(int menu) {
    switch (menu) {
    case 1:
        cout << "Drinks Menu" << endl;
        cout << "Please choose an item from the list below." << endl << endl;
        cout << "0. Back to Main Menu" << endl;
        for (int i = 0; i < 4; i++) {
            cout << i + 1 << ". ";
            cout << left << setw(25) << fixed << setprecision(2);
            cout << drinks[i] << "$" << drinkPrices[i] << endl;
        }

        cout << endl;
        break;
    case 2:
        cout << "Appetizers Menu" << endl;
        cout << "Please choose an item from the list below." << endl << endl;
        cout << "0. Back to Main Menu" << endl;
        for (int i = 0; i < 3; i++) {
            cout << i + 1 << ". ";
            cout << left << setw(25) << fixed << setprecision(2);
            cout << appetizers[i] << "$" << appetizerPrices[i] << endl;
        }
        cout << endl;
        break;
    case 3:
        cout << "Entrees Menu" << endl;
        cout << "Please choose an item from the list below." << endl << endl;
        cout << "0. Back to Main Menu" << endl;
        for (int i = 0; i < 5; i++) {
            cout << i + 1 << ". ";
            cout << left << setw(25) << fixed << setprecision(2);
            cout << entrees[i] << "$" << entreePrices[i] << endl;
        }
        cout << endl;
        break;
    case 4:
        cout << "Desserts Menu" << endl;
        cout << "Please choose an item from the list below." << endl << endl;
        cout << "0. Back to Main Menu" << endl;
        for (int i = 0; i < 3; i++) {
            cout << i + 1 << ". ";
            cout << left << setw(25) << fixed << setprecision(2);
            cout << desserts[i] << "$" << dessertPrices[i] << endl;
        }
        cout << endl;
        break;
    }
}

Last edited on
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
void makeOrder() {
    enum menuType { MAIN, DRINK, APPETIZER, ENTREE, DESSERT };

    bool done = false;
    int selection = -1;
    int quantity;
    double totalPrice;
    menuType curMenu = MAIN;

    while (!done) {

        if (curMenu == MAIN) {
            cout << mainMenu << endl;
            cout << "Please enter one of the options above: " << endl;
            cin >> selection;
            switch (selection) {
            case 0:
                done = true;
                break;
            case 1:
                curMenu = DRINK;
                break;
            case 2:
                curMenu = APPETIZER;
                break;
            case 3:
                curMenu = ENTREE;
                break;
            case 4:
                curMenu = DESSERT;
                break;
            }
        }
        else if (curMenu == DRINK) {
            printMenu(DRINK);
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                curMenu = MAIN;
                break;
            case 1:
                cout << "How many would you like? ";
                cin >> quantity;
                    totalPrice = (quantity * drinkPrices[0]);
                    drinkOrder[0] = totalPrice;
                break;
            case 2:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * drinkPrices[1]);
                drinkOrder[1] = totalPrice;
                break;
            case 3:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * drinkPrices[2]);
                drinkOrder[2] = totalPrice;
                break;
            case 4:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * drinkPrices[3]);
                drinkOrder[3] = totalPrice;
                break;
            }
        }
        else if (curMenu == APPETIZER) {
            printMenu(2);
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                curMenu = MAIN;
                break;
            case 1:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * appetizerPrices[0]);
                appetizerOrder[0] = totalPrice;
                break;
            case 2:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * appetizerPrices[1]);
                appetizerOrder[1] = totalPrice;
                break;
            case 3:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * appetizerPrices[2]);
                appetizerOrder[2] = totalPrice;
                break;
            }
        }
        else if (curMenu == ENTREE) {
            printMenu(3);
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                curMenu = MAIN;
                break;
            case 1:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * entreePrices[0]);
                entreeOrder[0] = totalPrice;
                break;
            case 2:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * entreePrices[1]);
                entreeOrder[1] = totalPrice;
                break;
            case 3:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * entreePrices[2]);
                entreeOrder[2] = totalPrice;
                break;
            case 4:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * entreePrices[3]);
                entreeOrder[3] = totalPrice;
                break;
            case 5:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * entreePrices[4]);
                entreeOrder[4] = totalPrice;
                break;
            }
        }
        else if (curMenu == DESSERT) {
            printMenu(4);
            cout << "Please enter one of the options above: ";
            cin >> selection;
            switch (selection) {
            case 0:
                curMenu = MAIN;
                break;
            case 1:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * dessertPrices[0]);
                dessertOrder[0] = totalPrice;
                break;
            case 2:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * dessertPrices[1]);
                dessertOrder[1] = totalPrice;
                break;
            case 3:
                cout << "How many would you like? ";
                cin >> quantity;
                totalPrice = (quantity * dessertPrices[2]);
                dessertOrder[2] = totalPrice;
                break;
            }
        }
        system("cls");
    }
}

void makePayment() {
    double tip, payment, updatedTotal;
    double orderTotal, drinkTotal, appetizerTotal, entreeTotal, dessertTotal;
    orderTotal = ((drinkOrder[0] + drinkOrder[1] + drinkOrder[2] + drinkOrder[3]) + 
                  (appetizerOrder[0] + appetizerOrder[1] + appetizerOrder[2]) + 
                  (entreeOrder[0] + entreeOrder[1] + entreeOrder[2] + entreeOrder[3] + entreeOrder[4]) + 
                  (dessertOrder[0] + dessertOrder[1] + dessertOrder[2]));

    cout << "Thank you for your business" << endl;
    cout << "Below is your receipt" << endl << endl;


    cout << "Subtotal: $" << orderTotal << endl;
    cout << "Sales Tax: $" << (orderTotal * salesTax) <<  endl;
    cout << "Total: $" << (orderTotal * 1.1) << endl << endl;


    cout << "Enter Tip Amount: $";
    cin >> tip;
    updatedTotal = (tip + (orderTotal * 1.1));
    cout << endl << "Updated Total: $" << (updatedTotal) << endl;


    cout << "Please Enter Payment: $";
    cin >> payment;
    cout << endl; 


    cout << "Thank you for your business!" << endl;
    cout << "Your Change is: $" << (payment - updatedTotal) << endl;
    cout << "Have a nice day! :)" << endl;

}
Hello ccorkran,

I take it there is no point in rehashing what has already been said.

Since you did not post the full instructions for the program no one knows what you have to do or what you have to use.

If the program works for you go with it, but it is wrong and still has problems.

Given from the input file: Pork $16.00 15. The quantity on hand is 15, but you never read or save this value in you program for use. Lets say that 20 people come into the restaurant wanting to order "Pork" 5 people will be disappointed when you run out because the program does not adjust the quantity, so you will never know when you have used all the "Pork" dinners.

Eventually the while loop in the "read" function will be a problem, but if you think it is good then stick with it.

The program could have a much better design and make use of a struct:
1
2
3
4
5
6
struct FoodItem
{
    std::string sFoodItem;  // <--- Or "sName" will work.
    double sPrice{};
    int sQuantityOnHand{};
};

to hold the information of each item. If this is allowed? Then you would only have 4 vectors for each item type.

The "makeOrder" function needs to deal with the starting quantity and adjust as needed.

In the "print" function you need to check the "QuantityOnHand" and if it is (0)zero say "sold out".

Andy
Topic archived. No new replies allowed.