I need a bit of help with some C++

Here is what I am supposed to do.
Create a program that implements text based characters and inventory items. Allow the user to view the
inventory of the characters. Implement a trading system that allows character to trade items with each
other. Keep a record of each trade. This project will require the use of arrays, functions, structures,
switch statement(s) and loops.
The program will have at least 3 types of objects. Each will be represented by a structure named
accordingly:
Item – Name, quantity, single argument constructor
Character – Name, array of 5 Items, single argument constructor
Record – Names of the traders, Items traded, the quantity, date and time of the trade.
Create additional structures as needed.
The program will have at least 4 global functions:
displayRecords() – Shows past trade information that was stored in records.
showInventory() – Receives a character as a parameter and displays their inventory.
Initialize() - Receives a character as a parameter and initializes their inventory
trade() - Receives 2 characters as parameters and handles a trade between them. Also creates a
record for each trade. Make sure your program can support at least 20 records.
Create additional functions as needed.
Make a global instance of 5 items. Give each item a name using a single argument constructor.
In main() create an array of 5 characters. Each character will have an array of 5 items.
Use the initialize() function to assign each character a random quantity of each item.
Each character will start with 1 - 100 of each item. Use the c++ rand() function to get the value.
Create a switch based menu in main() that provides the user with the following options:
1. Show Characters
2. Show Inventory
3. Show Records
4. Trade items
5. Exit
Implement each of these options using functions.
Validate your input.
Do not let characters trade items with a quantity of zero.
Do not show records that do not exist. Handle the case of no records to show.
Keep all output looking clean and professional.
I know the code doesn't look all that good. I am in the middle of fixing that. My problem lies with the showing of the record. I can't seem to figure it out.
Any help would be appreciated thanks!
Oh, I also no longer need to place the time and date.

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






struct Item
{
	string itemName;
	int quanity;
	Item() {}

	Item(string n)
	{
		itemName= n;
	}



};

struct Character
{
	string name;
	Item item[5];

	Character() {}

	Character(string n)
	{
		name = n;
	}
};

struct Record
{
	char charName1, charName2, charName3, charName4, charName5;
	char itemTraded;
	int num;
	string date;
	
};

void displayR();
void showInventory(Character&);
void initialize(Character&);
void trade(Character&, Character&);

Item gold("Gold"), potions("Healing Potions"), dust("Pixi Dust"), bone("Bone Dust"), claw("Claw");
int records[20];


int main()
{
	Character character[5];
	int choice = 0;
	int c = 0;
	int d = 0;

	for (int j = 0; j < 5; j++)
	{
		initialize(character[j]);
	}
	
	Record record;

	character[0].name = "Eragon";
	character[1].name = "Jayden";
	character[2].name = "Crow";
	character[3].name = "Miyu";
	character[4].name = "Yuna";

	cout <<"Welcome to this trading simulator.\n";
	cout << "------------------------------------------------------------------------------\n";
	while (true)
	{
		cout << "What would you like to do?\n";
		cout << "------------------------------------------------------------------------------\n";
		cout << "1. Show Characters\n";
		cout << "2. Show Inventory\n";
		cout << "3. Trade items\n";
		cout << "4. Show Records\n";
		cout << "5. Exit\n";
		cout << "------------------------------------------------------------------------------\n";
		cin >> choice;

		switch (choice)
		{
		case 1:
			cout << "------------------------------------------------------------------------------\n";
			cout << "These are the characters.\n";
			cout << "------------------------------------------------------------------------------\n";
			cout << "0. Eragon" << endl;
			cout << "1. Jayden" << endl;
			cout << "2. Crow" << endl;
			cout << "3. Miyu" << endl;
			cout << "4. Yuna" << endl;
			cout << "------------------------------------------------------------------------------\n";
			break;

		case 2:
			for (int i = 0; i < 5; i++)
			{
				showInventory(character[i]);
			}
			break;

		case 3:
			//trading

		
			cout << "------------------------------------------------------------------------------\n";
			cout << "First trader: ";
			cin >> c;
			cout << "------------------------------------------------------------------------------\n";
			cout << "Second trader: ";
			cin >> d;
			trade(character[c], character[d]);
			

		
			break;

		case 4:

			displayR();

			break;

		case 5:
			return(0);
			break;

		default:
			break;

		}

	}


	system("pause");

}


