create a purchase function

hello !
I am new here.
I need your help.I created program for inventory management.
Please guide to purchase a particular product and after purchasing a product, the number of products available in stock should also decrease according to purchased quantity.

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
  #include<iostream>
#include<conio.h>
#include<fstream>
#include<string.h>
using namespace std;

int stock, price;
string pname;
void add();
void show();
int remove();
void purchase();

int main()
{
	int c;
	char pass[10];
	mainmenu:
	cout<<"\t\t\t\t\t  =====================================\n";
	cout<<"\t\t\t\t\t| WELCOME TO SHOPPING INVENTORY SYSTEM |\n";
	cout<<"\t\t\t\t\t  =====================================";
	cout<<"\n\n\n\t\t\t\t=======================================================\n\n\n\t\t\t\t[1.] Manager Menu\n\t\t\t\t[2.] Customer Menu\n\t\t\t\t[3.] Exit\n\n\n\t\t\t\t=======================================================\n\n";
	cin>>c;
	
	switch(c)
	{
		case 1:
			{
				A:
				cout<<"Enter your password : ";
				cin>>pass;
				if(strcmp(pass,"manage")==0)
				{
					system("cls");
					goto managermenu;
				}
				else
				{
					cout<<"\n\nInvalid password...\n\n";
					goto A;
	
				}
				
			}
		case 2:
			{
				system("cls");
				goto customermenu;
			}
		case 3:
			{
				exit(0);
			}
	}
	int a, b;
	managermenu:
		cout<<"\t\t\t\t\t ==========================\n";
	    cout<<"\t\t\t\t\t| WELCOME TO MANAGER MENU |\n";
	    cout<<"\t\t\t\t\t ==========================";
	cout<<"\n\n\n\t\t\t\t============================================\n\n\t\t\t\t[1.] Add Product\n\t\t\t\t[2.] Available Products\n\t\t\t\t[3.] Remove Product\n\t\t\t\t[4.] Go Back\n\t\t\t\t[5.] Exit\n\n\t\t\t\t============================================\n\n";
	cin>>a;
	
	switch(a)
	{
		case 1:
			{
				add();
				system("cls");
				show();
				getch();
				system("cls");
				goto managermenu;
				break;
			}
		case 2:
			{
				system("cls");
				show();
				getch();
				goto managermenu;
				break;
			}
		case 3:
			{
				remove();
				getch();
				goto managermenu;
				break;
			}
		case 4:
			{
				system("cls");
				goto mainmenu;
			}
		case 5:
			{
				exit(0);
			}
	

				
	}
	customermenu:
		cout<<"\t\t\t\t\t ============================\n";
	    cout<<"\t\t\t\t\t| WELCOME TO CUSTOMER MENU |\n";
	    cout<<"\t\t\t\t\t ============================";
		int d;
		cout<<"\n\n\n\t\t\t\t================================================\n\n\n\t\t\t\t[1.] Purchase\n\t\t\t\t[2.] Available Products\n\t\t\t\t[3.] Back to mainmenu\n\t\t\t\t[4.] Exit\n\n\n\t\t\t\t================================================\n\n";
		cin>>d;
		switch(d)
		{
			case 1:
				{
					purchase();
					goto customermenu;
					break;
				
				}
			case 2:
				{
					system("cls");
				    show();
				    getch();
				    system("cls");
				    goto customermenu;
				    break;
				}
			case 3:
				{
					system("cls");
					goto mainmenu;
				}
			case 4:
				{
					exit(0);
				}
		}
	
	getch();
	return 0;
}

void add()
{
	cout<<"Enter product name, price and quantity respectively\n";
				cin>>pname>>price>>stock;
				
				ofstream shop;
				shop.open("shop.txt" , ios::app);
				shop<<pname<<endl;
				shop<<stock<<endl;
				shop<<price<<endl;
				shop.close(); 
}

