string problem

guys im having data types problem on strings

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
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

void menu();
void F_order(char Kod,char f_name,int Price,char Quantity);
void pay();
void display();
void D_base();

//global variable
int table_no,kod,total,total_amount,Price;
string Kod[6],f_name[30];  //error point on here

struct node
{   
	char kod[6],F_name[30];
	int price,quantity;
	struct node * next;
};

struct node * head=NULL;
struct node * tail=NULL;

void D_base()
{
	int code;

	cout<<"Enter food code\n";
	cout<<"1=001\n2=002\n3=003\n4=010\n5=020\n6=030\n7=100\n8=200\n9=300\n";
	cin>>code;

	switch (code)
	{
	case 1:

		Kod="001";
		f_name="Mixed Bento";
		Price=20;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;
	
	case 2:

		Kod="002";
		f_name="Cheese Ring Dakgalbi";
		Price=30;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 3:
		Kod="003";
		f_name="Nyam Nyeon Tongdak";
		Price=15;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 4:

		Kod="010";
		F_name="Frozen Caramel Apple";
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 5:

		Kod="020";
		f_name="Banana Split";
		Price=5;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 6:

		Kod="030";
		f_name="Cherry Cream Cheese";
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 7:

		Kod='100';
		f_name="Chocolate Cream Chip";
		Price=20;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 8:
		Kod='200';
		f_name="Expresso Mocha";
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 9:

		Kod="300";
		f_name="Big Milo Tabur";
		Price=5;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;
	
	}

}

void F_order(char Kod,char f_name,int Price,char Quantity)
{
	struct node * temp;
	temp=new node;

	temp->kod=Kod;
	temp->F_name=f_name;
	temp->price=Price;

	temp->next=NULL;

	if(head==NULL)
	{
		head=temp;
		tail=head;
	}
	else
	{
		tail->next=temp;
		tail=temp;
		tail->next=NULL;
	}

}

void pay()
{
	total=Price*Quantity;
	total_amount=total_amount+total;


}

void display()
{
	if(head==NULL)
	{
		cout<<"No order\n";
	}
	else
	{
		struct node *temp=head;

		while(temp->next!=NULL)
		{
			cout<<"THE CODE IS"<<temp->kod;
			cout<<"THE FOOD NAME IS"<<temp->F_name;
			cout<<"THE PRICE IS"<<temp->price;
		}
	}

	cout<<total_amount<<endl;
}


void main()
{
	int choose;

	cout<<"Enter your table number : ";
	cin>>table_no;

	do
	{
	cout<<"Choose a number\n";
	cout<<"1=TO display menu\n";
	cout<<"2=TO place customer order\n";
	cout<<"3=TO display the customer order\n";
	cout<<"4=TO diplay payment amount for customer order\n";
	cout<<"5=Exit\n\n";
	

	cin>>choose;

	cout<<"\n"<<endl;



	switch (choose)
	{
	case 1:
		menu();
	break;

	case 2:
		F_order();
	break;

	case 3:
		display();
	break;

	case 4:
		pay();
	break;

	case 5:
		exit(0);
	break;
	}

	}
	while (1);


}
guys im having data types problem on strings


What problem?

char Kod
int kod
string Kod[6]
char kod[6]

This is a really bad idea. Use different names for different things.


string Kod[6],f_name[30]; //error point on here
This is an array of six strings, and an array of thirty strings. Did you mean to make thirty strings?

I think you're mixing up string and char arrays. Don't do that.
Last edited on
i change it again but gain more error

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
#include <iostream.h>
#include <stdlib.h>

void menu();
void F_order(char Kod,char f_name,int Price,char Quantity);
void pay();
void display();
void D_base();

//global variable
int table_no,kod,total,total_amount,Price;
char f_name;
char Kod;

struct node
{   
	char kod,F_name;
	int price,quantity;
	struct node * next;
};

struct node * head=NULL;
struct node * tail=NULL;