void initialize(Character& c)
{
	c.item[0] = gold;
	c.item[1] = potions;
	c.item[2] = dust;
	c.item[3] = bone;
	c.item[4] = claw;
	for (int i = 0; i < 5; i++)
	{
		c.item[i].quanity = rand() % 100 + 1;
	}

}
void showInventory(Character& c)
{
	cout << "------------------------------------------------------------------------------\n";
	cout << c.name << "'s Inventory" << endl;
	cout << "------------------------------------------------------------------------------\n";
	for (int i = 0; i < 5; i++)
	{
		cout <<setw(2)<< i << setw(20) << left << c.item[i].itemName << setw(20) << c.item[i].quanity << endl;
		
	}
	cout << "------------------------------------------------------------------------------\n";
}
void trade(Character& a, Character& b )

	{
	int t, u,v, w;
	int h = 0;
	char choice1;
	if (a.name == b.name)
	{
		cout << "You cant trade with the samne person.";
		cout << "------------------------------------------------------------------------------\n";
		return;  // end the function
	}
	do {
		cout << right << setw(35) << "Trading Items" << endl;
		cout << "------------------------------------------------------------------------------\n";

		cout << a.name << "'s Inventory" << endl;
		cout << "------------------------------------------------------------------------------\n";
		for (int i = 0; i < 5; i++)

		{

			cout << i << setw(20) << left << a.item[i].itemName << setw(20) << a.item[i].quanity << endl;

		}
		cout << "------------------------------------------------------------------------------\n";
		cout << b.name << "'s Inventory" << endl;
		cout << "------------------------------------------------------------------------------\n";
		for (int i = 0; i < 5; i++)

		{

			cout << i << setw(20) << left << b.item[i].itemName << setw(20) << b.item[i].quanity << endl;

		}
		cout << "------------------------------------------------------------------------------\n";

		cout << "What would " << a.name << " like to trade: ";
		cin >> t;
		cout << "You are trading " << a.item[t].itemName << endl;
		cout << "how many: ";
		cin >> u;
		cout << "------------------------------------------------------------------------------\n";
		cout << "What would " << b.name << " like to trade: ";
		cin >> v;
		cout << "You are trading " << b.item[v].itemName << endl;
		cout << "how many: ";
		cin >> w;
		cout << "------------------------------------------------------------------------------\n";
		a.item[t].quanity -= u;
		a.item[v].quanity += w;
		cout << "Would you like to make another trade?";
		cin >> choice1;
	} while (choice1 == 'y' || choice1 =='Y' );
	{
		records[h]++;
	}
	if (choice1 == 'n' || choice1 == 'N')
	{
		records[h]++;
	}
	}

	void displayR()
	{
		for (int i = 0; i <= 20; i++)
		{
			if (i == 0)
			{
				cout << "There are no records.\n";
				return;

			}
			cout << i +1 << records[i] << endl;
			cout << records[i].name 

		}
	}
Last edited on
not sure what 'sowing' means, ("showing" ?? maybe??)

but you have an array of int:
int records[20]
which you attempt to hit with
records[i].name //no such thing

Record – Names of the traders, ...
I think that means the names of the two traders involved in the trade.
trade() - Receives 2 characters as parameters and handles a trade between them....
Your trade function prompts for multiple trades. move that logic outside the function.


