Pointer to the first struct element

I have two input files. One contains a menu of 20 food items and the other contains orders placed on certain days. I have declared a structure for both. The job of the function header is to provide the price of a given menu item.

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
  
struct Menu_Items
		{
			string item;
			double price;
		};

struct Orders
		{
			string Menu;
			string Item_Ordered;
			string Day_of_Week;
			string CustomerID;
			int quantity;
			int Line_Item_Total;

		};
double GetPrice( Menu_Items *[] , string );
int main()
{

          There is code between but it is irrelevant to question. It merely   
          displays the information of the input files.

return 0;
}
double GetPrice(menu[], string item)
{
	int i = 0;
	for (i = 0; i < NUM_MENU_ITEMS; ++i)
	if (menu[i].item == item)
		return menu[i].price;

}


This function was provided. I needed to code a prototype for the provided GetPrice() Function. The one in the code is the one I have come up with. The function must accept as input parameters a pointer to the first element of the Menu_Items structure along with the current item found in the orders structure.

My question is if I coded the correct prototype.

P.S. I am using Visual Studio2013 C++
Neither line 18 nor 27 are correct.

Line 18: You don't need the * since the [] indicate you're passing an array.
I'm assuming here that you did not dynamically allocate the Menu_Items array since you haven't shown the allocation of the array.

Line 27: You have not specified a type for the menu argument.

So for line 27, would the data type be Menu_Items? Since it will be for that structure.
Correct.
Last question. I was provided with a for loop which will calculate the total price for a certain order.

1
2
3
4
5
6
7
8
 for (i = 0; i < Num_of_Orders; ++i)
		 {
			
			Line_Item_Total = Orders[i].quantity * GetPrice(Menu_Items, Orders) * PA_SALES_TAX;
			Orders[i].Line_Item_Total = Line_Item_Total;
			cout << "The line item total for " << Orders[i].quantity << " " << 
Orders[i].item << " is: $" << setw(2) << fixed << setprecision(2) << Orders[i].Line_Item_Total << endl;
		}


When placed into the code, the sections in bold which are the structure names show up as an error. When I scroll over them it says "Error: type name is not allowed".

Why is this?

Just asking for a hint as to why so I can try and figure it out.
Last edited on
Orders is a type (of struct), not an array. See line 8 in your original post.
You can't index a type name. You have to reference an arrray. Something like:
1
2
3
4
5
  const int max_orders = 10;
  Orders  orders[max_orders];  // lower case used for array name
..
  Line_Item_Total = orders[i].quantity * GetPrice(Menu_Items, orders) * PA_SALES_TAX;
  orders[i].Line_Item_Total = Line_Item_Total;


Looks like you might have changed the second argument to GetPrice. Originally you were passing a string. Now, you appear to be passing the entire orders array. I suspect what you want to pass here is either a specific order, or an element of orders[i]. I'm not clear on the difference between Menu and Item_Ordered.

I did not want to post the entire code but I feel as if it would be easier for you.


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
struct Menu_Items
		{
			string Item;
			double Price;
		};

struct Orders
		{
			string Menu_Item_Ordered;
			string Day_of_Week;
			string CustomerID;
			int Quantity;
			int Line_Item_Total;

		};

const int NUM_MENU_ITEMS = 20;
const int ORDERS = 100;
const double PA_SALES_TAX = 1.06;

double GetPrice( Menu_Items [] , string );
void Biggest_Profit(double []);
int main()
{

	Menu_Items Menu_List[NUM_MENU_ITEMS];
	int i = 0;

	ifstream InFileMenu;
	InFileMenu.open("Menu.txt");
	ifstream InFileOrders;
	InFileOrders.open("Orders.txt");

	

	

	if ( InFileMenu.fail() )
	{
		cout << "The 'Menu.txt' file you have entered cannot be found!!!" << endl;
		system("pause");
	}
	else
	{
		cout << "The 'Menu.txt' file you have enetered was found!!" << endl;
		system("pause");


		string Item;
		double Price;

		while (InFileMenu >> Item >> Price)
		{
			cout << "The Menu item: #" << i + 1 << " is the " << Item << " and it cost: $" << Price << endl;
			Menu_List[i].Ptem = Item;
			Menu_List[i].Price = Price;
			i++;
		}


	}


	Orders Orders_Placed[ORDERS];
	int Orders_Index = 0;
	

	if (InFileOrders.fail())
	{
		cout << "The 'Orders.txt' file you have entered cannot be found!!!" << endl;
		system("pause");
	}
	else
	{
		cout << "The 'Orders.txt' file you have enetered was found!!" << endl << endl;
		system("pause");

		

		string Menu_Item_Ordered, Day_of_Week, Customer_ID;
		int Quantity, Max = 0, Num_of_Orders, Line_Item_Total;
		
		
		while ( InFileOrders >> Menu_Item_Ordered >> Quantity >> Day_of_Week >> Customer_ID )
		{
			
			cout << "Customer: " << Customer_ID << " ordered " << Quantity << " " << Menu_Item_Ordered << " on " << Day_of_Week << "." << endl << endl;

			Orders_Placed[Orders_Index].Menu_Item_Ordered = Menu_Item_Ordered;
			Orders_Placed[Orders_Index].Quantity = Quantity;
			Orders_Placed[Orders_Index].Day_of_Week = Day_of_Week;
			Orders_Placed[Orders_Index].CustomerID = Customer_ID;
			Orders_Index++;

			if (Orders_Index > Max)
			{
				Max = Orders_Index;
				Num_of_Orders = Orders_Index;
			}
			
		}
		cout << "There was a total of " << Num_of_Orders << " orders!!!" << endl;
		system("pause");

		
			
		 for (i = 0; i < Num_of_Orders; ++i)
		 {
			
			Line_Item_Total = Orders_Placed[i].Quantity * GetPrice(Menu_List, Orders_Placed[i]) * PA_SALES_TAX;
			Orders_Placed[i].Line_Item_Total = Line_Item_Total;
			cout << "The line item total for " << Orders_Placed[i].Quantity << " " << Orders_Placed[i].Menu_Item_Ordered << " is: $" << setw(2) << fixed << setprecision(2) << Orders_Placed[i].Line_Item_Total << endl;
		}
	}

	
	









	return 0;
}

