Setting Up a Data Structure

Hello All,
Beginner to C++ here. Can some please give me a few hints to help set up a data structure. Basically I need a way for someone ordering this fast food to be able to combine their options and then have them all print out on a "receipt" at the end.

I have already set up structures and given each item a value (price), I am just struggling to find for the program to keep track of it

Thanks!

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
#include<iostream>
#include<iomanip>
#include"msofcon.cpp"
#include<conio.h>
#include<windows.h>
#include <stdlib.h>

using namespace std;

void printMenu();
void printBurgerOptions();
void printChickenOptions();
void printSaladOptions();
void printFishOptions();
void printSides();
void printDrinks();

struct menuItem
{
	int idNumber;
	char name[20];
	float price;
};

struct order
{
	int quantity;
	char[20] name;
	float price;
	char menu[25];
};


int main()
{
	init_graphics();

	char ch='a';
	//////////////////////////Burger Prices////////////////////////
	int DeburgerPrice=5;
	int GigaburgerPrice=4.75;
	int FloppyBurgerPrice=5.25;
	int BurgerRomPrice=4.50;
	//////////////////////Chicken Prices///////////////////////////
	int cppPrice=4;
	int GigachickPrice=6.50;
	int JavachickPrice=6.25;
	////////////////////Salad Prices/////////////////////////////
	int CesearCPrice=5.50;
	int cppItalianPrice=4.75;
	int ReferenceRanchPrice=5.75;
	///////////////////Fish Prices///////////////////////////////
	int FishermenPrice=6.50;
	int FishaBytePrice=5.75;
	int	MysteryFishPrice=5.50; 
	//////////////////Side Prices///////////////////////////////
	int CompilerFriesPrice=2.50;
	int DoubleDogPrice=4.50;
	int IntergerIceCreamPrice=3.25;
	/////////////////Drink Prices//////////////////////////////
	int SpritePrice=2;
	int CocaColaPrice=2;
	int MountainDew=2;
	int WaterPrice=1.50;


	wait(2000);
	cout<<"Hello and Welcome to Program-A-Burger!"<<endl <<endl;
	wait(2000);
	cout<<"Please take a few mintues to look at the Menu, then press 'r' and then hit 'Enter' when you are ready to order." <<endl<<endl;
	wait(2000);
	cout<<endl;
	printMenu();

	do
		{	
			wait(3000);
			cin>>ch;
		}
	while(ch!='r');

	cout<<endl;
	cout<<"Now that you are ready to order,select the number that corresponds to your choice, then press enter."<<endl<<endl;
	cin>>ch;
	
	switch(ch)
	{
		case'1': printBurgerOptions();
				  break;
		case'2': printChickenOptions();
				  break;
		case'3': printSaladOptions();
				  break;
		case'4': printFishOptions();
				  break;
		case'5': printDrinks();
				  break;
		case'6': printSides();
				  break;
		default: cout<<"Thanks for your business, Goodbye."<<endl;
	
	};



	return(0);
}

void printMenu()
{
	set_cursor_pos(1,1);
	cout<<"Please choose one of the following Caterogories on our Menu: "<<endl<<endl;
	cout<<setw(5)<<"1.Hamburgers"<<endl<<endl;
	cout<<setw(5)<<"2.Chicken"<<endl<<endl;
	cout<<setw(5)<<"3.Salads"<<endl<<endl;
	cout<<setw(5)<<"4.Fish"<<endl<<endl;
	cout<<setw(5)<<"5.Drinks"<<endl<<endl;
	cout<<setw(5)<<"6.Sides"<<endl<<endl;
};

void printBurgerOptions()
{
	clear_screen();
	set_cursor_pos(1,1);
	cout<<"Please choose one of the following  options for your burger: "<<endl<<endl;
	cout<<setw(5)<<"1.The Deburger (Bacon, Onions, Pickles, Ketchup)"<<endl<<endl;
	cout<<setw(5)<<"2.The Gigaburger(Lettuce, Tomato, Onion)"<<endl<<endl;
	cout<<setw(5)<<"3.The FloppyBurger(Plain)"<<endl<<endl;
	cout<<setw(5)<<"4.The BurgerRom(VeggieBurger)"<<endl<<endl;
};

void printChickenOptions()
{
	clear_screen();
	set_cursor_pos(1,1);
	cout<<"Please choose one of the following  options for your chicken: "<<endl<<endl;
	cout<<setw(5)<<"1.The cpp-a-filet (Plain Chicken on a bun)"<<endl<<endl;
	cout<<setw(5)<<"2.The Gigachick(Lettuce, Tomato, Onion)"<<endl<<endl;
	cout<<setw(5)<<"3.The Javachick(Plain)"<<endl<<endl;
};