I figured out what it was. I needed
1
2
3
4
5
6
7
8
9
else if (rCount > 0)
				cout << "------------------------------------------------------------------------------\n";
			cout << "First trader "<< record[i].charName1 
                                       <<" traded " << record[i].num1 << " of their " << record[i].itemTraded1 << " with " << endl;
			cout << "second trader "<< record[i].charName2 
                                     <<" who traded " << record[i].num2 << " of their " 
                                   << record[i].itemTraded2 << endl;
			cout << "------------------------------------------------------------------------------\n";
		


in my void display()

I also had to add
1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < 20; i++)
			{
				record[i].charName1 = a.name;
				record[i].charName2 = b.name;

				record[i].itemTraded1 = a.item[t].itemName;
				record[i].itemTraded2 = b.item[v].itemName;

				record[i].num1 = u;
				record[i].num2 = w;
			}
			rCount++;


to the end of my void trade so that the items were being placed inside of my array of record.
Last edited on
Here is my final code

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



int rCount = 0;


struct Item
{
	string itemName;
	int quanity;
	Item() {}

	Item(string n)
	{
		itemName= n;
	}



}; // end of the item struct

struct Character
{
	string name;
	Item item[5];

	Character() {}

	Character(string n)
	{
		name = n;
	}
};// end of the character struct

struct Record
{
	string charName1, charName2;
	string itemTraded1, itemTraded2;
	int num1 = 0, num2 = 0;

	
}; // end of the record struct

void displayR();
void showInventory(Character&);
void initialize(Character&);
void trade(Character&, Character&);

Item gold("Gold"), potions("Healing Potions"), dust("Pixie Dust"), bone("Bone Dust"), claw("Claw");

Record record[20];

int main()
{
	Character character[5];
	int choice = 0;
	int c = 0;
	int d = 0;

	for (int j = 0; j < 5; j++)
	{
		initialize(character[j]);
	}// end of for loop
	
	

	character[0].name = "Eragon";
	character[1].name = "Jayden";
	character[2].name = "Crow";
	character[3].name = "Miyu";
	character[4].name = "Yuna";

	cout <<"Welcome to this trading simulator.\n";
	cout << "------------------------------------------------------------------------------\n";
	while (true)
	{
		cout << "What would you like to do?\n";
		cout << "------------------------------------------------------------------------------\n";
		cout << right << setw(35) << "Main Menu\n";
		cout << "------------------------------------------------------------------------------\n";
		cout << "1. Show Characters\n";
		cout << "2. Show Inventory\n";
		cout << "3. Trade items\n";
		cout << "4. Show Records\n";
		cout << "5. Exit\n";
		cout << "------------------------------------------------------------------------------\n";
		cin >> choice;

		switch (choice)
		{
		case 1:
			cout << "------------------------------------------------------------------------------\n";
			cout << right << setw(35) << "Characters\n";
			cout << "------------------------------------------------------------------------------\n";
			cout << "0. Eragon" << endl;
			cout << "1. Jayden" << endl;
			cout << "2. Crow" << endl;
			cout << "3. Miyu" << endl;
			cout << "4. Yuna" << endl;
			cout << "------------------------------------------------------------------------------\n";
			break;

		case 2:
			cout << "------------------------------------------------------------------------------\n";
			cout << right << setw(35) << "Item Inventory\n";
			cout << "------------------------------------------------------------------------------\n";
			for (int i = 0; i < 5; i++)
			{
				
				showInventory(character[i]);
			}
			break;

		case 3:
			//trading

		
			cout << "------------------------------------------------------------------------------\n";
			cout << "First trader: ";
			cin >> c;
			if (c > 5 || c < 0)
			{
				cout << "That is not a valid character.\n";
				break;
			}
			cout << "------------------------------------------------------------------------------\n";
			cout << "Second trader: ";
			cin >> d;
			if (d > 5 || d < 0)
			{
				cout << "That is not a valid character.\n";
				break;
			}
			trade(character[c], character[d]);
			

		
			break;

		case 4:

			
			displayR();

			break;

		case 5:
			return(0);
			break;

		default:
			break;

		}// end of switch loop

	} // end of while loop


	system("pause");

}// end of int main 


