Editing data

I am writing a program that will allow a user to edit hard coded data from an array. my issue is I don't know how to get the information to be saved and not just reset once the function has been exited.

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//Farncisco Diaz
//May 23, 2016
//CIS-17A: C++ Programming

#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<Windows.h>

using namespace std;

void mainMenu();
void menuOption(int, string);

void main()
{

	
	double price = 0;
	char select;
	//Sleep(120);

	mainMenu();

	do
	{
	cout<<"Would you like to return to the main menu(Y\\N)?";
	cin>>select;
	
	tolower(select);
	
	if( select !='n' )
	{
		mainMenu();
	}
	else
	{
		cout<<"Thank you for Choosing The Daily Digest Please Come Back Soon"<<endl;
	}
	}while(select !='n');

	system("pause"); 
}




void mainMenu()
{

	//Declarations
	int option = 2;
	int const exitmenu = 3;
	

	//exit funtion string 
	string goodBye = "Thank you for chosing The Daily Digest\n\t\tGOODBYE !!! (: . . .  ";
	

	//Print Menu
	cout<<"Hello and welcome to The Daily Digest"<<endl;
	cout<<"Order your daily serving of fruits or vegies and they will arive at your doorstep"<<endl
		<<"the very next day!"<<endl
		<<"**your PC's location settings must be turned on(during time of order) in order to recieve your delivery**"<<endl
		<<"**payments will be made in perocn to delivery personel**"<<endl<<endl;
		
		do
		{
		//Print only if looped 
		if(option<1 || option>3)
		{
		cout<<"Please type a number 1-3 to select the corresponding menu option"
			<<endl<<"if you wish to leave please input 3"<<endl;
		}
		//Print everytime
		cout<<"1.Find Food"<<endl;
		cout<<"2.Edit Item"<<endl;
		cout<<"3.Exit"<<endl<<endl;
		cin>>option;
		}while(option<1 || option>3);
	
		//Exit function 
	if(option == exitmenu)
	{
		system("CLS");
		cout<<endl<<"\t\t";

		for(int i=0; i < goodBye.size();i++)
		{
			cout<<goodBye.substr(i,1);
			Sleep(70);
		}

		exit(0);
	}

	menuOption(option, goodBye);

}

void menuOption(int option, string goodBye)
{
	int const prices = 1;	
	int const availableItems = 2;
	int const menu = 3;	

	string boxType[]={"Vegie", "Fruit", "Nut", "Grain", "Combo"};
	double boxPrice[]={9.99, 9.99, 6.99, 7.99, 15.50};
	string itemNum[]={"IT01","IT02","IT03","IT04","IT05"};
	
	string item;
	int edit;

	switch(option)
		{
		//Find Food
		case 1:
			{
				system("CLS");

				cout<<"\tWhat would you like to Find?"<<endl<<endl;
				cout<<"1.Prices"<<endl;
				cout<<"2.Available items"<<endl;
				cout<<"3.Return to Main Menu"<<endl;
				cout<<endl;
				cin>>option;
				if (option == prices)
				{
					for(int i=0; i < 5; i++)
					{
						cout<<boxType[i]<<" Box $"<<boxPrice[i]<<endl<<endl;
						
					}
					system("Pause");
				}
				else if(option == availableItems)
				{
					for(int i =0 ; i<5 ; i++)
					{
						cout<<boxType[i]<<" Box"<<endl;				
					}
					system("Pause");
				}

				else if(option == menu)
				{
					system("CLS");
					mainMenu();
				}
			}
		break;
		
		//Edit food 
		case 2:
			{
				system("cls");
				cout<< "Item ID: ";
				cin>>item;

				for(int i = 0; i < 5; i++)
				{
					if(!item.compare(itemNum[i]))
					{
					cout<<"-------------------------------------\n\t"<<itemNum[i]<< "  " <<boxType[i]<< " Box $"
						<<boxPrice[i]<< "\n------------------------------------- " <<endl<<endl;
					
					//Ask user if they would like to edit item.
					cout<<"\tWhat would you like to make changes to?"<<endl
						<<"\t1. Box Type"<<endl
						<<"\t2. Price"<<endl
						<<"\t3. Both"<<endl<<"\t";
					cin>>edit;

						if(edit == 1)
						{
							cout<<"\tWhat is the new Box Type?"<<endl
								<<"\t**Do not use Spaces**\n\t";
							cin>>boxType[i];
						}
						else if (edit == 2)
						{
							cout<<"\tWhat is the new price?";
							cin>>boxPrice[i];

						}
						else if (edit == 3 )
						{
							cout<<"\tWhat is the new Box Type?"<<endl
								<<"\t**Do not use Spaces**\n\t";
							cin>>boxType[i];
							cout<<"\tWhat is the new price?";
							cin>>boxPrice[i];
						}
						else if(edit == 4 )
						{
							system("CLS");
							cout<<endl<<"\t\t";

							for(int i=0; i < goodBye.size();i++)
							{
								cout<<goodBye.substr(i,1);
								Sleep(70);
							}

								exit(0);
						}
						cout<<"-------------------------------------\n\tNew Item: "<<itemNum[i]<< "  " <<boxType[i]<< " Box $"
						<<boxPrice[i]<< "\n-------------------------------------" <<endl;
					
							cout<<endl<<"\t\t\n\n";

							for(int i=0; i < goodBye.size();i++)
							{
								cout<<goodBye.substr(i,1);
								Sleep(70);
							}

								exit(0);
					}
				}

			}
			break;

		}
}