void printSaladOptions()
{
	clear_screen();
	set_cursor_pos(1,1);
	cout<<"Please choose one of the following  options for your burger: "<<endl<<endl;
	cout<<setw(5)<<"1.CesearC (Cesear Salad)"<<endl<<endl;
	cout<<setw(5)<<"2.cppItalian (House Salad)"<<endl<<endl;
	cout<<setw(5)<<"3.ReferenceRanch (House with Ranch Dressing)"<<endl<<endl;
};

void printFishOptions()
{
	clear_screen();
	set_cursor_pos(1,1);
	cout<<"Please choose one of the following  options for your burger: "<<endl<<endl;
	cout<<setw(5)<<"1.The Fishermen (Fried Flounder)"<<endl<<endl;
	cout<<setw(5)<<"2.FishaByte (Salmon with rice)"<<endl<<endl;
	cout<<setw(5)<<"3.MysterFish (Fried Tilopia)"<<endl<<endl;
};

void printSides()
{
	clear_screen();
	set_cursor_pos(1,1);
	cout<<"Please choose one of the following sides you'd like to order: "<<endl<<endl;
	cout<<setw(5)<<"1.Compiler Fries (French Fries)"<<endl<<endl;
	cout<<setw(5)<<"2.DoubleDog (Hot Dog)"<<endl<<endl;
	cout<<setw(5)<<"3.Interger Ice Cream"<<endl<<endl;
};

void printDrinks()
{
	clear_screen();
	set_cursor_pos(1,1);
	cout<<"Please choose one of the following drinks to complete your order"<<endl<<endl;
	cout<<setw(5)<<"1.Sprite"<<endl<<endl;
	cout<<setw(5)<<"2.Coca Cola"<<endl<<endl;
	cout<<setw(5)<<"3.Mountain Dew"<<endl<<endl;
	cout<<setw(5)<<"4.Water"<<endl<<endl;
};

Before you can write data to your structures you need to create an object of them. For example:

order myOrder;

then you can access its members like this..

myOrder.price = 4.99;

Anyway, how I would approach this in respect of your structures would be to create a Customer structure with an object of structure Order, that way the orders and customers remain linked automatically and don't need any special coding such as a "primary key".

Something like...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

struct Order
{
	char itemName[50];
	float itemPrice;
	float itemQty;

	// simple constructor to clear data.
	Order() { itemName[0] = 0; itemPrice = 0; itemQty = 0; }


};

struct Customer
{
	char custName[50];
	Order custOrders[20];  // max 20 items.

	// simple constructor to clear data.
	Customer() { custName[0] = 0; }
};



In the above example we could create our objects of customer like..

Customer MyCustomers[5];

What we did here is create objects of our structure in the same way we do with arrays, so when accessing them we do so in the same way..

MyCustomers[0].custName

so if I were reading in the customers name I could do something like this..

1
2
	cout << "Enter Name:";
	cin.getline(MyCustomers[0].custName, 50);



Because our structure Customer created our structure Order objects they are linked... so if we wanted to access the 1st order of the 1st customer (remembering that arrays start at 0):

MyCustomers[0].custOrders[0].itemName

Hope this all helps, if not just fire some questions.
Last edited on
You're missing the point of using structures. You create a menu struct at line 18, but then you never use it.

Lines 40-64: Creating individual variables for all of your prices is just going to cause you problems. BTW, declaring your prices as ints is going to truncate your prices.

If you want to keep different kinds of menu items, then you will need an array of each kind.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
  menuItem burgers[5];  // An array of various burgers
  menuItem chicken[3];  // An array of chicken items
... etc for salads, sides, drinks

    void AddItem (menuItem *items, int id, char * name, float price)
    {   items[id]->idnumber = id; 
        strcpy (items[id]->name, name); 
        items[id]->price = price; 
    }

    //  Add the various burgers to the menu array
    AddItem (burgers, 0, "Deburger", 5.00);
    AddItem (burgers, 1, "Gigaburger", 4.75);
    ... etc for each of the other burgers
    AddItem (chicken, 0, "cpp-a-filet", 4.00);
    ... etc for other chicken items
    ... etc for salads, sides and drinks

    //  To display a submenu
    void DisplaySubmenu (menuItems * items, int cnt)
    {  for (int i=0; i<cnt; i++)
            cout << i+1 << ". " << items[i]->name << " " << items[i]->price << endl;
    }
Last edited on
Topic archived. No new replies allowed.