Putting infile data into an array, inside a struct?

I'm getting confused here. I'm supposed to put what's in my infile (menuData) into an array called menu, which contains a struct called menuItemType.

Here's the struct, declarations (made in main), and function call/function. Any help would be greatly appreciated.

1
2
3
4
5
6
7
  
const int NO_OF_MENU_ITEMS = 8;

struct menuItemType {
	string menuItem;
	double menuPrice;
};


1
2
3
4
5
int choices[NO_OF_MENU_ITEMS][2];
	menuItemType menu[NO_OF_MENU_ITEMS];
	int choiceList[NO_OF_MENU_ITEMS][2]; 
	int choiceListLength;
	ifstream inFile;


1
2
//function call
getMenuFromFile(inFile, menu, choiceListLength);


1
2
3
4
5
6
7
void getMenuFromFile(ifstream& inFile, menuItemType menu[], int menuSize)
{
	

		

	}
Anyone? Please?
Maybe something like this:
1
2
3
4
5
6
7
8
9
10
11
12
void getMenuFromFile(ifstream& inFile, menuItemType menu[], int menuSize)
{
	for(int i = 0; (i < menuSize); ++i)
{
  if(inFile >> menu[i].menuItem >> menu[i].menuPrice)
    ; // ++menu_items_read
  else
    break;
}
		

	}
It depends on how the file is organized.

Plus: you need another variable that receives the number of items actually read.
This is the file:

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
Instead of using operator>> you may use getline:

http://www.cplusplus.com/reference/string/string/getline/

like this:
1
2
getline(inFile, menu[i].menuItem, '$');
inFile >> menu[i].menuPrice;


Note that the operator>> will leave the end of line in the stream (which will be fetched by getline(...)). Remove the the end of line with ignore(...):

http://www.cplusplus.com/reference/istream/istream/ignore/

Also note that menu[i].menuItem may contain white spaces at the end. You can detect it with isspace(...):

http://www.cplusplus.com/reference/cctype/isspace/?kw=isspace

and remove them with pop_back(...):

http://www.cplusplus.com/reference/string/string/pop_back/
Thank you, but now I'm having some trouble taking in the user input when they order from the menu. (maxorder is a constant int with a value of 50.) It has problems with the equal signs. Any tips as to why? The main types of errors are

"error C2040: '==' : 'int [][2]' differs in levels of indirection from 'int'."

"error C2446: '==' : no conversion from 'int' to 'int [][2]'"

"operand types are incompatible ("int (*)[2]" and "int")"



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
void makeSelection(menuItemType menu[], int menuSize, int choices[][2], int &choiceSize)
{

	double price = 0;
	string name;

	showMenu(menu, menuSize);

	cout << "What would you like to order? You can order up to 50 items. Type 'done' when you're finished ordering." << endl;

	for (int i = 0; i < maxorder; i++){

		cin >> choices[i][2];

	while (cin >> choices[i][2]) {
		
		showMenu(menu, menuSize);
		if (choices == 1)
		{
			price = 1.45;
			name = "Plain Egg";
		}
		else if (choices == 2)
		{
			price = 2.45;
			name = "Bacon and Egg";
		}
		else if (choices == 3)
		{
			price = 0.99;
			name = "Muffin";
		}
		else if (choices == 4)
		{
			price = 1.99;
			name = "French Toast";
		}
		else if (choices == 5)
		{
			price = 2.49;
			name = "Fruit Basket";
		}
		else if (choices == 6)
		{
			price = 0.50;
			name = "Cereal";
		}
		else if (choices == 7)
		{
			price = 0.50;
			name = "Coffee";
		}
		else if (choices == 8)
		{
			price = 0.75;
			name = "Tea";
		}
		else if (choices == "done")
		{
			break;
		}
		else
			cout << "\n Please input an order number or 'done' if you're finished.\n";
			continue;
		}
	}
}
Why is choices a 2 dimensional array?

Since choices is an array you cannot compare it with a value. Neither int nor string.

cin >> choices[i][2]: 2 is out of bounds. Only indexes 0 and 1 are allowed for an array with 2 elements.

What are the cascade of if good for? Local variables are dismissed at the end of the function.
Choices is a 2-dimensional array because that's what it has to be for the assignment.
Well, you should ask your teacher for the reason why.

My guess is that customer is supposed to have 2 choices per order:
One for food and one for drink.

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
void makeSelection(menuItemType menu[], int menuSize, int choices[][2], int &choiceSize)
{
	showMenu(menu, menuSize);

	cout << "What would you like to order? You can order up to 50 items. Type 'done' when you're finished ordering." << endl;

	for (int i = 0; i < maxorder; i++){

	string order;
	cout << "Food: " << endl;

	int j = 0;
	for(; j < 2; ++j)
	{
		cin >> order;
		if("done" == order)
		  break;
		else
		  choices[i][j] = std::stoi(order);

		cout << "Drink: " << endl;
	}
	if(j > 0)
	  ++choiceSize;
	if("done" == order)
	  break;
	}
...
}
Note that you need to check whether the user input is correct.

For stoi see:
http://www.cplusplus.com/reference/string/stoi/?kw=stoi

Further more I'd guess that the content of choices is and index in menu
According to the assignment, choices is 2-dimensional because it has "8 rows and 2 columns. Each row will refer to one menu item the customer picked; the first column will show the item number in the menu and the second column will show the number of pieces the customer ordered from that item."
Actually, she just sent the class an email giving more detail about it.

"User will make choices from the menu and these choices will be recorded in the choices array. For instance, if the user makes only one choice from the menu, first row of the choices array will be updated. If the user makes 3 choices from the menu first 3 rows of the choices array will be updated. The user can make up to 8 choices from the menu (note that the choices array has only 8 rows)."
Just the question changes and the processing later.
Topic archived. No new replies allowed.