Last edited on
Lines 108 - 110 need to be static. Right now, every time you enter the function, these variables get created and populated. If you want the values to maintain their values over multiple function calls, you need to declare the variables as static.

static double boxPrice[]={9.99, 9.99, 6.99, 7.99, 15.50};
Thank you
i have one more issue
its telling me that my function void makePurchase is overloaded i dont know why ive only created one prototype and only called it once;

the issue only arises when i try to pass

static double boxPrice[]={9.99, 9.99, 6.99, 7.99, 15.50};



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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//Farncisco Diaz
//May 23, 2016
//CIS-17A: C++ Programming

#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<Windows.h>

using namespace std;


void mainMenu();
void menuOption(int, string);
void makePurchase(string [],double []);
double getAverage(double, double);

void main()
{

	
	double price = 0;
	char select;
	//Sleep(120);

	mainMenu();

	do
	{
	cout<<"Would you like to return to the main menu(Y\\N)?";
	cin>>select;
	
	tolower(select);
	
	if( select !='n' )
	{
		system("cls");
		mainMenu();
	}
	else
	{
		cout<<"Thank you for Choosing The Daily Digest Please Come Back Soon"<<endl;
	}
	}while(select !='n');

	system("pause"); 
}




void mainMenu()
{

	//Declarations
	int option = 2;
	int const exitmenu = 4;
	

	//exit funtion string 
	string goodBye = "Thank you for chosing The Daily Digest\n\t\tGOODBYE !!! (: . . .  ";
	

	//Print Menu
	cout<<"Hello and welcome to The Daily Digest"<<endl;
	cout<<"Order your daily serving of fruits or vegies and they will arive at your doorstep"<<endl
		<<"the very next day!"<<endl
		<<"**your PC's location settings must be turned on(during time of order) in order to recieve your delivery**"<<endl
		<<"**payments will be made in perocn to delivery personel**"<<endl<<endl;
		
		do
		{
		//Print only if looped 
		if(option<1 || option>4)
		{
		cout<<"Please type a number 1-3 to select the corresponding menu option"
			<<endl<<"if you wish to leave please input 3"<<endl;
		}
		//Print everytime
		cout<<"1.Find Food"<<endl;
		cout<<"2.Edit Item"<<endl;
		cout<<"3.Make Purchase"<<endl;
		cout<<"4.Exit"<<endl<<endl;
		cin>>option;
		}while(option<1 || option>4);
	
		//Exit function 
	if(option == exitmenu)
	{
		system("CLS");
		cout<<endl<<"\t\t";

		for(int i=0; i < goodBye.size();i++)
		{
			cout<<goodBye.substr(i,1);
			Sleep(70);
		}

		exit(0);
	}

	menuOption(option, goodBye);

}

