Working on a program that has a diner menu in a struct.

Hello everyone. I am working on a program that creates a diner menu. The struct contains the food item and the price. I need some help making an array of structs. So lets say their are 11 items i want the array to repeat that struct 11 times once for each item. Also the user is able to change the menu in the .txt file so the array cant be locked in at a set amount of items. I am working on this program with other people so the code might not make sense because little bits keep getting uploaded and we each have separate parts we are working on. I need help making an array of structs for as many item the user want to put in the menu.

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
 #include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

using namespace std;

void getCheck(double& totalPrice, menu[], int a);

struct menu{
	string menuitem;
	double menuprice;
};


int main()
{
	cout << "**************************************"     << endl ;
	cout <<           "Blocks Restaurant"                << endl;
	cout << "**************************************\n\n" << endl;






	ifstream infile;
	infile.open("dinermenu.txt");

   menu menulist[11];

  




	return 0;
}


void getCheck(double & totalPrice, menu[], int a)
{
	for (int i = 0; i < a; i++)
	{
		totalPrice = totalPrice + (menu.price[i] * menu.num_ordered[i]);
	}
}


Last edited on
use std::vector for dynamic arrays:

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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

using namespace std;

void getCheck(double& totalPrice, menu[], int a);

struct menu {
	string menuitem;
	double menuprice;
};

int main()
{
	cout << "**************************************"     << endl ;
	cout <<           "Blocks Restaurant"                << endl;
	cout << "**************************************\n\n" << endl;

	ifstream infile;
	infile.open("dinermenu.txt");

        std::vector<menu> menulist;
        
        while(infile.good())
        {
            menulist.push_back(menu());
            infile >> menulist.back().menuitem>> menulist.back().menuprice;
        }
    return 0;
}


void getCheck(double & totalPrice, const std::vector<menu>& menu_list, int a)
{
	for (int i = 0; i < menu_list.size(); i++)
	{
		totalPrice = totalPrice + (menu.price[i] * menu.num_ordered[i]);
	}
}


But please be more specific with your question.
Last edited on
So i have a text file with different food items and their corresponding prices. I made one struct that gets the name and price of the item. I want to repeat that struct for every item on the menu. I am trying to repeat the struct using an array as per my teachers directions. The problem is i am confused on how to make an array repeat the struct.
does that help clarify?
Also the user is able to change the menu in the .txt file so the array cant be locked in at a set amount of items.
... using an array as per my teachers directions.

Your teacher is an idiot.
arrays aren't resizeable so you have to use a dynamic data structure if the user may change the .txt file.

But let's ignore that for the moment.
So you simply have a text file and want to read the items in an array of menu's?

You should rename your struct because that struct only represents 1 menu_item and so the name is confusing.

you can use a loop and infile >> menulist[i].name >> menulist[i].price; to get the name and the price

1
2
3
4
for(int i = 0; i < 11; ++i)
{
    infile >> menulist[i].name >> menulist[i].price;
}
ok so i have a new problem with the things underlined. getdata says 18 IntelliSense: more than one instance of overloaded function "getData" matches the argument list:
function "getData(std::ifstream &inData, <error-type> *theMenu)"
function "getData(std::ifstream &inData, menuType *theMenu)"
argument types are: (std::ifstream, menuType [26]) c:\Users\Owner\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp 46 2 ConsoleApplication1

showmenu says 19 IntelliSense: no instance of overloaded function "showMenu" matches the argument list
argument types are: (menuType) c:\Users\Owner\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp 54 3 ConsoleApplication1

menulist says 20 IntelliSense: no suitable conversion function from "menuType" to "menuType *" exists c:\Users\Owner\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp 57 19 ConsoleApplication1


I cannot find any problems with the code.

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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>

using namespace std;

const int a = 26; 
// This global constant should always be equal to the number of items on the menu.
// If the menu is altered, adjust ?a? accordingly. 

void getData(ifstream & inData,menuType theMenu[]);
void showMenu(menuType menulist1[]);
void getCheck(checkType & final_check, menuType menu[]);

struct menuType{
	string menuitem;
	double menuprice;
	int num_ordered;
};
struct checkType{
	menuType menu[a];
	double totalPrice;
	double tax;
	double amount_due;
	int customer_num;
};

int main()
{

	cout << "**************************************" << endl;
	cout <<           "Blocks Restaurant" << endl;
	cout << "**************************************\n\n" << endl;





	menuType menulist[a];
	ifstream inFile;
bool done = false;
	inFile.open("dinermenu.txt");
	getData(inFile, menulist);
	checkType check;
	check.customer_num = rand() % 100 + 1;
char cont;
	while(!done)
{


showMenu(menulist[a]);


getCheck(check, menulist[a]);
 for(int i = 0; i < a; i++)
{
if(check.menu[i].num_ordered > 0)
{
cout << check.menu[i].num_ordered << " " << check.menu[i].menuitem << " $" << check.menu[i].menuprice << endl;
}
}

check.customer_num++;
cout << "Would you like to order again? (Y/N)" << endl;
cin >> cont;
if (cont == 'n' || cont == 'N')
{
done = true;
}



system("cls");
}
inFile.close();
	return 0;
}



void getData(ifstream & inData, menuType theMenu[])
{
	for (int i = 0; i < a; i++)
		inData >> theMenu[i].menuitem >> theMenu[i].menuprice;
}


void getCheck(checkType & final_check, menuType menu[])
{

	for (int i = 0; i < a; i++)
	{
		final_check.totalPrice = final_check.totalPrice + (menu[i].menuprice * menu[i].num_ordered);
	
final_check.menu[i] = menu[i];
final_check.menu[i].menuprice = menu[i].menuprice * menu[i].num_ordered;


	}
final_check.tax = final_check.totalPrice * 0.08;
final_check.amount_due = final_check.tax + final_check.totalPrice;
}

void showMenu(menuType menulist1[])
{ for (int i=0; i < a; i++)

{ 
  
	cout << setw (30)<< left << menulist1[i].menuitem  << "$";
	cout << right << menulist1[i].menuprice << "\n" << endl;

}
Last edited on
declare the struct above your function declerations.
The compiler does not find the type menuType because it is declared AFTER the function decleration.
1
2
3
4
5
6
7
8
9
struct menuType{
	string menuitem;
	double menuprice;
	int num_ordered;
};

void getData(ifstream & inData,menuType theMenu[]);
void showMenu(menuType menulist1[]);
void getCheck(checkType & final_check, menuType menu[]);
cool thanks, that got rid of the problem with the getdata function. Now i get these errors.

Error 1 error C2664: 'void showMenu(menuType [])' : cannot convert argument 1 from 'menuType' to 'menuType []' c:\users\david\documents\visual studio 2013\projects\final project\final project\source.cpp 61 1 Final project

Error 2 error C2664: 'void getCheck(checkType &,menuType [])' : cannot convert argument 2 from 'menuType' to 'menuType []' c:\users\david\documents\visual studio 2013\projects\final project\final project\source.cpp 64 1 Final project

3 IntelliSense: no suitable conversion function from "menuType" to "menuType *" exists c:\Users\David\Documents\Visual Studio 2013\Projects\Final project\Final project\Source.cpp 61 12 Final project

4 IntelliSense: no suitable conversion function from "menuType" to "menuType *" exists c:\Users\David\Documents\Visual Studio 2013\Projects\Final project\Final project\Source.cpp 64 19 Final project
any ideas?
yeah, you only pass 1 item of the array, you need to pass the whole array if you need all items:
1
2
53: showMenu(menulist[a]);
61: getCheck(check, menulist[a]);
Topic archived. No new replies allowed.