Breakfast Menu Program help

Hey guys so i am doing a c++ program where the user picks items from the menu and it shows the sub total, tax and grand total. Everything seems to be working but I am having trouble figuring out how to make it show A count of the number of times the item has been ordered
 and write a function whereto determine (and return) the index of the item with the highest count (if more
than one item has the maximum count, it must return the index of any one of them).


#include <iostream>

using namespace std;
struct menuItemType
{
string name;
double price;
};

void GetData(menuItemType & menu, string name, double price)
{
menu.name = name;
menu.price = price;
}

void ShowMenu()
{
cout << "(1) Plain Egg.............$1.45\n"
<< "(2) Bacon and Egg....$2.45\n"
<< "(3) Muffin..................$0.99\n"
<< "(4) French Toast.......$1.99\n"
<< "(5) Fruit Basket.........$2.49\n"
<< "(6) Cereal.................$0.69\n"
<< "(7) Coffee......................$0.50\n"
<< "(8) Tea.........................$0.75\n";
}

void printCheck(menuItemType & selection, double* total)
{
*total += selection.price;
cout << selection.name << " " << selection.price << endl;
}

int main()
{
menuItemType menuList[30];
cout << "Select as many items are you want. Press N then enter key to stop\n";
int choice;
int counter = 0;
double price;
double total = 0;
string name;
menuItemType * ptr_begin(menuList);
menuItemType * ptr_end(menuList);

ShowMenu();
while (cin >> choice)
{
ShowMenu();
if (choice == 1)
{
price = 1.45;
name = "Plain Egg";
}
else if (choice == 2)
{
price = 2.45;
name = "Bacon and Egg";
}
else if (choice == 3)
{
price = 0.99;
name = "Muffin";
}
else if (choice == 4)
{
price = 1.99;
name = "French Toast";
}
else if (choice == 5)
{
price = 2.49;
name = "Fruit Basket";
}
else if (choice == 6)
{
price = 0.69;
name = "Cereal";
}
else if (choice == 7)
{
price = 0.50;
name = "Coffee";
}
else if (choice == 8)
{
price = 0.75;
name = "Tea";
}

else
{
cout << "\n***Thats not a proper selection. Please try again.***\n";
continue;
}
GetData(menuList[counter], name, price);
ptr_end += 1;
++counter;
if (counter == 30)
break;
}
if (ptr_begin != ptr_end)
{
cout << "\nYou selection consists of;\n";
for (menuItemType * ptr_begin(menuList); ptr_begin != ptr_end; ptr_begin += 1)
{
printCheck(*ptr_begin, &total);
}
cout<< "\nYour subtotal will be:$"<< total<<endl;
cout << "\nTotal Tax: $"<< (total * .07) <<endl;
cout.precision(3);
cout <<"Your Grand Total:$"<< total + (total * .07) << endl;
}
cout << "\nThank you. Come again.\n";

return 0;
}
hi anger 512.

Please always user code tags (the <> icon on the right side of the text box) it makes your code a whole lot easier for people to read.

Everything seems to be working but I am having trouble figuring out how to make it show A count of the number of times the item has been ordered
 and write a function whereto determine (and return) the index of the item with the highest count (if more
than one item has the maximum count, it must return the index of any one of them).

Why don't you make an array for instance holding for each of the items, the number of times the user has bought them (incrementing by 1 each time user choses said item). Then you can run a comparison function to find the item(s) that have been bought the most times.
This is how I would have implemented what you mentioned. Changing a couple of things, especially your menuItemType pointers..

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

using namespace std;

enum { plain_egg, bacon_and_egg, muffin, french_toast, fruit_basket, cereal, coffe, tea };

struct Item
{
	string name;
	int count = 0;
	double price;
	
	Item(string n, double p) : name(n), price(p)
	{}
};

void showMenu(Item*, const int);
string checkMostPurchasedItem(Item*, const int);
void pause();



int main()
{
	const int MAX_ITEMS = 8;
	const int MAX_PURCHASES = 10;
	Item items[MAX_ITEMS] = { {"Plain egg", 1.45},
							{"Bacon and egg", 2.45},
							{"Muffin", 0.99},
							{"French Toast", 1.99},
							{"Fruit basket", 2.49},
							{"Cereal", 0.69},
							{"Coffee", 0.50},
							{"Tea", 0.75} };
							
	int purchasedItems[MAX_PURCHASES] = { -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1 };					
	double totalPrice = 0;
	
	cout << "\n You can buy up to " << MAX_PURCHASES << " items. Enter \"0\" when you want to cash out";
	
	for (int count = 0; count < MAX_PURCHASES; count ++)
	{
	    int choice;
	    
	    do
	    {
		    showMenu(items, MAX_ITEMS);
		
		    cin >> choice;
		    choice--;
		    
		    if (choice == -1)
			    break;
		
			cout << "\n Incorrect input! Try again..";
		
			for (int index = plain_egg; index <= tea; index ++)
			{
				if (choice == index)
				{
					purchasedItems[count] = index;
										
					totalPrice += items[index].price;
					items[index].count++;
					break;
				}
			}
	    }
		while (choice < -1 || choice > 7);
		
		if (choice != -1)
			cout << "\t\t\t Item bought!";
	    else
	        break;
	}

		
	cout << "\n\n You have purchased the following items: \n";
	
	for (int count = 0; count < MAX_PURCHASES; count ++)
	{
		if (purchasedItems[count] != -1)
		{
			cout << "\n_ " << items[purchasedItems[count]].name;
		}
		else
			break;
	}
	
	cout << checkMostPurchasedItem(items, MAX_ITEMS);
	
	cout << "\n\n Total is: $" << totalPrice;
	
	cout.precision(3);
	
	cout << "\n Total with taxes is $" << totalPrice + (totalPrice *.07)
		<< "\n Thank you. Come again.\n";
		
		
	
	pause();
	
	return 0;
}


void showMenu(Item item[], const int MAX)
{
	cout << "\n\n ___MENU___\n";
	
	for (int count = 0; count < MAX; count ++)
	{		
		cout << "\n (" << count + 1 << ")_" << item[count].name << " ";
				
		for (int index = item[count].name.size(); index < 30; index ++)
			cout <<  ".";
			
		cout << " $" << item[count].price;
		
	
	}
	
	cout << "\n\n Choice: ";
	
	return;
}

string checkMostPurchasedItem(Item item[], const int MAX)
{
	int highest = 0;
	string highestName;
	
	for (int count = 0; count < MAX; count ++)
	{
		if (item[count].count > highest)
		{
			highest = item[count].count;
			highestName = item[count].name;
		}
	}
	
	if (highest != 0)
	{
		string temp = "\n\n\n The Item that was the most bought was ";
		temp += highestName;
		temp += ",\n you bought it ";
		temp.append(to_string(highest));
		temp += " times!";
		
		return temp;
	}
	else
		return ("");
}

void pause()
{
	cout << "\n\n\n Press ENTER to exit...";
	cin.sync();
	cin.get();
	cout << "\n\n\n\n";
	
	return;
}
Last edited on
Topic archived. No new replies allowed.