Breakfast billing problem using structs and arrays

Hello again,

I'm in the middle of working on a programming problem, and it's a breakfast billing program that is actually found on here, with the only exception being the max number of selections are 8 instead of 50 (1 of each menu item):

http://www.cplusplus.com/forum/beginner/75558/

It's the problem posted on 7/20/12.

The answer is also here:
http://pastebin.com/MLNKJdxq


However, I'm still having an issue with this problem, as it uses pointers (I think thats what the * indicates, yes?), and I have zero knoweledge of how to use these.

Since I have not learned use of pointers yet, how would I go about doing the billing part without them? The issue I'm coming up with is that I don't know how to store the choices the other person makes, and add up the price with tax accordingly...

I'm also having difficulty just displaying what choices they made as well. This has been very frustrating for me, can someone give me a push in the right direction?

Thanks,
-Drew
Still need help, but here is my attempt so far... it has big problems.

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

//structure with both the item's name, and price of said item;
struct menuItemType {
	string item;
	double price;
} ;

void displayMenu(); //displays menu along with item numbers
void getData(menuItemType& menuList, string item, double price);  //loads data into the array menuList
void printCheck(menuItemType& menuList, int n); //calculates, and displays the check, formatted to 2 dec. places

void main()
{
	const int breakfast = 7; //const int for the 8 breakfast items listed
	menuItemType menuList[breakfast]; //array menuList includes item name with prices
	int order;
	double price;
	string item;
	int i=0; //counter for adding items to array

	cout << "Welcome to Johnny's Restaurant! \n\n"
		    "Our menu options for today are:\n\n ";

	displayMenu(); //displays the menu with item numbers included

	cout << "Please select your choices by item number!\n"
			"To order, please enter the item numbers you wish to order.\n"
			"ex: to order coffee and an egg, the order would be: 7 1\n\n"
			"To stop ordering, type s, then hit enter.\n\n"
		    "In consideration to others, we ask that you not order\n"
			"more than one of each item please! \n\n";

	//ordering loop below, by item number
	while (cin >> order)
	{
		displayMenu();
		switch (order)
		{
		case 1:
			{
				item = "Plain egg";
				price = 1.45;
				break;
			}
		case 2:
			{
				item = "Bacon with egg";
				price = 2.45;
				break;
			}
		case 3:
			{
				item = "Muffin";
				price = 0.99;
				break;
			}
		case 4:
			{
				item = "French Toast";
				price = 1.99;
				break;
			}
		case 5:
			{
				item = "Fruit Basket";
				price = 2.49;
				break;
			}
		case 6:
			{
				item = "Cereal";
				price = 0.69;
				break;
			}
		case 7:
			{
				item = "Coffee";
				price = 0.50;
				break;
			}
		case 8:
			{
				item = "Tea";
				price = 0.75;
				break;
			}
		default:
			{
			cout << "We do not carry that selection, sorry. \n"
					"Please attempt your order again!\n";
			continue;
			}
		}
			getData(menuList[i], item, price);
			++i; //increases counter by 1
			if (i==8)
				break;
	}
	printCheck(menuList[0], i);
}

void displayMenu()
{
	cout <<
			"\n "
			"Item #--Item-------------------Price----\n"
			" 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"
			"----------------------------------\n";
}

void getData(menuItemType& menuList, string item, double price)
{
	menuList.item = item;
	menuList.price = price;
}

void printCheck(menuItemType& menuList, int n)
{
	for(int i=0; i < n ; ++i)
	{
		cout << menuList.item << "    " << menuList.price << "\n";
	}
}


Now the printCheck is just being used as an output test right now, and what I found is that it always gives me multiple of whatever the first object I order is... for example if I order 4 items, and the first one is a plain egg, it gives me 4 plain eggs. -_-

Also, the program crashes if I enter more than 8 items, and I can't figure out why.
1
2
	const int breakfast = 7; //const int for the 8 breakfast items listed
	menuItemType menuList[breakfast]; //array menuList includes item name with prices 


If you want room to hold 8 items, you must allocate room for 8 items. breakfast should be initialized to 8.

1
2
3
4
5
6
7
void printCheck(menuItemType& menuList, int n)
{
	for(int i=0; i < n ; ++i)
	{
		cout << menuList.item << "    " << menuList.price << "\n";
	}
}


printCheck takes a reference to one menuItemType and prints that one object n times.
EDIT:
actually that still leaves the question of how to dispay multiple, different orders?

and the program still crashes at 9 inputs now.