double GetPrice( Menu_Items Menu[], string Item)
{
	int i = 0;
	for (i = 0; i < NUM_MENU_ITEMS; ++i)
	if (Menu[i].Item = = Item)
		return Menu[i].Price;
	
}
//void Biggest_Profit(double Orders_Total[])
void Biggest_Profit(double Orders_Total[])
{
	double MonTotals, TueTotals, WedTotals, ThuTotals, FriTotals, SatTotals, SunTotals;
	int i;
	double Winner;

	for (i = 0; i < ORDERS; ++i)
	{
		if (MonTotals < TueTotals)
			Winner = TueTotals;
		else if (WedTotals > Winner)
			Winner = WedTotals;
		else if (ThuTotals > Winner)
			Winner = ThuTotals;
		else if (FriTotals > Winner)
			Winner = FriTotals;
		else if (SatTotals > Winner)
			Winner = SatTotals;
		else if (SunTotals > Winner)
			Winner = SunTotals;
		else if (MonTotals > Winner)
			Winner = MonTotals;
	}


	return;
}






Okay so I changed as you suggested and most of the errors went away. When doing this, the only error I am having is that of the "Orders_Placed". It says " no suitable user-defined conversion from 'Orders' to 'std::string' exists."

So I figured that maybe changing the function parameter from "string" to "Orders" may fix and it did in fact fix that error. But when doing so, another one popped up. The "==" in the function shows an error that say:
"no operator '==' matches these operands operand types are std::string == Orders"

Now this is what I do not understand. If I change it to "Orders", which is a structure, why does it say that I am matching a string and a structure? They are both structures so shouldn't this be fine?
Last edited on
Posting your full code is appreciated as I can paste in in my compiler and duplicate what you are getting.

Line 55: Menu_List[i].Ptem - misspelling

Line 110: You're attempting to pass a struct of type Orders as the second argument to GetPrice. GetPrice is expecting a string. Use Orders_Placed[i].Menu_Item_Ordered instead. This is why the compiler is reporting no suitable user-defined conversion from 'Orders' to 'std::string' exists."

Line 134: Use == (don't put a space between the =).

If I change it to "Orders", which is a structure, why does it say that I am matching a string and a structure? They are both structures so shouldn't this be fine?

No. std::string is a class. You're trying to compare two different object types. The compiler doesn;t know how to do that unless you provide your own comparison routine (overload the == operator).






Thank you much! I have figured it out.

My last question is that regarding the Void function. The function accepts a pointer to the first element of the Orders_Placed array. The function must have a single for loop to cycle through each array element. The loop should contain 7 sections which should process each possible day of the week.

I need to have 7 double variables which which the totals for each day of the week.
The function should tally up the 7 totals and display the most profitable day or week.


How do I accumulate the totals for each day? I'm not sure how this is done within the function.
You need to do the following:
1) Allocate an array of 7 doubles (totals).
2) Initialize the array to zeroes.
3) Iterate through the Order_Totals array, taking one order at a time and adding it's total to the totals for the specific day of that order.
4) Iterate through the totals array keeping track of the index of which entry (day) has the largest total.
5) Return the index of the day with the larest total.

Note: regarding #5, I'm not clear if you intend to return the day of the week with the biggest profit, or just the biggest profit. In eaither case, the function should have the appropriate type so this value can be returned. I also do not asee a call to BigestProfit from main so as currently coded, this function will never execute.
Last edited on
Topic archived. No new replies allowed.