Please help to correct my mistake !

Please help me to correct my mistake! i don't really know to use class, but i tried, appreciate that u correct my mistake.
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
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
 #include <iostream>
#include <iomanip>
 
using namespace std;
 
class item {
private:
	static char name;
	static float price;
	static int quantity;
public:
	void set_item(char n, float p, int q);
	void set_item_name(char n);
	void set_item_price(float p);
	void set_item_quantity(int q);
	char get_item_name();
	float get_item_price();
	int get_item_quantity();
};
 
void item :: set_item_name(char n){
	name = n;
}
 
void item :: set_item_price(float p){
	price = p;
}
 
void item :: set_item_quantity(int q){
	quantity = q;
}
 
char item :: get_item_name(){
	return name;
}
 
float item :: get_item_price(){
	return price;
}
 
int item :: get_item_quantity(){
	return quantity;
}
 
 
void item :: set_item(char n, float p, int q){
	name = n;
	price = p;
	quantity = q;
}
 
 
 
int add();
int edit();
int search();
int list();
void edit_code(int x);
void edit_name(int x);
void search_code(int x);
void search_name(int x);
 
int s[10000];
int i = 0;
int last_item;
class item;
 
int main(){
	int function;
	cout << "1. List\n2. Add\n3. Edit\n4. Search\n";
			cout <<"\nEnter :";
			cin >> function;
 
			if(function == 1)
				list();
			else if(function == 2)
				add();
			else if(function == 3)
				edit();
			else if(function == 4)
				search();
			else 
				cout << "\nERROR!";
 
 
 
	cout << "\n\n";
	return main();
}
 
int add() {
	char name;
	float price;
	int quantity;
	int continue_;
 
	item s[10000];
 
	cout << "\nEnter item name :";
	cin >> name;
	cout << "\nEnter item price :";
	cin >> price;
	cout << "\nEnter item quantity :";
	cin >> quantity;
 
	s[i].set_item(name, price, quantity);
 
	cout << "\n\nContinue to Add? (Y/N) :";
	cin >> continue_;
 
	last_item += 1;
	i++;
 
	cout << "\n\n";
 
	if( continue_ == 'Y' || continue_ == 'y')
		return add();
	else 
		return main();
}
 
int edit() {
	int x, continue_;
	cout << "\nEnter item name or item's code :";
	cin >> x;
 
	if( isdigit(x))
		edit_code(x);
	else
		edit_name(x);
 
	cout << "\nContinue edit? (Y or N) :";
	cin >> continue_;
 
	cout << "\n\n";
 
	if( continue_ == 'Y' || continue_ == 'y')
		return edit();
	else 
		return main();
}
 
void edit_code(int x){
	char test;
	char n;
	float p;
	int q;
 
	item s[10000];
 
	cout << "\nEnter N(edit name)/ P(edit price)/ Q(edit quantity) :";
	cin >> test;
 
	if( test == 'N'|| test == 'n'){
		cout << "\nEnter new name :";
		cin >> n;
		s[x].set_item_name(n);
	}
	else if ( test == 'P' || test == 'p'){
		cout << "\nEnter new price :";
		cin >> p;
		s[x].set_item_price(p);
	}
	else if( test == 'Q' || test == 'q'){
		cout << "\nEnter new quantity :";
		cin >> q;
		s[x].set_item_quantity(q);
	}
	else 
		cout << "Error!";
}
 
void edit_name(int x){
	int loop = 0;
	char test;
	char n;
	float p;
	int q;
 
	item s[10000];
 
	for( loop ; loop < last_item + 1; loop ++){
		if(s[loop].get_item_name() == x)
			break;
	}
 
	cout << "\nEnter N(edit name)/ P(edit price)/ Q(edit quantity) :";
	cin >> test;
 
	if( test == 'N'|| test == 'n'){
		cout << "\nEnter new name :";
		cin >> n;
		s[loop].set_item_name(n);
	}
	else if ( test == 'P' || test == 'p'){
		cout << "\nEnter new price :";
		cin >> p;
		s[loop].set_item_price(p);
	}
	else if( test == 'Q' || test == 'q'){
		cout << "\nEnter new quantity :";
		cin >> q;
		s[loop].set_item_quantity(q);
	}
	else 
		cout << "\nError!";
}
 