void menuOption(int option, string goodBye)
{
	int const prices = 1;	
	int const availableItems = 2;
	int const menu = 3;	

	static string boxType[]={"Vegie", "Fruit", "Nut", "Grain", "Combo"};
	static double boxPrice[]={9.99, 9.99, 6.99, 7.99, 15.50};
	static string itemNum[]={"IT01","IT02","IT03","IT04","IT05"};
	
	string item;
	int edit;

	switch(option)
		{
		//Find Food
		case 1:
			{
				system("CLS");

				cout<<"\tWhat would you like to Find?"<<endl<<endl;
				cout<<"1.Prices"<<endl;
				cout<<"2.Available items"<<endl;
				cout<<"3.Return to Main Menu"<<endl;
				cout<<endl;
				cin>>option;
				if (option == prices)
				{
					for(int i=0; i < 5; i++)
					{
						cout<<boxType[i]<<" Box $"<<boxPrice[i]<<endl;
						
					}
					cout<<endl;
					system("Pause");
				}
				else if(option == availableItems)
				{
					for(int i =0 ; i<5 ; i++)
					{
						cout<<boxType[i]<<" Box"<<endl;				
					}
					system("Pause");
				}

				else if(option == menu)
				{
					system("CLS");
					mainMenu();
				}
			}
		break;
		
		//Edit food 
		case 2:
			{
				system("cls");
				cout<< "Item ID: ";
				cin>>item;

				for(int i = 0; i < 5; i++)
				{
					if(!item.compare(itemNum[i]))
					{
					cout<<"-------------------------------------\n\t"<<itemNum[i]<< "  " <<boxType[i]<< " Box $"
						<<boxPrice[i]<< "\n------------------------------------- " <<endl<<endl;
					
					//Ask user if they would like to edit item.
					cout<<"\tWhat would you like to make changes to?"<<endl
						<<"\t1. Box Type"<<endl
						<<"\t2. Price"<<endl
						<<"\t3. Both"<<endl<<"\t";
					cin>>edit;

						if(edit == 1)
						{
							cout<<"\tWhat is the new Box Type?"<<endl
								<<"\t**Do not use Spaces**\n\t";
							cin>>boxType[i];
						}
						else if (edit == 2)
						{
							cout<<"\tWhat is the new price?";
							cin>>boxPrice[i];

						}
						else if (edit == 3 )
						{
							cout<<"\tWhat is the new Box Type?"<<endl
								<<"\t**Do not use Spaces**\n\t";
							cin>>boxType[i];
							cout<<"\tWhat is the new price?";
							cin>>boxPrice[i];
						}
						else if(edit == 4 )
						{
							system("CLS");
							cout<<endl<<"\t\t";

							for(int i=0; i < goodBye.size();i++)
							{
								cout<<goodBye.substr(i,1);
								Sleep(70);
							}

								exit(0);
						}
						cout<<"-------------------------------------\n\tNew Item: "<<itemNum[i]<< "  " <<boxType[i]<< " Box $"
						<<boxPrice[i]<< "\n-------------------------------------" <<endl;
					
							
					}
				}

			}
			break;

			case 3:
				{
					makePurchase(boxType,boxPrice);
				}
		}
}


void makePurchase(string boxType[],boxPrice[])
{
	
	
	char again;
	int buy, quantity,total, average;
	double	totalQuantity=0.00, completeTotal=0.00;

	do
	{
	cout<<"what would you like to purchase?";
	cout<<endl;
	for(int i=0;i<5;i++)
	{
		cout<<(i+1)<<". "<<boxType[i]<<"Box $"<<boxPrice[i]<<endl;

	}

	cin>>buy;
		while(buy<1 || buy>5)
		{
			cout<<"Please input a number 1 - 5 to select its corresponding menu option";
			cin>>buy;
		}
	
	
	cout<<"how many would you like ? ";
	cin>>quantity;
		while(quantity<1)
		{
			cout<<"You can not buy a negative quantity"
				<<"\nPlease enter an actual amount";
		}


	totalQuantity += quantity;
	
	switch(buy)
	{
	
	case 1:
		{
			total = quantity*boxPrice[0];
		}
		break;
	
	case 2:
		{
			total = quantity*boxPrice[1];
		}
		break;
	
	case 3:
		{
			total = quantity*boxPrice[2];
		}
		break;
	
	case 4:
		{
			total = quantity*boxPrice[3];
		}
		break;
	
	case 5:
		{
			total = quantity*boxPrice[4];
		}
		break;
	}

	completeTotal += total;
	
	cout<<"Your Total is: $"<<completeTotal<<endl;
	cout<<"Would you like to purchase another box?(Y\\N) ";
	cin>>again;

	tolower(again);

	}while(again != 'n');

	average=getAverage(totalQuantity,completeTotal);

	cout<<setprecision(2)<<showpoint;
	cout<<"Your Complete Total is: $"<<completeTotal<<endl
		<<"Your average is: $"<<average<<" Per Box"<<endl<<endl;

	system("pause");
}

double getAverage(double quantity, double total)
{
	return total/quantity;
}
Last edited on
nevermind after posting i saw my mistake... thank you again(:
Topic archived. No new replies allowed.