struct input error

First I want to say thanks to all the people that helped before! I have a new problem. I am creating a text based point of sales system. The code I have so far compiles okay but the struct is messed up. I have input that I think I did right but when its printed back out it's all garbage. please help!! thanks!

here is the code for what I have, 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
#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int num = 8;

struct menuItemType
{
	string menuItem;
	double menuPrice;
};

void getData (struct menuItemType);
void showMenu (struct menuItemType);
void printCheck (struct menuItemType);

int main()
{
	int i = 0;
	int responce;
	vector<int>purchased;
	menuItemType breakfastMenu[num];
	getData (breakfastMenu[num]);
	
	cout << "**************************" << endl;
	cout << " Welcome to Joe's Dinner!" << endl;
	cout << "**************************" << endl;
	cout << "What would you like for breakfast?" << endl;
	
	showMenu (breakfastMenu[num]);
	
	cout << endl;
	cout << "To place an order simply input the" << endl;
	cout << "number of the item you wish to order." << endl;
	cin >> responce;
	switch (responce)
	{
		case 0:
			purchased.push_back(1.45);
			break;
		case 1:
			purchased.push_back(2.45);
			break;
		case 2:
			purchased.push_back(0.99);
			break;
		case 3:
			purchased.push_back(1.99);
			break;
		case 4:
			purchased.push_back(2.49);
			break;
		case 5:
			purchased.push_back(0.69);
			break;
		case 6:
			purchased.push_back(0.50);
			break;
		case 7:
			purchased.push_back(0.75);
			break;
		default:
			cout << "Sorry I didn't understand, please try again" << endl;
			break;
	}

return 0;
}

void getData (struct menuItemType)
{
	struct menuItemType breakfastMenu[num];
	breakfastMenu[0].menuItem = "Plain Egg";
	breakfastMenu[1].menuItem = "Bacon and Egg";
	breakfastMenu[2].menuItem = "Muffin";
	breakfastMenu[3].menuItem = "French Toast";
	breakfastMenu[4].menuItem = "Fruit Basket";
	breakfastMenu[5].menuItem = "Ceral";
	breakfastMenu[6].menuItem = "Coffee";
	breakfastMenu[7].menuItem = "Tea";
	breakfastMenu[0].menuPrice = 1.45;
	breakfastMenu[1].menuPrice = 2.45;
	breakfastMenu[2].menuPrice = 0.99;
	breakfastMenu[3].menuPrice = 1.99;
	breakfastMenu[4].menuPrice = 2.49;
	breakfastMenu[5].menuPrice = 0.69;
	breakfastMenu[6].menuPrice = 0.50;
	breakfastMenu[7].menuPrice = 0.75;
}	 

void showMenu (struct menuItemType)
{
	int i = 0;
	struct menuItemType breakfastMenu[num];
	for (i = 0; i < num; i++)
	{
	cout << " " << i << ")  " << breakfastMenu[i].menuItem << "     " << breakfastMenu[i].menuPrice << endl;
	}
}
You need to look up what "scopes of variables" are. When you modify breakfastMenu inside of getData(), you are modifying a local variable with that name, completely separate from the one in main(). The same with your other functions.
Wow two things. First thanks for the super fast response and okay... missed that one cool thanks lol. So I removed the struct's form the definitions but I still get errors I have included the updated code and the errors I get.

I get 2 errors both the same one in 78 and the other in 101 (marked in the code)
"error 'breakfastMenu' was not declared in this scope". PLEASE HELP

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
/*  Breakfast Menu
	By: Luke Covack
	07/06/10
*/
#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int num = 8;

struct menuItemType
{
	string menuItem;
	double menuPrice;
};

void getData (struct menuItemType);
void showMenu (struct menuItemType);
void printCheck (struct menuItemType);

int main()
{
	int i = 0;
	int responce;
	vector<int>purchased;
	menuItemType breakfastMenu[num];
	getData (breakfastMenu[num]);
	
	cout << "**************************" << endl;
	cout << " Welcome to Joe's Dinner!" << endl;
	cout << "**************************" << endl;
	cout << "What would you like for breakfast?" << endl;
	
	showMenu (breakfastMenu[num]);
	
	cout << endl;
	cout << "To place an order simply input the" << endl;
	cout << "number of the item you wish to order." << endl;
	cin >> responce;
	switch (responce)
	{
		case 0:
			purchased.push_back(1.45);
			break;
		case 1:
			purchased.push_back(2.45);
			break;
		case 2:
			purchased.push_back(0.99);
			break;
		case 3:
			purchased.push_back(1.99);
			break;
		case 4:
			purchased.push_back(2.49);
			break;
		case 5:
			purchased.push_back(0.69);
			break;
		case 6:
			purchased.push_back(0.50);
			break;
		case 7:
			purchased.push_back(0.75);
			break;
		default:
			cout << "Sorry I didn't understand, please try again" << endl;
			break;
	}

return 0;
}

void getData (struct menuItemType)
{ // this is line 77
	breakfastMenu[0].menuItem = "Plain Egg";
	breakfastMenu[1].menuItem = "Bacon and Egg";
	breakfastMenu[2].menuItem = "Muffin";
	breakfastMenu[3].menuItem = "French Toast";
	breakfastMenu[4].menuItem = "Fruit Basket";
	breakfastMenu[5].menuItem = "Ceral";
	breakfastMenu[6].menuItem = "Coffee";
	breakfastMenu[7].menuItem = "Tea";
	breakfastMenu[0].menuPrice = 1.45;
	breakfastMenu[1].menuPrice = 2.45;
	breakfastMenu[2].menuPrice = 0.99;
	breakfastMenu[3].menuPrice = 1.99;
	breakfastMenu[4].menuPrice = 2.49;
	breakfastMenu[5].menuPrice = 0.69;
	breakfastMenu[6].menuPrice = 0.50;
	breakfastMenu[7].menuPrice = 0.75;
}	 