void show()
{
	            cout<<"\n\t\t\t==================================================================";
	            cout<<"\n\n\t\t\t=================\tTHE STOCK ITEMS ARE\t==================";
	            cout<<"\n\n\t\t\t==================================================================\n";
	            cout<<"\n\n\t\t\t\tPARTICULARS\tSTOCK AVAILABLE\t\t PRICE";
	            cout<<"\n\n\t\t\t============================================================\n\n";
	            ifstream show;
				show.open("shop.txt");
				show>>pname;
				show>>stock;
				show>>price;
				
				while(!show.eof())
				{
					cout<<"\t\t\t\t"<<pname<<"\t\t\t"<<stock<<"\t\t "<<price<<endl;
					
					show>>pname;
				    show>>stock;
				    show>>price;
				}
				show.close();
			}



int remove()
{
	string b;
	cout<<"Enter the name of product you want to delete : ";
				cin>>b;
				
				ifstream del;
				del.open("shop.txt");
				ofstream nfile;
				nfile.open("temp.txt");
				
				del>>pname;
				del>>stock;
				del>>price;
				
				if(b!=pname)
				{
					cout<<"\nProduct not found...";
				}
				
				while(!del.eof())
				{
					if(pname !=b)
					{
						nfile<<pname<<endl;
				        nfile<<stock<<endl;
				        nfile<<price<<endl;
					}
					
					else
					
						cout<<"Record Deleted.";
					
					
					del>>pname;
				    del>>stock;
				    del>>price;
					
				}
				
				nfile.close();
				del.close();
				
				remove("shop.txt");
				rename("temp.txt", "shop.txt");
				
}

void purchase()
{
	string p;
	int quantity;
	cout<<"Enter the name of product : ";
	cin>>p;
	cout<<"Enter quantity : ";
	cin>>quantity;
	ifstream buy;
    buy.open("stop.txt", ios::app);
    ifstream tmp;
    tmp.open("temp.txt");
    
    tmp>>pname>>endl;
    tmp>>price>>endl;
    tmp>>stock>>stock;
	
}
Do you have a teacher or are you learning on your own?
If you have a teacher and he's teaching you to use global variables and gotos then you need to report him to the FBI or something, because that's a crime against humanity.

Don't use global variables.
Instead, use local variables and function parameters.

Don't use gotos.
Instead, use proper loops and function calls.
(E.g., manager menu and customer menu should be functions.)

Until you fix that stuff, perfecting your purchase function is pointless.
Hello umairbilal,

In addition to what dutch has said your above code does not compile and needs several fixes before it can compile and run.

In your includes you have "conio.h". Not everyone has this header file and:

salem c once wrote:

#include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.



Next "string.h" is not the same as "string". You can use "string" and do away with "string.h". You will also need to change line 32 if(strcmp(pass,"manage")==0) to if (pass == "manage") to use a "std::string".

Try to avoid using single letter variable names. It is very confusing and hard to follow in the code. A variable name should be a noun that describes what it is or does.

exit (0); use sparingly. This is an unconditional exit from the program and not the best way to use it. When used in a switch you should leave the switch and go back to where it was called from.

As dutch has said I would put all the menus in functions using "main" to direct the program. And when it comes time to exit the program you would return to main and exit properly.

"main" should be used to direct the program flow not be the program.

Going back to your global variables they are not needed and even moving them to "main" there are not needed there.

The functions that use these variables are where they should be defined because the functions are the only place where they are used.

Last thought "system(???)" should be avoided. This is not as secure as you might think and not everyone used Windows or can use this. If this is for personal use or school it works.

There are many parts of your program that can be improved, but first thing first.

Hope that helps,

Andy

P.S. Post the requirements of the program, if any, so everyone can tell if you are following the directions.
Hello umairbilal,

I have been working on the program and have some questions:

Do you start with an empty file for "shop.txt" or is there something in it? If the file is not empty provide the file or at least a good sample.

When you ask for a product name what does it look like? Does it contain spaces or not?

When you ask for a price what does it look like? Give an example.

Do you know about "vectors", "arrays" and "structs"? Reading the file every time you need something is not the best way to do this.

All this makes a difference when dealing with the program.

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.