Problem with my array

In the code I wrote below, when a user selects "See Order" it only works some of the time. It also only adds something to an order some of the time. Says that total and totalwtax are uninitialized. Any help appreciated.


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
170
171
 #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int NUMITEMS = 8;
const double TAX = 1.05;

struct menuItemType
{
	string item;
	double price;
	int quant;
};
 
/*****
Function: getData
Preconditon: none
Postcondition: gets menu data from a file
Date Coded: 4/25/14
*****/
void getData (ifstream& fin, menuItemType menu[], int& numItems);

/*****
Function: showMenu
Precondition: getData
Postcondition: shows items and allows selection
Date Coded: 4/25/14
*****/
int mymenu(menuItemType menu[], int numItems);

/*****
Function: printCheck
Precondition: getdata
Postcondition: calculates tax and prints check
Date Coded: 4/25/14
*****/
double printCheck (double TAX, menuItemType menu[],double total, double totalwtax);

int main ()
{
	menuItemType menu[NUMITEMS];
	int numItems = 0, menuchoice, i;
	double total, totalwtax;
	ifstream fin;
	ofstream fout;
	menu[0].quant = 0;
	menu[1].quant = 0;
	menu[2].quant = 0;
	menu[3].quant = 0;
	menu[4].quant = 0;
	menu[5].quant = 0;
	menu[6].quant = 0;
	menu[7].quant = 0;
	fin.open("menu.txt");

	getData (fin, menu, numItems);
	cout << "Welcome to Johnny's Restaurant" << endl;

	do 
	{
		menuchoice = mymenu(menu, numItems);

		if (menuchoice == 1)
		{	
			menu[0].quant++;
			mymenu(menu, numItems);
		}
		if (menuchoice == 2)
		{
			menu[1].quant++;
			mymenu(menu, numItems);
		}
		if (menuchoice == 3)
		{
			menu[2].quant++;
			mymenu(menu, numItems);
		}
		if (menuchoice == 4)
		{
			menu[3].quant++;
			mymenu(menu, numItems);
		}
		if (menuchoice == 5)
		{
			menu[4].quant++;
			mymenu(menu, numItems);
		}
		if (menuchoice == 6)
		{
			menu[5].quant++;
			mymenu(menu, numItems);
		}
		if (menuchoice == 7)
		{
			menu[6].quant++;
			mymenu(menu, numItems);
		}
		if (menuchoice == 8)
		{
			menu[7].quant++;
			mymenu(menu, numItems);
		}
		if (menuchoice == 9)
		{
			cout << endl;
			cout << "Your order is: " << endl;
			for (i = 0; i<NUMITEMS; i++)
				cout << menu[i].item << right << setw(5) << menu[i].quant << endl;
		}

		if (menuchoice == 10)
		{
			cout << endl;
			cout << "Your order is: " << endl;
			for (i = 0; i<NUMITEMS; i++)
				cout << menu[i].item << right << setw(5) << menu[i].quant << endl;
			cout << endl;
			cout << "Your total with tax is: " << endl;
			totalwtax = printCheck (TAX, menu, total, totalwtax);
			cout << setprecision(2) << totalwtax;
		}

	}while (menuchoice != 11);
	
	return 0;
}

void getData (ifstream& fin, menuItemType menu[], int& numItems)
{
	do
	{
		getline(fin, menu[numItems].item);
		fin >> menu[numItems].price;
		fin.get();
		numItems++;
	}
	while (!fin.eof());
	return;
}

int mymenu(menuItemType menu[], int numItems)
{
	int choice;
	cout << endl;
	cout << left << "1." << menu[0].item << right << setw(5) << "$" << menu[0].price << endl;
	cout << left << "2." << menu[1].item << right << setw(6) << "$" << menu[1].price << endl;
	cout << left << "3." << menu[2].item << right << setw(13) << "$" << menu[2].price << endl;
	cout << left << "4." << menu[3].item << right << setw(7) << "$" << menu[3].price << endl;
	cout << left << "5." << menu[4].item << right << setw(7) << "$" << menu[4].price << endl;
	cout << left << "6." << menu[5].item << right << setw(13) << "$" << menu[5].price << endl;
	cout << left << "7." << menu[6].item << right << setw(13) << "$" << setprecision(4) << menu[6].price << endl;
	cout << left << "8." << menu[7].item << right << setw(16) << "$" << menu[7].price << endl;
	cout << left << "9. See Order" << endl;
	cout << left << "10. Print Check" << endl;
	cout << left << "11. Exit Menu" << endl;
	cout << endl;
	cin >> choice;
	return choice;
}

double printCheck (double TAX, menuItemType menu[], double total, double totalwtax)
{
	
	total = ((menu[0].quant * menu[0].price) + (menu[1].quant * menu[1].price)+ (menu[2].quant * menu[2].price) + (menu[3].quant * menu[3].price)
		+ (menu[4].quant * menu[4].price) + (menu[5].quant * menu[5].price) + (menu[6].quant * menu[6].price) + (menu[7].quant * menu[7].price));
	totalwtax = total * TAX;
	return totalwtax;
}
Hi

You need to ensure that you initialize all variables, either when they are declared are before they are used - my recommendation would be when they are declared as then you get into a practise of doing it.

When the total and totalwtax above are declared, they have an undetermined value until they are assigned a value for the first time. Reading them prior to assigning a value results in undefined behaviour. When the printCheck function is called these variables are still undefined.

You could also refactor the code in the do...while loop into a switch statement something along the lines of:

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
	do 
	{
		menuchoice = mymenu(menu, numItems);

                switch (menuchoice)
                {
                     case 1:
                     case 2:
                     case 3:
                     case 4:
                     case 5:
                     case 6:
                     case 7:
                     case 8:
                         menu[menuchoice - 1].quant++;
                         mymenu(menu, numItems);
                         break;
                     case 9:
                     case 10:
                     {
                          cout << endl;
                          cout << "Your order is: " << endl;
                          for (int i = 0; i < NUMITEMS; i++)     // remove declaration of i at top of code and declare for local scope only
                              cout << menu[i].item << right << setw(5) << menu[i].quant << endl;

                          if (menuchoice == 10)
                          {
                               cout << endl;
                               cout << "Your total with tax is: " << endl;
                               totalwtax = printCheck (TAX, menu, total, totalwtax);  // these variables should be initialized
                               cout << setprecision (2) << totalwtax;
                           }
                           break;
                      }
                 }
	}while (menuchoice != 11);


It is also good practise to have a default case in the switch statement to handle unexpected values.
Topic archived. No new replies allowed.