int search(){
	int x;
	int continue_;
 
	cout << "\nEnter item name or item's code :";
	cin >> x;
 
	if( isdigit(x))
		search_code(x);
	else
		search_name(x);
 
	cout << "\nContinue search? (Y or N) :";
	cin >> continue_;
 
	cout << "\n\n";
 
	if( continue_ == 'Y' || continue_ == 'y')
		return search();
	else 
		return main();
}
 
void search_code(int x){
	item s[10000];
 
	cout << "\nCode :" << x << "\nName :" << s[x].get_item_name() << "\nPrice :" << s[x].get_item_price() <<  "\nQuantity :" << s[x].get_item_quantity();
}
 
void search_name(int x){
	int loop = 0;
 
	item s[10000];
 
	for( loop ; loop < last_item + 1; loop ++){
		if(s[loop].get_item_name() == x)
			break;
	}
 
	cout << "\nCode :" << x << "\nName :" << s[x].get_item_name() << "\nPrice :" << s[x].get_item_price() <<  "\nQuantity :" << s[x].get_item_quantity();
}
 
int list(){
	item s[10000];
 
	cout << "\nCode\tName\tPrice\tQuantity";
 
	for(i = 0; i < last_item+1; i++ )
		cout << "\n" << i << "\t" << s[i].get_item_name() << "\t" << s[i].get_item_price() << "\t" << s[i].get_item_quantity();
 
	cout << "\n\nPress any key to go menu";
 
	return main();
}
You didn't tell us what the problem is. Compiling error? Output not giving the desired result? etc. etc.
I thnk the problem is you're using statc variable in your member of class, in that case all of the object that you create will have a same name, price, and quantity
closed account (1CfG1hU5)
in the 'int list()' function, you 'return main();'

returning main() has to be wrong.

return an integer or char to main(), but not return the main() function.

Hyperfine: I'm apologize. output isn't giving the desired result.

LendraDwi: I'll try to remove the static keyword.

jt1: I'll correct it .

Thanks for reply !
output isn't giving the desired result.

That doesn't really tell us much more! What is the desired result? What is the output you're actually getting?

Why are you not telling us the helpful details? Don't you want us to be able to help you?
Hi guys, i made some change on my code, but it's still have problem. First, at the function void add(),after the cin >> name, it become infinity loop.

#include <iostream>
#include <iomanip>

using namespace std;

class item {
private:
char name;
float price;
int quantity;
public:
void set_item(char n, float p, int q);
void set_item_name(char n);
void set_item_price(float p);
void set_item_quantity(int q);
char get_item_name();
float get_item_price();
int get_item_quantity();
};

void item :: set_item_name(char n){
name = n;
}

void item :: set_item_price(float p){
price = p;
}

void item :: set_item_quantity(int q){
quantity = q;
}

char item :: get_item_name(){
return name;
}

float item :: get_item_price(){
return price;
}

int item :: get_item_quantity(){
return quantity;
}


void item :: set_item(char n, float p, int q){
name = n;
price = p;
quantity = q;
}



void add();
void edit();
void search();
void list();
void edit_code(int x);
void edit_name(int x);
void search_code(int x);
void search_name(int x);

int inf = 0;
int s[10000];
int i = 0;
int test = i - 1;
int last_item;
class item;

int main(){
int function;
do{
cout << "1. List\n2. Add\n3. Edit\n4. Search\n";
cout <<"\nEnter :";
cin >> function;

if(function == 1)
list();
else if(function == 2)
add();
else if(function == 3)
edit();
else if(function == 4)
search();
else
cout << "\nERROR!";
} while(inf != 1);


cout << "\n\n";
return 0;
}