void D_base()
{
	int code;

	cout<<"Enter food code\n";
	cout<<"1=001\n2=002\n3=003\n4=010\n5=020\n6=030\n7=100\n8=200\n9=300\n";
	cin>>code;

	switch (code)
	{
	case 1:

		Kod='001';
		f_name='Mixed Bento';
		Price=20;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;
	
	case 2:

		Kod='002';
		f_name='Cheese Ring Dakgalbi';
		Price=30;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 3:
		Kod='003';
		f_name='Nyam Nyeon Tongdak';
		Price=15;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 4:

		Kod='010';
		F_name='Frozen Caramel Apple';
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 5:

		Kod='020';
		f_name="Banana Split";
		Price=5;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 6:

		Kod='030';
		f_name="Cherry Cream Cheese";
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 7:

		Kod='100';
		f_name='Chocolate Cream Chip';
		Price=20;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 8:
		Kod='200';
		f_name='Expresso Mocha';
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 9:

		Kod='300';
		f_name='Big Milo Tabur';
		Price=5;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;
	
	}

}

void F_order(char Kod,char f_name,int Price,char Quantity)
{
	struct node * temp;
	temp=new node;

	temp->kod=Kod;
	temp->F_name=f_name;
	temp->price=Price;

	temp->next=NULL;

	if(head==NULL)
	{
		head=temp;
		tail=head;
	}
	else
	{
		tail->next=temp;
		tail=temp;
		tail->next=NULL;
	}

}

void pay()
{
	total=Price*Quantity;
	total_amount=total_amount+total;


}

void display()
{
	if(head==NULL)
	{
		cout<<"No order\n";
	}
	else
	{
		struct node *temp=head;

		while(temp->next!=NULL)
		{
			cout<<"THE CODE IS"<<temp->kod;
			cout<<"THE FOOD NAME IS"<<temp->F_name;
			cout<<"THE PRICE IS"<<temp->price;
		}
	}

	cout<<total_amount<<endl;
}


void main()
{
	int choose;

	cout<<"Enter your table number : ";
	cin>>table_no;

	do
	{
	cout<<"Choose a number\n";
	cout<<"1=TO display menu\n";
	cout<<"2=TO place customer order\n";
	cout<<"3=TO display the customer order\n";
	cout<<"4=TO diplay payment amount for customer order\n";
	cout<<"5=Exit\n\n";
	

	cin>>choose;

	cout<<"\n"<<endl;



	switch (choose)
	{
	case 1:
		menu();
	break;

	case 2:
		F_order();
	break;

	case 3:
		display();
	break;

	case 4:
		pay();
	break;

	case 5:
		exit(0);
	break;
	}

	}
	while (1);


}



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
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(37) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(37) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(38) : error C2015: too many characters in constant
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(42) : error C2065: 'Quantity' : undeclared identifier
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(52) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(52) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(53) : error C2015: too many characters in constant
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(66) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(66) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(67) : error C2015: too many characters in constant
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(81) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(81) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(82) : error C2065: 'F_name' : undeclared identifier
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(82) : error C2015: too many characters in constant
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(96) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(96) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(97) : error C2440: '=' : cannot convert from 'char [13]' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(111) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(111) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(112) : error C2440: '=' : cannot convert from 'char [20]' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(126) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(126) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(127) : error C2015: too many characters in constant
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(140) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(140) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(141) : error C2015: too many characters in constant
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(155) : warning C4305: '=' : truncation from 'const int' to 'char'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(155) : warning C4309: '=' : truncation of constant value
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(156) : error C2015: too many characters in constant
Error executing cl.exe.

project20juta.exe - 11 error(s), 18 warning(s)


this is the errors
Last edited on
closed account (E0p9LyTq)
guys im having data types problem on strings

If you are getting errors it would be helpful to post the errors you receive.

You are including <string.h>, a C library header. There is no C++ string class in the header, only functions to deal with C char arrays.

Including <iostream.h> shows your compiler and/or your class instructions are outdated.

