Grocery Program, deleting & searching records and password inputting.

I am creating a grocery list program for buyer and seller. i don't know how to search and delete records when you're a seller. and the program won't let the seller input the password, it jumps to menu without letting it through password entering. Please help me, point out what's wrong with my code and guide me! 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
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "Windows.h"
#include <conio.h>
using namespace std;
struct groList
{
	int id;
	char name[20];
	double price;
	int quantity;
	char type[20];
};

void addRec();
void delRec();
void viewRec();
void srchRec();

int main()
{
	groList groc[1];
	int choice_menu=0;
	char choice_by[20];
	char choice='Y';
	char input;
	char passChar[100];

	cout<<"Welcome to Grocery List Updater!";
	system ("pause>0");
	system ("cls");
	cout<<"Are you a buyer[b] or a seller[a]? ";
	cout<<"Choice: ";
	cin.getline(choice_by, 19);
	

	string sellPass="";

	switch(choice_by[20])
	{
		case 'a' : 
			while(choice=='Y')
			{
				cout<<"Please enter password: ";

			for (int i=0; ;i++)
			{
				input=_getch();
				if ((input!=8)&&(input!=13))
				{	
					passChar[i]=input;
				}

				else if (input==8)
					i-=2;
				else if (input==13)
					break;

			cout<<"Please enter password: ";

			for (int j=0; j<i+1; j++)
				cout<<"*";
			}
				cout<<endl;

				if (!strcmp(passChar, "imaseller"))
				{
					cout<<"Password correct!\n";                  
									  }
			else
			cout<<"Password incorrect!\n";
			if ((!strcmp(passChar, "secret")))
			{
		
				cout<<"Well then, please wait..";
				Sleep(240);
				goto menu;
		
			}
			else 
				cout<<"Do you want to try again? Y/N : ";
				cin>>choice;
					choice=toupper(choice);
					cin.ignore(100,'\n');
					for (int i=0; i<100; passChar[i++]=0);
    
			}

				break;
		case 'b' : exit(0);
			break;
		default: cout<<"Please type 'buyer' or 'seller' only.";
			break;
	}

menu:

	system("cls");
	cout<<"MENU: \n";
	cout<<"\n1- Add record/s";
	cout<<"\n2- Delete record/s";
	cout<<"\n3- View record/s";
	cout<<"\n4- Search record/s";
	cout<<"\n5- Exit";
	cout<<"\nChoice: ";
	cin>>choice_menu;
	
	switch(choice)
	{
	case 1: addRec();
		goto menu;
		break;
	case 2: delRec();
		goto menu;
		break;
	case 3: viewRec();
		goto menu;
		break;
	case 4: srchRec();
		goto menu;
		break;
	case 5: cout<<"Thank you for updating the grocery list!";
		system ("pause>0");
		return 0;
		break;
	default: cout<<"Invalid Input! Please enter 1 - 5 only, thank you.";
		break;
	}
}

void addRec()
{
	groList groc[1];
	int numProd;
	system("cls");
	cout<<"Enter number of products to be entered: ";
	cin>>numProd;

		for (int i=0; i<numProd; i++)
		{
			cout<<"Enter the following needed information.\n";
			cout<<"ID of the product: ";
			cin>>groc[i].id;
			cin.get();
			cout<<"\nName of the product: ";
			cin.getline(groc[i].name, 19);
			cout<<"\nType of the product: ";
			cin.getline(groc[i].type, 19);
			cout<<"\nQuantity of the product: ";
			cin>>groc[i].quantity;
			cin.get();
			cout<<"\nPrice of the product: ";
			cin>>groc[i].price;
			cin.get();
		}
}

void delRec()
{
}

void viewRec()
{
	groList groc[1];
	int numProd=0;
		cout<<setw(5)<<"ID"
			<<setw(20)<<"Product Name"
			<<setw(13)<<"Type"
			<<setw(14)<<"Quantity"
			<<setw(17)<<"Price";
		

	for (int i=0; i<numProd; i++)
	{
		cout<<endl;
		cout<<setw(5)<<groc[i].id
			<<setw(20)<<groc[i].name
			<<setw(13)<<groc[i].type
			<<setw(14)<<groc[i].quantity
			<<setw(17)<<groc[i].price;
	}
	
	system ("pause>0");
}