void initialize(Character& c)
{
	c.item[0] = gold;
	c.item[1] = potions;
	c.item[2] = dust;
	c.item[3] = bone;
	c.item[4] = claw;
	for (int i = 0; i < 5; i++)
	{
		c.item[i].quanity = rand() % 100 + 1;
	}// end of for loop

}// end void initialize
void showInventory(Character& c)
{
	cout << "------------------------------------------------------------------------------\n";
	cout << c.name << "'s Inventory" << endl;
	cout << "------------------------------------------------------------------------------\n";
	for (int i = 0; i < 5; i++)
	{
		cout << i <<". "<< setw(20) << left << c.item[i].itemName << setw(20) << c.item[i].quanity << endl;
		
	}// end of for loop
	cout << "------------------------------------------------------------------------------\n";
}// end of void showInventory
void trade(Character& a, Character& b)

{
	int t, u, v, w;
	int h = 0;
	char choice1;
	if (a.name == b.name)
	{
		cout << "You can't trade with the samne person.";
		cout << "------------------------------------------------------------------------------\n";
		return;  // end the function
	}// end of if loop

		cout << right << setw(35) << "Trading Items" << endl;
		cout << "------------------------------------------------------------------------------\n";

		cout << a.name << "'s Inventory" << endl;
		cout << "------------------------------------------------------------------------------\n";
		for (int i = 0; i < 5; i++)

		{

			cout << i <<". "<< setw(20) << left << a.item[i].itemName << setw(20) << a.item[i].quanity << endl;

		} // end of for loop
		cout << "------------------------------------------------------------------------------\n";
		cout << b.name << "'s Inventory" << endl;
		cout << "------------------------------------------------------------------------------\n";
		for (int i = 0; i < 5; i++)

		{

			cout << i <<". "<< setw(20) << left << b.item[i].itemName << setw(20) << b.item[i].quanity << endl;

		} // end of for loop
		cout << "------------------------------------------------------------------------------\n";

		cout << "What would " << a.name << " like to trade: ";
		cin >> t;
		if (t > 5)
		{
			cout << "That is not a correct responce\n";
			return;
		}// end of if loop
		cout << "You are trading " << a.item[t].itemName << endl;
		cout << "how many: ";
		cin >> u;
		


		cout << "------------------------------------------------------------------------------\n";
		cout << "What would " << b.name << " like to trade: ";
		cin >> v;
		if (v > 5)
		{
			cout << "That is not a correct responce\n";
			return;
		}// end of if loop
		cout << "You are trading " << b.item[v].itemName << endl;
		cout << "how many: ";
		cin >> w;
		a.item[t].quanity -= u;
		a.item[v].quanity += w;
		b.item[v].quanity -= w;
		b.item[t].quanity += u;

		
		
			cout << "------------------------------------------------------------------------------\n";
			cout << "The trade successfully happened!\n";
			


			for (int i = 0; i < 20; i++)
			{
				record[i].charName1 = a.name;
				record[i].charName2 = b.name;

				record[i].itemTraded1 = a.item[t].itemName;
				record[i].itemTraded2 = b.item[v].itemName;

				record[i].num1 = u;
				record[i].num2 = w;
			}
			rCount++;
		

		


		
	

}// void of void trade

	void displayR()
	{
		
		for (int i = 0; i < rCount; i++)
		{
			if (rCount == 0)
			{
				cout << "There are no records.\n";
				return;

			} // end of if 
			else if (rCount > 0)
				cout << "------------------------------------------------------------------------------\n";
			cout << "First trader "<< record[i].charName1 <<" traded " << record[i].num1 << " of their " << record[i].itemTraded1 << " with " << endl;
			cout << "second trader "<< record[i].charName2 <<" who traded " << record[i].num2 << " of their " << record[i].itemTraded2 << endl;
			cout << "------------------------------------------------------------------------------\n";
			
		}// end of for
		
	}// end of void display 
Topic archived. No new replies allowed.