I'm going to step back and think some more, maybe I'll figure this out.
Last edited on
and the program still crashes at 9 inputs now.


Well, of course it does, you don't allocate room to store that many. Consider using a std::vector to store the inputs.

actually that still leaves the question of how to dispay multiple, different orders?


1
2
3
4
5
6
7
void printCheck(menuItemType* menuList, int n)
{
	for(int i=0; i < n ; ++i)
	{
		cout << menuList[i].item << "    " << menuList[i].price << "\n";
	}
}


Or, using a std::vector<menuItemType>:

1
2
3
4
5
6
7
void printCheck(const std::vector<menuItemType>& v)
{
	for(int i=0; i < v.size() ; ++i)
	{
		cout << v[i].item << "    " << v[i].price << "\n";
	}
}
Well, isn't the * part a pointer? And I don't know how to use vectors yet, we have not covered them currently.

Regardless, if I incorporate the example of displaying multiple orders, It displays the following:

Welcome to Johnny's Restaurant!

Our menu options for today are:


 Item #--Item-------------------Price----
 1      Plain egg                $1.45
 2      Bacon and egg            $2.45
 3      Muffin                   $0.99
 4      French Toast             $1.99
 5      Fruit Basket             $2.49
 6      Cereal                   $0.69
 7      Coffee                   $0.50
 8      Tea                      $0.75
----------------------------------
Please select your choices by item number!
To order, please enter the item numbers you wish to order.
ex: to order coffee and an egg, the order would be: 7 1

To stop ordering, type s, then hit enter.

In consideration to others, we ask that you not order
more than one of each item please!

1

 Item #--Item-------------------Price----
 1      Plain egg                $1.45
 2      Bacon and egg            $2.45
 3      Muffin                   $0.99
 4      French Toast             $1.99
 5      Fruit Basket             $2.49
 6      Cereal                   $0.69
 7      Coffee                   $0.50
 8      Tea                      $0.75
----------------------------------
2

 Item #--Item-------------------Price----
 1      Plain egg                $1.45
 2      Bacon and egg            $2.45
 3      Muffin                   $0.99
 4      French Toast             $1.99
 5      Fruit Basket             $2.49
 6      Cereal                   $0.69
 7      Coffee                   $0.50
 8      Tea                      $0.75
----------------------------------
s
P    1.45
l    1.45
Press any key to continue . . .


So it's taking the first and second letter of "Plain egg" and putting them as both outputs...
I thought I could fix it by using cin.ignore, but I have been unable to fix the outputs so far.

So this makes me think, maybe I'm trying to store them in the first place incorrectly?

Also, I keep trying to terminate the ordering process if the orders reach 8, by saying:
1
2
3
++i; //increases counter by 1
if (i==8)
break;


But that is obviously incorrect, as the program becomes unrepsonsive and force quits. What am I doing wrong? I though break was supposed to terminate the loop?
Last edited on
Wait, never mind folks, I'm sorry.

I've been really really stupid....

I solved the whole problem by rewriting it, I just simply had to start by declaring a constant array
1
2
3
4
5
6
7
8
9
10
11
const  int breakfast = 8;

const menuItemType menuList[breakfast] = 
{"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};


for the menu, and then fill in a dynamic one for the customer, and then the whole thing just popped like a lightbulb for me. Such a simple problem that I was way way overcomplicating....

Sorry for any headaches I caused anyone!

EDIT #2:

Welcome to Andrew's Restaurant!

Our menu options for today are the following:

 Item #----Item----------Price----
1.)   Plain Egg           1.45
2.)   Bacon and egg       2.45
3.)   Muffin              0.99
4.)   French Toast        1.99
5.)   Fruit Basket        2.49
6.)   Cereal              0.69
7.)   Coffee              0.50
8.)   Tea                 0.75

Please select your choices by item number!
To order, please enter the item numbers you wish to order.
ex: to order coffee and an egg, the order would be: 7 1

To stop ordering, type s, then hit enter.

In consideration to others, we ask that you not order
more than one of each item please!

2 5 8 1
s

---------Customer-Bill----------
1.)   Bacon and egg       2.45
2.)   Fruit Basket        2.49
3.)   Tea                 0.75
4.)   Plain Egg           1.45

--------------Total--------------
Total before tax:         7.14
Total after 5% tax:       7.50
---------------------------------

Thanks for visiting, please come again!

Press any key to continue . . .


^_^ WEEEEEEEEEEEEEEE!
Last edited on
Topic archived. No new replies allowed.