void srchRec()
{
}
W H Y D O E S N O O N E EVER REPLY!? how is this helpful? >:|
Generally it's because the OP hasn't attempted the questions or shown any willingness to do so.

Or perhaps it's because we don't really like reading through 200 lines of code to find the source of the problem.

In either respect, a second post like that will probably make most people thankful that they didn't reply to the first.
Right near the beginning there's this code (simplified):
1
2
3
4
5
6
7
8
9
    char choice_by[20];

    cout << "Are you a buyer[b] or a seller[a]? ";
    cout << "Choice: ";
    cin.getline(choice_by, 19);
    
    switch(choice_by[20])
    {
        case 'a' : 

Now choice_by may be considered either as an array of 20 individual characters, or as a string.

In the getline here, cin.getline(choice_by, 19);, it is treated as a string.

Then in the switch statement it is treated not as a string, but as a lot of separate characters. We can see that because the case 'a' is testing for the single letter 'a'. But which part of the array does it test?

Let's look at the array again. char choice_by[20];
it is an array of 20 characters, they are accessed individually by means of a subscript in the range 0 to 19, thus:
choice_by[0]
choice_by[1]
choice_by[2]
...
choice_by[17]
choice_by[18]
choice_by[19]

But which character is used in the switch statement?
switch(choice_by[20])
None of them. choice_by[20] is the 21st character, it is not inside the array, it is the next memory location past the end of the array. Whatever is in that location, the program should not be accessing it, as it doesn't belong to the program.
Also, don't use goto.
Ok, belly full of turkey and this peaked my interest.

Didn't implement the password part and in my delItem while loop you should be able to see how I like to do linear searches. Stayed with structs.

Needs some polishing but hope this helps point you in a direction that helps.

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

const int MAX_LIST_SIZE = 20;//Items in grocery list

struct Product{

	int quantity;//of product 
	double price;//of product
	std::string name;//of product

	//Constructors
	Product():
	quantity(0), price(0), name("NO_NAME"){}

	Product(int q, double p, std::string n) :
		quantity(q), price(p), name(n){}
	
};

struct GroceryList{

	std::string name;//of list
	Product list[MAX_LIST_SIZE];//Array limited by const MAX_LIST_SIZE
	double total;//amount due

	//Constructors
	GroceryList(){}

	GroceryList(std::string n) :name(n){}

	//Calculates total due
	double totalPrice(){
		total = 0.0;
		for (int i = 0; i < MAX_LIST_SIZE; i++)
			total += list[i].price * list[i].quantity;
		return total;
	}//totalPrice

	//Print  grocery list
	void printList(){//Debug: Needs formatting
		std::cout << name << "\n\n";
		std::cout << "Item\t\tPrice\tQuantity" << std::endl;
		for (int i = 0; i < MAX_LIST_SIZE; i++){
			if (list[i].quantity != 0){
				std::cout << list[i].name;
				std::cout << "\t";
				std::cout << std::fixed << std::setw(6) << std::setprecision(2) << "$" << list[i].price;
				std::cout << "\t";
				std::cout << std::fixed << std::setw(6) << std::setprecision(2) << list[i].quantity << std::endl;
				std::cout << std::endl;
			}
		}//for
	}//printList
};


//Exercise structs

Product addProduct();
bool delItem(GroceryList&, std::string);

int main(){

	char ch = NULL;
	int i = 5;//Start index for user enter product

	//Create myList
	GroceryList myList("Weekly List");
	
	//Create Products
	Product apples(15, 1.0, "Apples");
	Product milk(4, 4.0, "Milk"); 
	Product cereal(4, 2.0, "Cereal");
	Product bread(2, 1.99, "Bread");
	Product butter(2, 3.99, "Butter");

	//Add products to myList.list
	myList.list[0] = apples;
	myList.list[1] = milk;
	myList.list[2] = cereal;
	myList.list[3] = bread;
	myList.list[4] = butter;

	//Test addProduct
	std::cout << "Add item to list (Y/N): ";
	std::cin >> ch;
	std::cout << std::endl;

	while (ch == 'Y' || ch == 'y' && i < MAX_LIST_SIZE){

		myList.list[i] = addProduct();

		i++;

		if (i < MAX_LIST_SIZE){
			std::cout << "Add item to list (Y/N): ";
			std::cin >> ch;
			std::cout << std::endl;
		}
		else
			std::cerr << "List Full\n";
		
	}//while

	//Test delete item
	delItem(myList, "Apples");
	delItem(myList, "cheese");//Not in list
	
	//Test list print
	myList.printList();

	//Test list total
	std::cout << "Total Due: $";
	std::cout << std::fixed << std::setw(6) << std::setprecision(2) << myList.totalPrice() << "\n\n";

	//Done
	std::cout << "******Normal Termination******" << std::endl;

	return 0;
}//main

Product addProduct(){//Debug: Limited error checking

	std::string name;
	int quantity;
	double price;

	std::cout << "Enter item name: ";
	std::cin >> name;
	
	std::cout << "Enter item price: ";
	std::cin >> price;
	while (price < 0){//No negative pricing
		std::cerr << "Price can not be less than 0.\n";
		std::cout << "Enter item price: ";
		std::cin >> price;
	}//while
	
	std::cout << "Enter item quantity: ";
	std::cin >> quantity;
	while (quantity < 0){//No negative quantities
		std::cerr << "Quantity can not be less than 0.\n";
		std::cout << "Enter item price: ";
		std::cin >> price;
	}//while

	return Product(quantity, price, name);

}//addProduct

bool delItem(GroceryList& list, std::string item){ 

	bool flag = false;
	int position = -1;
	int index = 0;

	while (index < MAX_LIST_SIZE && !flag){

		if (item.compare(list.list[index].name) == 0){

			position = index;
			flag = true;
		}//if
		index++;
	}//while

	if (flag){

		GroceryList temp;

		for (int k = 0, j = 0; k < MAX_LIST_SIZE; k++, j++){

			if (k == position)//Skip over record
				k++;//if

			temp.list[j] = list.list[k];//Copy records
		}//for

		temp.name = list.name;//Copy list name
		temp.totalPrice();//Update totalPrice
		list = temp;//Copy back
	}
	else
		std::cerr << item << " not in list." << std::endl;

	return flag;
}//delItem 
@mobotus your code runs, but it doesn't do the delItem(); function and the search too. To be honest i'm a bit confused how. my code looks like this now:

ERRORS: none.
WARNINGS: warning C4102: 'pass' : unreferenced label
warning C4101: 'choice_by' : unreferenced local variable
warning C4101: 'input' : unreferenced local variable


Your code actually helped. My problem is how will i put the delete and search function here? What would my code look like? thank you very much!.


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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "Windows.h"
#include <conio.h>
#include <string>
#include <vector>
using namespace std;
struct groList
{
	int id;
	char name[20];
	double price;
	int quantity;
	char type[20];
};

void loading(void);
void gotoxy(int, int);

int main()
{
	groList groc[1];
	int choice_menu=0;
	char choice_by; 
	char input; 

	system ("color 3");
	/*loading();*/
	system("cls");
	int numProd=0;

	
pass:

//{
//    char userChar[100], choice='Y', input, passChar[100];
//    
//    for (int i=0; i<100; userChar[i++]=0);
//    
//	while(choice=='Y')
//	{
//		gotoxy(20, 4);
//		for (int i=0; i<41; i++)
//		{
//			cout<<char(220);
//		}
//			gotoxy(20, 5);
//			cout<<char(220);
//			gotoxy(20, 6);
//			cout<<char(220);
//			gotoxy(20, 7);
//			cout<<char(220);
//			gotoxy(20, 8);
//			cout<<char(220);
//			gotoxy(20, 9);
//			cout<<char(220);
//			gotoxy(20, 10);
//			cout<<char(220);
//			gotoxy(20, 11);
//			cout<<char(220);
//			gotoxy(20, 12);
//			cout<<char(220);
//
//			gotoxy(60, 5);
//			cout<<char(220);
//			gotoxy(60, 6);
//			cout<<char(220);
//			gotoxy(60, 7);
//			cout<<char(220);
//			gotoxy(60, 8);
//			cout<<char(220);
//			gotoxy(60, 9);
//			cout<<char(220);
//			gotoxy(60, 10);
//			cout<<char(220);
//			gotoxy(60, 11);
//			cout<<char(220);
//			gotoxy(60, 12);
//			cout<<char(220);
//		
//			gotoxy(20, 12);
//		for (int i=0; i<41; i++)
//		{
//			cout<<char(220);
//		}
//
//		gotoxy(25, 8);
//		cout<<"ENTER USERNAME: ";
//		gotoxy (42, 8);
//		cin.getline(userChar,100);
//		system("cls");
//
//		gotoxy(20, 4);
//		for (int i=0; i<41; i++)
//		{
//			cout<<char(220);
//		}
//			gotoxy(20, 5);
//			cout<<char(220);
//			gotoxy(20, 6);
//			cout<<char(220);
//			gotoxy(20, 7);
//			cout<<char(220);
//			gotoxy(20, 8);
//			cout<<char(220);
//			gotoxy(20, 9);
//			cout<<char(220);
//			gotoxy(20, 10);
//			cout<<char(220);
//			gotoxy(20, 11);
//			cout<<char(220);
//			gotoxy(20, 12);
//			cout<<char(220);
//
//			gotoxy(60, 5);
//			cout<<char(220);
//			gotoxy(60, 6);
//			cout<<char(220);
//			gotoxy(60, 7);
//			cout<<char(220);
//			gotoxy(60, 8);
//			cout<<char(220);
//			gotoxy(60, 9);
//			cout<<char(220);
//			gotoxy(60, 10);
//			cout<<char(220);
//			gotoxy(60, 11);
//			cout<<char(220);
//			gotoxy(60, 12);
//			cout<<char(220);
//		
//			gotoxy(20, 12);
//		for (int i=0; i<41; i++)
//		{
//			cout<<char(220);
//		}
//
//		gotoxy (25, 8);
//		cout<<"ENTER PASSWORD: ";
//		gotoxy (42, 8);
//		
//	for (int i=0;;i++)
//	{
//	input=_getch();
//	if ((input!=8)&&(input!=13))
//	{	
//		passChar[i]=input;
//	}
//
//	else if (input==8)
//		i-=2;
//	else if (input==13)
//		break;
//
//	gotoxy(25, 8);
//    cout<<"ENTER PASSWORD: ";
//	gotoxy(42, 8);
//	for (int j=0; j<i+1; j++)
//		cout<<char(236);
//	}
//		cout<<endl;
//    if ((!strcmp(userChar,"admin"))&&(!strcmp(passChar, "secret")))
//    {
//		gotoxy (24, 20);
//        cout<<"Access Granted!";
//		Sleep(500);
//		goto menu;
//		
//	}
//    else 
//		gotoxy (24, 19);
//		cout<<"Acess denied!\n";
//		gotoxy (24, 20);
//		cout<<"Do you want to try again? Y/N : ";
//		cin>>choice;
//			choice=toupper(choice);
//			cin.ignore(100,'\n');
//		for (int i=0; i<100; userChar[i++]=0);
//			for (int i=0; i<100; passChar[i++]=0);
//			system("cls");
//    
//}
//		system("cls");
//	}
//
//
//
//	system("cls");
//	gotoxy(17, 2);
//	cout<<"              __          __   __          __   \n";
//	gotoxy(17, 3);
//	cout<<" \\  /  \\  /  |__   |     |    |  | |\\  /| |__ \n";
//	gotoxy(17, 4);
//	cout<<"  \\/    \\/   |__   |__   |__  |__| | \\/ | |__ \n";
//	gotoxy(17, 5);
//	cout<<"                 ___   __  \n";
//	gotoxy(17, 6);
//	cout<<"                  |   |  | \n";
//	gotoxy(17, 7);
//	cout<<"                  |   |__| \n\n";
//	gotoxy(21, 8);
//	cout<<" ___   __    __   __   __    __         \n";
//	gotoxy(21, 9);
//	cout<<"| __  |__|  |  | |    |__   |__|   \\ / \n";
//	gotoxy(21, 10);
//	cout<<"|___| |  \\  |__| |__  |__   |  \\    |  \n\n";
//	gotoxy(23, 11);
//	cout<<"      __   __   __  __   __   __ \n";
//	gotoxy(23, 12);
//	cout<<"|  | |__| |  \\ |__|  |  |__  |__| \n ";
//	gotoxy(23, 13);
//	cout<<"|__| |    |__/ |  |  |  |__  |  \\\n";
//	gotoxy(23, 14);
//
//	system ("pause>0");
//	system ("cls");
	
menu:
	system("cls");
	gotoxy(28,8);
	cout<<"MENU: \n";
	gotoxy(28,9);
	cout<<"1- ADD RECORD/s";
	gotoxy(28,10);
	cout<<"2- DELETE RECORD/s";
	gotoxy(28,11);
	cout<<"3- VIEW RECORD/s";
	gotoxy(28,12);
	cout<<"4- SEARCH RECORD/s";
	gotoxy(28,13);
	cout<<"5- EXIT";
	gotoxy(28,14);
	cout<<"CHOICE: ";
	gotoxy(36,14);
	cin>>choice_menu;

	switch(choice_menu)
	{
	case 1:
		system("cls");
		gotoxy(24,8);
		cout<<"NO. OF PRODUCTS TO BE ENTERED: ";
		cin>>numProd;

		for (int i=0; i<numProd; i++)
		{
			system ("cls");
			gotoxy(22,9);
			cout<<"ENTER THE FOLLOWING INFORMATION.";
			gotoxy(22,10);
			cout<<"ID OF THE PRODUCT: ";
			cin>>groc[i].id;
			cin.get();
			gotoxy(22,11);
			cout<<"NAME OF THE PRODUCT: ";
			cin.getline(groc[i].name, 19);
			gotoxy(22,12);
			cout<<"TYPE OF THE PRODUCT: ";
			cin.getline(groc[i].type, 19);
			gotoxy(22,13);
			cout<<"QUANTITY OF THE PRODUCT: ";
			cin>>groc[i].quantity;
			cin.get();
			gotoxy(22,14);
			cout<<"PRICE OF THE PRODUCT: ";
			cin>>groc[i].price;
			cin.get();
		}
		goto menu;
		break;
	case 2: 
		goto menu;
		break;
	case 3:
			system ("cls");
			cout<<setw(4)<<"ID"
				<<setw(15)<<"NAME"
				<<setw(13)<<"TYPE"
				<<setw(14)<<"QUANTITY"
				<<setw(15)<<"PRICE"
				<<setw(18)<<"TOTAL SALES";
		

		for (int i=0; i<numProd; i++)
		{	
			double totalSales =0;
			totalSales = totalSales + (groc[i].price * groc[i].quantity);
			double totalSalestwo =0;
			totalSalestwo = totalSalestwo + totalSales;

			cout<<endl;
			cout<<setw(4)<<groc[i].id
				<<setw(15)<<groc[i].name
				<<setw(13)<<groc[i].type
				<<setw(14)<<groc[i].quantity
				<<setw(15)<<groc[i].price
				<<setw(18)<<totalSales;
			
		}

		for (int i=0; i<numProd; i++)
			{
				double totalSales =0;
				totalSales=0;
				double totalSalestwo=0;
				totalSalestwo = totalSalestwo + (totalSales + (groc[i].price * groc[i].quantity));

				cout<<endl;
				cout<<setw(80)<<totalSalestwo;
			}

		system ("pause>0");
		system ("cls");
		goto menu;
		break;
	case 4: 

		break;
	case 5: system("cls");
		gotoxy(17, 10);
		cout<<"THANK YOU FOR UPDATING THE GROCERY LIST!";
		system ("pause>0");
		return 0;
		break;
	default: system("cls");
		gotoxy (15, 10);
		cout<<"IINVALID INPUT! PLEASE, ENTER 1-5 ONLY. THANK YOU.";
		Sleep(1400);
		goto menu;
		break;
	}
}
	
void loading(void)
{
	gotoxy(32, 10);
	cout<<"L O A D I N G \n";
	for (int i=0; i<80; i++)
	{
		cout<<(char)221;
		for (long j=0; j<16000000; j++)
		{}
	}
	cout<<endl;
}

void gotoxy (int x, int y)
{

  COORD coord;

  coord.X = x; coord.Y = y;

  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}
Topic archived. No new replies allowed.