C++ headers don't include .h in the name. It is <iostream>. For C++ strings it is <string>.
Stop using char. char is for holding ONE character. ONE.

Let's count.
Kod='001';
How many characters in '001'? Three.
So can we store THREE characters in one char? No.

Stop using char. Use string.

Stop using char. Use string.

Kod='001';
This should be Kod="001";.

You've been on this problem for ages. Maybe you should try something easier.

#include <string.h>
No no NO. That's for C code. You're trying to write in C++, aren't you?

From here, it just looks like you're really mixed up and you really don't know what you're doing and don't understand basic types like char and int. You need to start again with the simple basics.

i try string already.still error

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
#include <iostream>
#include <cstdlib>

void menu();
void F_order();
void pay();
void display();
void D_base();

//global variable
int table_no,kod,total,total_amount,Price;
string Kod,f_name;

struct node
{   
	string kod,F_name;
	int price,quantity;
	struct node * next;
};

struct node * head=NULL;
struct node * tail=NULL;

void D_base()
{
	int code;

	cout<<"Enter food code\n";
	cout<<"1=001\n2=002\n3=003\n4=010\n5=020\n6=030\n7=100\n8=200\n9=300\n";
	cin>>code;

	switch (code)
	{
	case 1:

		Kod="001";
		f_name="Mixed Bento";
		Price=20;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;
	
	case 2:

		Kod="002";
		f_name="Cheese Ring Dakgalbi";
		Price=30;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 3:
		Kod="003";
		f_name="Nyam Nyeon Tongdak";
		Price=15;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 4:

		Kod="010";
		F_name="Frozen Caramel Apple";
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 5:

		Kod="020";
		f_name="Banana Split";
		Price=5;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 6:

		Kod="030";
		f_name="Cherry Cream Cheese";
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 7:

		Kod="100";
		f_name="Chocolate Cream Chip";
		Price=20;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 8:
		Kod="200";
		f_name="Expresso Mocha";
		Price=10;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;

	case 9:

		Kod="300";
		f_name="Big Milo Tabur";
		Price=5;

		cout<<"Enter quantity\n";
		cin>>Quantity;

		F_order(Kod,f_name,Price,Quantity);

		pay(Price,Quantity);

	break;
	
	}

}

void F_order(string Kod,string f_name,int Price,char Quantity)
{
	struct node * temp;
	temp=new node;

	temp->kod=Kod;
	temp->F_name=f_name;
	temp->price=Price;

	temp->next=NULL;

	if(head==NULL)
	{
		head=temp;
		tail=head;
	}
	else
	{
		tail->next=temp;
		tail=temp;
		tail->next=NULL;
	}

}

void pay()
{
	total=Price*Quantity;
	total_amount=total_amount+total;


}

void display()
{
	if(head==NULL)
	{
		cout<<"No order\n";
	}
	else
	{
		struct node *temp=head;

		while(temp->next!=NULL)
		{
			cout<<"THE CODE IS"<<temp->kod;
			cout<<"THE FOOD NAME IS"<<temp->F_name;
			cout<<"THE PRICE IS"<<temp->price;
		}
	}

	cout<<total_amount<<endl;
}


void main()
{
	int choose;

	cout<<"Enter your table number : ";
	cin>>table_no;

	do
	{
	cout<<"Choose a number\n";
	cout<<"1=TO display menu\n";
	cout<<"2=TO place customer order\n";
	cout<<"3=TO display the customer order\n";
	cout<<"4=TO diplay payment amount for customer order\n";
	cout<<"5=Exit\n\n";
	

	cin>>choose;

	cout<<"\n"<<endl;



	switch (choose)
	{
	case 1:
		menu();
	break;

	case 2:
		F_order();
	break;

	case 3:
		display();
	break;

	case 4:
		pay();
	break;

	case 5:
		exit(0);
	break;
	}

	}
	while (1);


}