void add() {
char name;
float price;
int quantity;
int continue_;

item s[10000];
do{
cout << "\nEnter item name :";
cin >> name;
cout << "\nEnter item price :";
cin >> price;
cout << "\nEnter item quantity :";
cin >> quantity;

s[i].set_item(name, price, quantity);

cout << "\n\nContinue to Add? (Y or N) :";
cin >> continue_;

last_item += 1;
i++;

cout << "\n\n";

}while( continue_ == 'Y' || continue_ == 'y');
}

void edit() {
int x, continue_;
do{
cout << "\nEnter item name or item's code :";
cin >> x;

if( isdigit(x))
edit_code(x);
else
edit_name(x);

cout << "\nContinue edit? (Y or N) :";
cin >> continue_;

cout << "\n\n";

}while( continue_ == 'Y' || continue_ == 'y');
}

void edit_code(int x){
char test;
char n;
float p;
int q;

item s[10000];

cout << "\nEnter N(edit name)/ P(edit price)/ Q(edit quantity) :";
cin >> test;

if( test == 'N'|| test == 'n'){
cout << "\nEnter new name :";
cin >> n;
s[x].set_item_name(n);
}
else if ( test == 'P' || test == 'p'){
cout << "\nEnter new price :";
cin >> p;
s[x].set_item_price(p);
}
else if( test == 'Q' || test == 'q'){
cout << "\nEnter new quantity :";
cin >> q;
s[x].set_item_quantity(q);
}
else
cout << "Error!";
}

void edit_name(int x){
int loop = 0;
char test;
char n;
float p;
int q;

item s[10000];

for( loop ; loop < last_item + 1; loop ++){
if(s[loop].get_item_name() == x)
break;
}

cout << "\nEnter N(edit name)/ P(edit price)/ Q(edit quantity) :";
cin >> test;

if( test == 'N'|| test == 'n'){
cout << "\nEnter new name :";
cin >> n;
s[loop].set_item_name(n);
}
else if ( test == 'P' || test == 'p'){
cout << "\nEnter new price :";
cin >> p;
s[loop].set_item_price(p);
}
else if( test == 'Q' || test == 'q'){
cout << "\nEnter new quantity :";
cin >> q;
s[loop].set_item_quantity(q);
}
else
cout << "\nError!";
}

void search(){
int x;
int continue_;

do{
cout << "\nEnter item name or item's code :";
cin >> x;

if( isdigit(x))
search_code(x);
else
search_name(x);

cout << "\nContinue search? (Y or N) :";
cin >> continue_;

cout << "\n\n";

}while( continue_ != 'Y' || continue_ != 'y');

}

void search_code(int x){
item s[10000];

cout << "\nCode :" << x << "\nName :" << s[x].get_item_name() << "\nPrice :" << s[x].get_item_price() << "\nQuantity :" << s[x].get_item_quantity();
}

void search_name(int x){
int loop = 0;

item s[10000];

for( loop ; loop < last_item + 1; loop ++){
if(s[loop].get_item_name() == x)
break;
}

cout << "\nCode :" << x << "\nName :" << s[x].get_item_name() << "\nPrice :" << s[x].get_item_price() << "\nQuantity :" << s[x].get_item_quantity();
}

void list(){
item s[10000];
int t;

do{
if (test < 0){
cout << "\n\nEmpty!";
}
else if (test >= 0){
cout << "\nCode\tName\tPrice\tQuantity";
for(i = 0; i < last_item+1; i++ )
cout << "\n" << i << "\t" << s[i].get_item_name() << "\t" << s[i].get_item_price() << "\t" << s[i].get_item_quantity();
}

cout << "\n\nPress Q to go menu";
cin >> t;
cout << "\n\n";
}while ( t != 'q' || t != 'Q');

}
Topic archived. No new replies allowed.