Question on using structs and functions

I have been working on a project for a programming class. The problem is as follows:

"Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:
a. Show the customer the different breakfast items offered by the restaurant
b. Allow the customer to select more than one item from the menu.
c. Calculate the bill

The problem provides a list of food items and prices.

Define a struct menuItemType with two components to hold the name and price of an item. Use an array, menuList, of the menuItemTypes. Your program must contain at least the following functions:
-Function GetData: This function loads data into the array
-Function ShowMenu: Displays items offered by restaurant and tells user how to select items.
-Function printCheck: calculates and prints check(Note that the billing amount should include a 5% tax"

I am mainly stuck on this issue: how do I transfer information on which items the user selected from showMenu() into printCheck() in order to list and add up just those item prices in the check? I think my getData and showMenu functions are written correctly, as well as the rest of the program, but I am not sure what to do about printCheck. If you could provide any helpful advice on what logic to follow or send some code for this, I would be deeply grateful. Does this require an extra function?

I also cannot make the getData function work yet, it only reads in the first item and price but not the rest.
Here is relevant code I've written so far:

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
 void getData(menuType menuList[], int counter)
{
	while (!inData.eof())
	{
	inData>>menuList[counter].name>>menuList[counter].itemPrice;
	counter++; }
}

void showMenu(menuType menuList[], char selection, int counter)
{
	cout<<"This menu includes the following: "<<endl;
	cout<<fixed<<showpoint<<setprecision(2);
	cout<<menuList[counter].name<<setw(2 <<menuList[counter].itemPrice<<endl;

	do {
	cout<<"Enter the capital first letter of the item you want."<<endl;
	cout<<"Press 'N' for chicken kabob."<<endl;
	cout<<"Lastly, press 'Z' when you are finished."<<endl;
	
	cin>>selection;
	switch(selection)
	{
		case 'F':
		cout<<menuList[0].name<<"  "<<menuList[0].itemPrice<<endl;
		break;
		case 'C':
		cout<<menuList[1].name<<"  "<<menuList[1].itemPrice<<endl;
		break;
		case 'R':
		cout<<menuList[2].name<<"  "<<menuList[2].itemPrice<<endl;
		break;

		case 'N':
		cout<<menuList[3].name<<"  "<<menuList[3].itemPrice<<endl;
		break;
		case 'B':
		cout<<menuList[4].name<<"  "<<menuList[4].itemPrice<<endl;
		break;
		case 'L':
		cout<<menuList[5].name<<"  "<<menuList[5].itemPrice<<endl;
		break;

		case 'G':
		cout<<menuList[6].name<<"  "<<menuList[6].itemPrice<<endl;
		break;
		case 'S':
		cout<<menuList[7].name<<"  "<<menuList[7].itemPrice<<endl;
		break;
		default:
		cout<<"Invalid selection, try again please."<<endl;
			} //end of switch-case
		} while (selection != 'Z');
}

void printCheck(menuType menuList[], int counter)
{

}


I did not include the input file itself here. Thank you very much for any hints or help in advance!

Best,
Kei
If you know about classes i give you this example i hate to say this but it is in spanish :
To do what you want whitout classes you must to create a int bill that incresses his value in each switch that you made; i hope you this give you some ideas:


/* Este programa nos sirve para automatizar el menu en un restaurante y generar facturas*/
#include <iostream>
#include <cstring>
using namespace std;
class menu{
protected:
int precio;
char nombre[30];
public:
menu(int a, char *b){precio=a;strcpy(nombre,b);}
void show() {cout<<nombre<<"\t........................."<<precio<<" $"<<endl;}
int precios(){return precio;}
void nombres(){cout<<nombre;}
};
class comida:public menu{

public:
comida(int a, char *b):menu(a,b){}
void show() {cout<<nombre<<"........................."<<precio<<" $"<<endl;}
};

void showmenu(menu*ob[]){
for(int a=0;a<10;a++){cout<<a+1<<".-";
ob[a]->show();}
cout<<endl<<endl;
}
void print(menu*ob[],int e,int cuenta){
cout<<" MESON TAURINO "<<endl;
cout<<" Nota de Consumo "<<endl;
cout<<endl;
cout<<" Concepto Precio "<<endl;
cout<<endl;
for(int x=0;x<e;x++){
cout<<" ";
ob[x]->nombres();
cout<<" ";
cout<<ob[x]->precios()<<" $ "<<endl;
}
cout<<endl;
cout<<" Subtotal "<<cuenta<<endl;
cout<<" IVA "<<cuenta*.16<<endl;
cout<<" Total "<<cuenta*1.16<<endl;
}

void getdata(int a[],int b){
int c=0;
a[c]=b;
c++;}

int main()
{
int a,cuenta;
int subtotal[1000];
cuenta=0;
int e;
char d;
e=0;

menu *menus[10];
menu *factura[30];
menus[0]=&comida(90,"Milanesa ");
menus[1]=&comida (95,"Pescado a la diabla");
menus[2]=&comida (60,"Huevos con jamon ");
menus[3]=&comida (70,"Sopa minestron ");
menus[4]=&comida (78,"Mole de olla ");
menus[5]=&comida (80,"Posole ");
menus[6]=&comida (43,"Helado de vainilla ");
menus[7]=&comida (16,"Refresco ");
menus[8]=&comida (24,"Cerveza ");
menus[9]=&comida (18,"Agua de sabor ");
cout<<"//////////////////////////////////////////////////////"<<endl<<endl;
cout<<" Bienvendio al Meson Taurino"<<endl<<endl;
cout<<"//////////////////////////////////////////////////////"<<endl<<endl;
showmenu(menus);
do{cout<<"Seleccione el numero del platillo que desea: "<<endl;
cin>>a;
switch(a){
case 1:{ cuenta+=menus[0]->precios();factura[e]=menus[0];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[0]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 2:{ cuenta+=menus[1]->precios();factura[e]=menus[1];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[1]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 3:{ cuenta+=menus[2]->precios();factura[e]=menus[2];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[2]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 4:{ cuenta+=menus[3]->precios();factura[e]=menus[3];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[3]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 5:{ cuenta+=menus[4]->precios();factura[e]=menus[4];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[4]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 6:{ cuenta+=menus[5]->precios();factura[e]=menus[5];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[5]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 7:{ cuenta+=menus[6]->precios();factura[e]=menus[6];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[6]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 8:{ cuenta+=menus[7]->precios();factura[e]=menus[7];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[7]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 9:{ cuenta+=menus[8]->precios();factura[e]=menus[8];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[8]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;
case 10:{ cuenta+=menus[9]->precios();factura[e]=menus[9];e++;
cout<<"Un momentos por favor enseguida traeremos su ";
menus[9]->nombres();
cout<<endl<<"Su cuenta hasta el momentos es de: "<<cuenta<<" $";
cout<<endl<<"¿Desea ordenar otra cosa? (s/n)"<<endl;
cin>>d;}
break;}
}
while(d!='n');
cout<<endl<<endl;
cout<<"//////////////////////////////////////////////////////"<<endl<<endl;
cout<<"¿Desea que le imprimamos su nota de consumo? (s/n)"<<endl;
cin>>d;
if(d=='s'){print(factura,e,cuenta);getdata(subtotal,cuenta);
}
else {cout<<endl<<endl<<"GRACIAS POR SU VISITA!!!!";}
if(d=='s'){cout<<endl<<endl;
cout<<"//////////////////////////////////////////////////////"<<endl<<endl;
cout<<" GRACIAS POR SU VISITA!!!!"<<endl<<endl;
cout<<"//////////////////////////////////////////////////////"<<endl<<endl;}
system("pause");
return 0;
}

1.-I forgot if you want to do the switch whitout classes here is the example:

int bill;
bill=0;

switch(a)
case 1:cout<<"One minute please"; bill+=120;
break;
case 2:cout<<"One minute please"; bill+=40;
break;

or

switch(a)
case 1:cout<<"One minute please"; bill+=menuprice[0];
break;
case 2:cout<<"One minute please"; bill+=menuprice[1];
break;

then in the end of all of it, you have the right bill;

2.- If you want to store each price and food that you order then:

int a;
switch(a)
case 1: menubill[a]=menulist[0];pricebill[a]=pricelistlist[0];a++;
break;
case 2: menubill[a]=menulist[1];pricebill[a]=pricelistlist[1];a++;
break;

if you look in the example each time you choose a switch a is incremented and you store the info in that array or pointer array.

3.- The las part for the bill is to create a good format; remember to use a do while loop and you will be ok.

sorry for the bad english and good luck!

Topic archived. No new replies allowed.