1
2
3
4
5
6
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(12) : error C2146: syntax error : missing ';' before identifier 'Kod'
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(12) : error C2501: 'string' : missing storage-class or type specifiers
C:\Users\user\Desktop\spartan\spartan c++\project20juta.cpp(12) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

project20juta.exe - 3 error(s), 0 warning(s)

closed account (E0p9LyTq)
i try string already.still error

What is your compiler/integrated development environment (IDE)?

You may be using one that is outdated.

There are several good FREE compilers/IDEs available that are C++14/17 capable.

Microsoft Visual Studio 2017 Community (Windows) or Code::Blocks (Windows, Mac and Linux).

VS 2017 has a very good debugger integrated into the IDE. Knowing how to debug a misbehaving program is a very essential part of learning to program.
andy !!! i need you!!
closed account (E0p9LyTq)
Thread duplication:

http://www.cplusplus.com/forum/beginner/233642/
You'll have a much easier time if you carefully organize your list and hierarchy. I see you want to do a linked list, so I propose an OrderManager that deals with the placing and displaying of Orders. Nodes hold orders and point to next. Please also use descriptive variable names -- "F_name" sounds much worse than "food".

Example code (hopefully I didn't leak memory):
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
#include <iostream>
#include <string>

using namespace std;

struct Order
{  
    string code;
    string food;
    float price;
    int quantity;
    Order() :
        code("000"),
        food("Nothing"),
        price(0.0f),
        quantity(0)
    {
    }
    
    Order(string c, string f, float p, int q) :
        code(c),
        food(f),
        price(p),
        quantity(q)
    {
    }
    
    Order(Order& o) :
        code(o.code),
        food(o.food),
        price(o.price),
        quantity(o.quantity)
    {
    }
};
typedef Order Order;

struct Node
{
    Order order;
    struct Node* next;
    Node(Order& o) : order(o)
    {
    }
};
typedef Node Node;

// Linked list manager of orders
class OrderManager
{
public:
    // Deallocate memory
    ~OrderManager()
    {
        CleanUp();
    }

    // Place a new order
    void Place(Order& o)
    {
        Node* n = new Node(o);
        if (!head)
        {
            head = n;
            tail = n;
        }
        else
        {
            tail->next = n;
            tail = n;
        }
    }
    
    void Place(string c, string f, float p, int q)
    {
        Order order(c,f,p,q);
        Place(order);
    }
    
    void Display()
    {
        if (!head)
        {
            cout << "No orders placed.\n";
        }
        else
        {
            Node* n = head;
            while(n)
            {
                Order& o = n->order;
                cout << o.quantity << " @ $" << o.price << " for [" << o.code << "] " <<
                    o.food << "\n";
                n = n->next;
            }
        }
    }
    
    // Delete all nodes
    void CleanUp()
    {
        Node* n = head;
        Node* tmp;
        while(n)
        {
            tmp = n->next;
            delete n; 
            n = tmp;
        }
        head = nullptr;
        tail = nullptr;
    }
    
private:
    Node* head;
    Node* tail;
};

int main()
{
    OrderManager* root = new OrderManager;
    root->Place("313", "Nachos", 3.99, 2);
    root->Place("011", "Spaghetti", 2.44, 10);
    root->Place("099", "Greek Salad", 5.04, 1);
    root->Display();
    
    root->CleanUp();
    cout << "Cleaned!\n";
    root->Place("444", "Iced Tea", 2.95, 5);
    root->Display();
    
    return 0;
}


In action: https://repl.it/repls/BlissfulSilverLivecd

With this platform, you'll avoid using globals, and it'll be much easier to maintain problems.
Last edited on
Hello helena97,

Sorry I did not find this until this morning and by accident at that.

This is the same program you posted in http://www.cplusplus.com/forum/beginner/233642/3/ It is better to stick with one topic. It makes everything easier to follow.

Still have the same questions. Why did the last project fail. What was wrong and what are the program instructions / requirements?

Knowing what the program should do will better help me help you.

Andy
Topic archived. No new replies allowed.