void showMenu (struct menuItemType)
{ //this is line 97
	int i = 0;
	for (i = 0; i < num; i++)
	{
	cout << " " << i << ")  " << breakfastMenu[i].menuItem << "     " << breakfastMenu[i].menuPrice << endl;
	}
}
Last edited on
1) Your function definitions (at the bottom), are slightly incorrect. You haven't named what the variable you are passing in is called, so you can't use it in the function.

2) Your functions are only accepting a *single* menuItemType variable, not an entire array. I would suggest turning the array into a vector since it is easier to pass than an array.

3) Your getData function will make a copy of the variable passed to it, which means the changes you make in the function don't actually affect the variable you are passing in. Pass the variable by reference if you want to change it.

1
2
3
4
//example:
void change_int_to_two(int& the_int) {//note the '&'
    the_int = 2; //since we pass by reference, this actually modifies what is passed
}
kk I made some changes and now when I try and compile it I get the following two errors:
error: line 29: undefined reference to 'getData(menuItemType)'
error: line 36: undefined reference to 'showmenu(menuItemType)'

please help, 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
/*  Breakfast Menu
	By: Scott K
	07/06/10
*/
#include <iostream>
#include <vector>
#include <string>

using namespace std;

//const int num = 8;

struct menuItemType
{
	string menuName;
	double menuPrice;
};

void getData (struct menuItemType);
void showMenu (struct menuItemType);
void printCheck (struct menuItemType);

int main()
{
	int i = 0;
	int responce;
	vector<int>purchased;
	menuItemType breakfastMenu[8];
	getData (breakfastMenu[8]);
	
	cout << "*************************" << endl;
	cout << " Welcome to Joe's Diner!" << endl;
	cout << "*************************" << endl;
	cout << "What would you like for breakfast?" << endl;
	
	showMenu (breakfastMenu[8]);
	
	cout << endl;
	cout << "To place an order simply input the" << endl;
	cout << "number of the item you wish to order." << endl;
	cin >> responce;
	switch (responce)
	{
		case 0:
			purchased.push_back(1.45);
			break;
		case 1:
			purchased.push_back(2.45);
			break;
		case 2:
			purchased.push_back(0.99);
			break;
		case 3:
			purchased.push_back(1.99);
			break;
		case 4:
			purchased.push_back(2.49);
			break;
		case 5:
			purchased.push_back(0.69);
			break;
		case 6:
			purchased.push_back(0.50);
			break;
		case 7:
			purchased.push_back(0.75);
			break;
		default:
			cout << "Sorry I didn't understand, please try again" << endl;
			break;
	}

return 0;
}

void getData (struct menuItemType breakfastMenu[8]) 
{
	breakfastMenu[0].menuName = "Plain Egg";
	breakfastMenu[1].menuName = "Bacon and Egg";
	breakfastMenu[2].menuName = "Muffin";
	breakfastMenu[3].menuName = "French Toast";
	breakfastMenu[4].menuName = "Fruit Basket";
	breakfastMenu[5].menuName = "Ceral";
	breakfastMenu[6].menuName = "Coffee";
	breakfastMenu[7].menuName = "Tea";
	breakfastMenu[0].menuPrice = 1.45;
	breakfastMenu[1].menuPrice = 2.45;
	breakfastMenu[2].menuPrice = 0.99;
	breakfastMenu[3].menuPrice = 1.99;
	breakfastMenu[4].menuPrice = 2.49;
	breakfastMenu[5].menuPrice = 0.69;
	breakfastMenu[6].menuPrice = 0.50;
	breakfastMenu[7].menuPrice = 0.75;
}	 

void showMenu (struct menuItemType breakfastMenu[8])
{
	int i = 0;
	for (i = 0; i < 8; i++)
	{
	cout << " " << i << ")  " << breakfastMenu[i].menuName << "     " << breakfastMenu[i].menuPrice << endl;
	}
}
You only changed the signatures in the definitions of your functions. Fix the declarations too and it should be ok.

Also, you should declare purchased like this -> vector<double>purchased;
Last edited on
woot thanks for the fast response and I did make the vector change, don't know why I didn't catch that one earlier. as far as the declarations what changes do I need to make? I am really confused and a little overwhelmed. do I simply need to make them look like the definitions?? if not could someone please post an example.
lukecovack wrote:
do I simply need to make them look like the definitions??

Yes.
kk I changed them to:
1
2
void getData (struct menuItemType breakfastMenu[8]);
void showMenu (struct menuItemType breakfastMenu[8])

now I get:
error: cannot convert 'menuItemType*' for argument '1' to 'void getData(menuItemType*)
in lines 29 and 36
Yes, when you make the call you should just pass breakfastMenu. The type of breakfastMenu is menuItemType[8]. The compiler already knows that, you told him in the declarations/definitions of your functions. What you simply have to do now is:

getData (breakfastMenu);

showMenu (breakfastMenu);

EDIT: breakfastMenu[8] actually is the 9th element of your array (the element past the 8th element) and its type is menuItemType, not what your functions expect. In fact, your functions expect menuItemType* and breakfastMenu is silently converted from menuItemType[8] to menuItemType* when you pass it.

EDIT 2: These may help:

http://cplusplus.com/doc/tutorial/arrays/
http://cplusplus.com/doc/tutorial/pointers/
Last edited on
WOOT thanks a lot I got it working now!!!!! man you guys and gals are awesome!
Topic archived. No new replies allowed.