Having Variables in Identifiers

How do I put variables (in this case, x) into identifiers (such as ArmoryLocker*, where * is a predetermined number)?

I have underlined all the variables added to identifiers.

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

#define HIMi 0
#define HI 1
#define HIMa 2
#define EMK 3
#define ArmorHead 4
#define ArmorChest 5
#define ArmorHands 6
#define ArmorLegs 7
#define ArmorFeet 8
#define Item_Types 9

struct Inventory { int counts[Item_Types] ; } UserInventory = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, ArmoryLocker1 = { 1, 0, 0, 1, 0, 0, 0, 0, 0 }, ArmoryLocker5 = { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		ArmoryLocker8 = { 0, 0, 0, 0, 2, 0, 1, 0, 1 } ;

int x, y, PlayerArmor;
string ArmoryAction, LockerAction;

int main(){
cout << endl << " You have entered the Armory." << endl;
ArmorySelection:
cout << endl << " What would you like to do?" << endl;
cout << "1. Open Locker 1" << endl;
cout << "2. Open Locker 5" << endl;
cout << "3. Open Locker 8" << endl;
cout << "4. Leave" << endl;
cout << endl << " ";
getline(cin, ArmoryAction);

if(ArmoryAction == "1"){
	x = 1;
	y = 1; }
else if(ArmoryAction == "2"){
	x = 5;
	y = 1; }
else if(ArmoryAction == "3"){
	x = 8;
	y = 1; }

if(ArmoryAction == "4"){
	y = 1; 
	... }
else if(y != 1){
	cout << endl << endl << " Invalid command." << endl << endl;
	goto ArmorySelection; }
else{
	cout << endl << " You open Locker " << x << "." << endl;
	Locker:
	if(ArmoryLocker(x).counts[HIMi] > 0){
		cout << endl << "   This locker contains " << ArmoryLocker(x).counts[HIMi] << (ArmoryLocker(x).counts[HIMi] == 1 ? " Health Injector (Minor)." : " Health Injectors (Minor)."); }
	if(ArmoryLocker(x).counts[HI] > 0){
		cout << endl << "   This locker contains " << ArmoryLocker(x).counts[HI] << (ArmoryLocker(x).counts[HI] == 1 ? " Health Injector." : " Health Injectors."); }
	if(ArmoryLocker(x).counts[HIMa] > 0){
		cout << endl << "   This locker contains " << ArmoryLocker(x).counts[HIMa] << (ArmoryLocker(x).counts[HIMa] == 1 ? " Health Injector (Major)." : " Health Injectors (Major)."); }
	if(ArmoryLocker(x).counts[EMK] > 0){
		cout << endl << "   This locker contains " << ArmoryLocker(x).counts[EMK] << (ArmoryLocker(x).counts[EMK] == 1 ? " Emergency Medical Kit." : " Emergency Medical Kits."); }
	if(ArmoryLocker(x).counts[ArmorHead] > 0){
		cout << endl << "   This locker contains a Helmet with an Armor Value of " << ArmoryLocker(x).counts[ArmorHead]; }
	if(ArmoryLocker(x).counts[ArmorChest] > 0){
		cout << endl << "   This locker contains a Chestpiece with an Armor Value of " << ArmoryLocker(x).counts[ArmorChest]; }
	if(ArmoryLocker(x).counts[ArmorHands] > 0){
		cout << endl << "   This locker contains Gloves with an Armor Value of " << ArmoryLocker(x).counts[ArmorHands]; }
	if(ArmoryLocker(x).counts[ArmorLegs] > 0){
		cout << endl << "   This locker contains Leggings with an Armor Value of " << ArmoryLocker(x).counts[ArmorLegs]; }
	if(ArmoryLocker(x).counts[ArmorFeet] > 0){
		cout << endl << "   This locker contains Boots with an Armor Value of " << ArmoryLocker(x).counts[ArmorFeet]; }
	if(ArmoryLocker(x).counts[HIMi] == 0 && ArmoryLocker(x).counts[HI] == 0 && ArmoryLocker(x).counts[HIMa] == 0 && ArmoryLocker(x).counts[EMK] == 0 && ArmoryLocker(x).counts[ArmorHead]
	== 0 && ArmoryLocker(x).counts[ArmorChest] == 0 && ArmoryLocker(x).counts[ArmorHands] == 0 && ArmoryLocker(x).counts[ArmorLegs] == 0 && ArmoryLocker(x).counts[ArmorFeet] == 0){
		cout << endl << " The locker is empty." << endl;
		goto ArmorySelection; }
	LockerSelection:
	cout << endl << endl << " What would you like to do?" << endl;
	cout << "1. Take the items" << endl;
	cout << "2. Leave" << endl;
	cout << endl << " ";
	getline(cin, LockerAction);

	if(LockerAction == "1"){
		cout << endl << " You have taken the following items.";
		if(ArmoryLocker(x).counts[HIMi] > 0){
			cout << endl << ArmoryLocker(x).counts[HIMi] << (ArmoryLocker(x).counts[HIMi] == 1 ? "  Health Injector (Minor)" : "  Health Injectors (Minor)"); }
		else { cout << ""; }
		if(ArmoryLocker(x).counts[HI] > 0){
			cout << endl << ArmoryLocker(x).counts[HI] << (ArmoryLocker(x).counts[HI] == 1 ? "  Health Injector" : "  Health Injectors"); }
		else { cout << ""; }
		if(ArmoryLocker(x).counts[HIMa] > 0){
			cout << endl << ArmoryLocker(x).counts[HIMa] << (ArmoryLocker(x).counts[HIMa] == 1 ? "  Health Injector (Major)" : "  Health Injectors (Major)"); }
		else { cout << ""; }
		if(ArmoryLocker(x).counts[EMK] > 0){
				cout << endl << ArmoryLocker(x).counts[EMK] << (ArmoryLocker(x).counts[EMK] == 1 ? "  Emergency Medical Kit" : "  Emergency Medical Kits"); }
		else { cout << ""; }
		if(ArmoryLocker(x).counts[ArmorHead] > 0){
				cout << endl << "     A Helmet with an Armor Value of " << ArmoryLocker(x).counts[ArmorHead]; }
		else { cout << ""; }
		if(ArmoryLocker(x).counts[ArmorChest] > 0){
			cout << endl << "     A Chestpiece with an Armor Value of " << ArmoryLocker(x).counts[ArmorChest]; }
		else { cout << ""; }
		if(ArmoryLocker(x).counts[ArmorHands] > 0){
			cout << endl << "     Gloves with an Armor Value of " << ArmoryLocker(x).counts[ArmorHands]; }
		else { cout << ""; }
		if(ArmoryLocker(x).counts[ArmorLegs] > 0){
				cout << endl << "     Leggings with an Armor Value of " << ArmoryLocker(x).counts[ArmorLegs]; }
		else { cout << ""; }
		if(ArmoryLocker(x).counts[ArmorFeet] > 0){
			cout << endl << "     Boots with an Armor Value of " << ArmoryLocker(x).counts[ArmorFeet]; }
		else { cout << ""; }

		UserInventory.counts[HIMi] = UserInventory.counts[HIMi] + ArmoryLocker(x).counts[HIMi];
		UserInventory.counts[HI] = UserInventory.counts[HI] + ArmoryLocker(x).counts[HI];
		UserInventory.counts[HIMa] = UserInventory.counts[HIMa] + ArmoryLocker(x).counts[HIMa];
		UserInventory.counts[EMK] = UserInventory.counts[EMK] + ArmoryLocker(x).counts[EMK];

		if(ArmoryLocker(x).counts[ArmorHead] > UserInventory.counts[ArmorHead]){
			UserInventory.counts[ArmorHead] = ArmoryLocker(x).counts[ArmorHead]; }
		if(ArmoryLocker(x).counts[ArmorChest] > UserInventory.counts[ArmorChest]){
			UserInventory.counts[ArmorChest] = ArmoryLocker(x).counts[ArmorChest]; }
		if(ArmoryLocker(x).counts[ArmorHands] > UserInventory.counts[ArmorHands]){
			UserInventory.counts[ArmorHands] = ArmoryLocker(x).counts[ArmorHands]; }
		if(ArmoryLocker(x).counts[ArmorLegs] > UserInventory.counts[ArmorLegs]){
			UserInventory.counts[ArmorLegs] = ArmoryLocker(x).counts[ArmorLegs]; }
		if(ArmoryLocker(x).counts[ArmorFeet] > UserInventory.counts[ArmorFeet]){
			UserInventory.counts[ArmorFeet] = ArmoryLocker(x).counts[ArmorFeet]; }

		if(ArmoryLocker(x).counts[ArmorHead] > 0 || ArmoryLocker(x).counts[ArmorChest] > 0 || ArmoryLocker(x).counts[ArmorHands] > 0 || ArmoryLocker(x).counts[ArmorLegs]
		> 0 || ArmoryLocker(x).counts[ArmorFeet] > 0){
			PlayerArmor = UserInventory.counts[ArmorHead] + UserInventory.counts[ArmorChest] + UserInventory.counts[ArmorHands] + UserInventory.counts[ArmorLegs]
		+ UserInventory.counts[ArmorFeet];
			cout << endl << endl << "     New Armor Value: " << PlayerArmor; }
		else { cout << ""; }

		ArmoryLocker(x).counts[HIMi] = 0; ArmoryLocker(x).counts[HI] = 0; ArmoryLocker(x).counts[HIMa] = 0; ArmoryLocker(x).counts[EMK] = 0;
		ArmoryLocker(x).counts[ArmorHead] = 0; ArmoryLocker(x).counts[ArmorChest] = 0; ArmoryLocker(x).counts[ArmorHands] = 0; ArmoryLocker(x).counts[ArmorLegs] = 0;
		ArmoryLocker(x).counts[ArmorFeet] = 0;
		goto Locker; }
	else if(LockerAction == "Leave" || LockerAction == "2"){
		goto ArmorySelection; }
	else{
		cout << " Invalid command.";
		goto LockerSelection; } } } }
You can't.

When your code is compiled and turned into an exe, the variable names don't really do anything anymore. If you build in Release mode (at least in VS), then I don't think that variable names are retained. Therefore identifying or editing variable names at runtime (while the program is running) is impossible.

What you CAN do is make ArmoryLocker an array.

Example:
1
2
3
4
5
6
7
8
9
10
Inventory AmoryLocker[8] = 
{  { 1, 0, 0, 1, 0, 0, 0, 0, 0}, //AmoryLocker[0] contents
   { 1, 0, 0, 1, 0, 0, 0, 0, 0}, //AmoryLocker[1] contents
   { 0, 0, 0, 0, 2, 0, 1, 0, 1}, //AmoryLocker[2] contents
   { 1, 0, 0, 1, 0, 0, 0, 0, 0}, //AmoryLocker[3] contents
   { 1, 0, 0, 1, 0, 0, 0, 0, 0}, //AmoryLocker[4] contents
   { 0, 0, 0, 0, 2, 0, 1, 0, 1}, //AmoryLocker[5] contents
   { 1, 0, 0, 1, 0, 0, 0, 0, 0}, //AmoryLocker[6] contents
   { 1, 0, 0, 1, 0, 0, 0, 0, 0}, //AmoryLocker[7] contents
};
Last edited on
Alright, I can see how that would work.

But I've never been very good with arrays. One line, as in my code, was easy enough to understand, but something like that I wouldn't even know where to begin with defining it. Perhaps I'm just too tired to work it through, so how would defining Inventory ArmoryLocker[8] be put into the checks for items?

Specifically, how would it fit into
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(ArmoryLocker(x).counts[HIMi] > 0){
	cout << endl << "   This locker contains " << ArmoryLocker(x).counts[HIMi] << (ArmoryLocker(x).counts[HIMi] == 1 ? " Health Injector (Minor)." : " Health Injectors (Minor)."); }
if(ArmoryLocker(x).counts[HI] > 0){
	cout << endl << "   This locker contains " << ArmoryLocker(x).counts[HI] << (ArmoryLocker(x).counts[HI] == 1 ? " Health Injector." : " Health Injectors."); }
if(ArmoryLocker(x).counts[HIMa] > 0){
	cout << endl << "   This locker contains " << ArmoryLocker(x).counts[HIMa] << (ArmoryLocker(x).counts[HIMa] == 1 ? " Health Injector (Major)." : " Health Injectors (Major)."); }
if(ArmoryLocker(x).counts[EMK] > 0){
	cout << endl << "   This locker contains " << ArmoryLocker(x).counts[EMK] << (ArmoryLocker(x).counts[EMK] == 1 ? " Emergency Medical Kit." : " Emergency Medical Kits."); }
if(ArmoryLocker(x).counts[ArmorHead] > 0){
	cout << endl << "   This locker contains a Helmet with an Armor Value of " << ArmoryLocker(x).counts[ArmorHead]; }
if(ArmoryLocker(x).counts[ArmorChest] > 0){
	cout << endl << "   This locker contains a Chestpiece with an Armor Value of " << ArmoryLocker(x).counts[ArmorChest]; }
if(ArmoryLocker(x).counts[ArmorHands] > 0){
	cout << endl << "   This locker contains Gloves with an Armor Value of " << ArmoryLocker(x).counts[ArmorHands]; }
if(ArmoryLocker(x).counts[ArmorLegs] > 0){
	cout << endl << "   This locker contains Leggings with an Armor Value of " << ArmoryLocker(x).counts[ArmorLegs]; }
if(ArmoryLocker(x).counts[ArmorFeet] > 0){
	cout << endl << "   This locker contains Boots with an Armor Value of " << ArmoryLocker(x).counts[ArmorFeet]; }


I only have 3 useable lockers, so I don't want to have to repeat that check more than that. I'm trying to avoid as much repetition as I can get.

The code I have now (with the x variable that I now know is pointless) I boiled down from 3 lengthy checks where x was actually 1, 5, and 8 for the 3 different instances.
I would replace your code with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (int i = 0; i < 8; i++)
{
	if(ArmoryLocker[i].counts[HIMi] > 0){
		cout << endl << "   This locker contains " << ArmoryLocker[i].counts[HIMi] << (ArmoryLocker[i].counts[HIMi] == 1 ? " Health Injector (Minor)." : " Health Injectors (Minor)."); }
	if(ArmoryLocker[i].counts[HI] > 0){
		cout << endl << "   This locker contains " << ArmoryLocker[i].counts[HI] << (ArmoryLocker[i].counts[HI] == 1 ? " Health Injector." : " Health Injectors."); }
	if(ArmoryLocker[i].counts[HIMa] > 0){
		cout << endl << "   This locker contains " << ArmoryLocker[i].counts[HIMa] << (ArmoryLocker[i].counts[HIMa] == 1 ? " Health Injector (Major)." : " Health Injectors (Major)."); }
	if(ArmoryLocker[i].counts[EMK] > 0){
		cout << endl << "   This locker contains " << ArmoryLocker[i].counts[EMK] << (ArmoryLocker[i].counts[EMK] == 1 ? " Emergency Medical Kit." : " Emergency Medical Kits."); }
	if(ArmoryLocker[i].counts[ArmorHead] > 0){
		cout << endl << "   This locker contains a Helmet with an Armor Value of " << ArmoryLocker[i].counts[ArmorHead]; }
	if(ArmoryLocker[i].counts[ArmorChest] > 0){
		cout << endl << "   This locker contains a Chestpiece with an Armor Value of " << ArmoryLocker[i].counts[ArmorChest]; }
	if(ArmoryLocker[i].counts[ArmorHands] > 0){
		cout << endl << "   This locker contains Gloves with an Armor Value of " << ArmoryLocker[i].counts[ArmorHands]; }
	if(ArmoryLocker[i].counts[ArmorLegs] > 0){
		cout << endl << "   This locker contains Leggings with an Armor Value of " << ArmoryLocker[i].counts[ArmorLegs]; }
	if(ArmoryLocker[i].counts[ArmorFeet] > 0){
		cout << endl << "   This locker contains Boots with an Armor Value of " << ArmoryLocker[i].counts[ArmorFeet]; }
}
If I'm not mistaken, that would output every item in all lockers at once.


I believe I found a way around the problem by locking the values into temporary variables, then setting the values to 0. On exiting a locker, any items that remain are put back into their values and the temporary variables are set to 0 again.

The code is about 100 lines longer, but that's still over 250 lines shorter than when I started.

The important bits:

# is a set value, 1, 5, or 8. This code repeats 3 times in else if statements for each locker.
I may eventually change this to a for loop as suggested, where _ is the same number as # through a single variable. This will lead to many more variables, but will accomplish the same goal without repeating code.[/b]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
else if(ArmoryAction == "#" || ArmoryAction == "Open Locker #"){
	HIMi_ = ArmoryLocker#.counts[HIMi];
	HI_ = ArmoryLocker#.counts[HI];
	HIMa_ = ArmoryLocker#.counts[HIMa];
	EMK_ = ArmoryLocker#.counts[EMK];
	ArHead_ = ArmoryLocker#.counts[ArmorHead];
	ArChes_ = ArmoryLocker#.counts[ArmorChest];
	ArHand_ = ArmoryLocker#.counts[ArmorHands];
	ArLegs_ = ArmoryLocker#.counts[ArmorLegs];
	ArFeet_ = ArmoryLocker#.counts[ArmorFeet];
	ArmoryLocker#.counts[HIMi] = 0;
	ArmoryLocker#.counts[HI] = 0;
	ArmoryLocker#.counts[HIMa] = 0;
	ArmoryLocker#.counts[EMK] = 0;
	ArmoryLocker#.counts[ArmorHead] = 0;
	ArmoryLocker#.counts[ArmorChest] = 0;
	ArmoryLocker#.counts[ArmorHands] = 0;
	ArmoryLocker#.counts[ArmorLegs] = 0;
	ArmoryLocker#.counts[ArmorFeet] = 0;
	y = #; } 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cout << endl << " You open Locker " << y << "." << endl;
Locker:
if(HIMi_ > 0){
	cout << endl << "   This locker contains " << HIMi_ << (HIMi_ == 1 ? " Health Injector (Minor)." : " Health Injectors (Minor)."); }
if(HI_ > 0){
	cout << endl << "   This locker contains " << HI_ << (HI_ == 1 ? " Health Injector." : " Health Injectors."); }
if(HIMa_ > 0){
	cout << endl << "   This locker contains " << HIMa_ << (HIMa_ == 1 ? " Health Injector (Major)." : " Health Injectors (Major)."); }
if(EMK_ > 0){
	cout << endl << "   This locker contains " << EMK_ << (EMK_ == 1 ? " Emergency Medical Kit." : " Emergency Medical Kits."); }
if(ArHead_ > 0){
	cout << endl << "   This locker contains a Helmet with an Armor Value of " << ArHead_; }
if(ArChes_ > 0){
	cout << endl << "   This locker contains a Chestpiece with an Armor Value of " << ArChes_; }
if(ArHand_ > 0){
	cout << endl << "   This locker contains Gloves with an Armor Value of " << ArHand_; }
if(ArLegs_ > 0){
	cout << endl << "   This locker contains Leggings with an Armor Value of " << ArLegs_; }
if(ArFeet_ > 0){
	cout << endl << "   This locker contains Boots with an Armor Value of " << ArFeet_; }

if(HIMi_ == 0 && HI_ == 0 && HIMa_ == 0 && EMK_ == 0 && ArHead_ == 0 && ArChes_ == 0 && ArHand_ == 0 && ArLegs_ == 0 && ArFeet_ == 0){
	cout << endl << " The locker is empty." << endl;
	goto ArmorySelection; }


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
if(LockerAction == "1" || LockerAction == "Take items"){
	cout << endl << endl << " You have taken the following items.";
	if(HIMi_ > 0){
		cout << endl << HIMi_ << (HIMi_ == 1 ? "  Health Injector (Minor)" : "  Health Injectors (Minor)"); }
	else { cout << ""; }
	if(HI_ > 0){
		cout << endl << HI_ << (HI_ == 1 ? "  Health Injector" : "  Health Injectors"); }
	else { cout << ""; }
	if(HIMa_ > 0){
		cout << endl << HIMa_ << (HIMa_ == 1 ? "  Health Injector (Major)" : "  Health Injectors (Major)"); }
	else { cout << ""; }
	if(EMK_ > 0){
		cout << endl << EMK_ << (EMK_ == 1 ? "  Emergency Medical Kit" : "  Emergency Medical Kits"); }
	else { cout << ""; }
	if(ArHead_ > 0){
		cout << endl << "     A Helmet with an Armor Value of " << ArHead_; }
	else { cout << ""; }
	if(ArChes_ > 0){
		cout << endl << "     A Chestpiece with an Armor Value of " << ArChes_; }
	else { cout << ""; }
	if(ArHand_ > 0){
		cout << endl << "     Gloves with an Armor Value of " << ArHand_; }
	else { cout << ""; }
	if(ArLegs_ > 0){
		cout << endl << "     Leggings with an Armor Value of " << ArLegs_; }
	else { cout << ""; }
	if(ArFeet_ > 0){
		cout << endl << "     Boots with an Armor Value of " << ArFeet_; }
	else { cout << ""; }

	UserInventory.counts[HIMi] = UserInventory.counts[HIMi] + HIMi_;
	UserInventory.counts[HI] = UserInventory.counts[HI] + HI_;
	UserInventory.counts[HIMa] = UserInventory.counts[HIMa] + HIMa_;
	UserInventory.counts[EMK] = UserInventory.counts[EMK] + EMK_;

	if(ArHead_ > UserInventory.counts[ArmorHead]){
		UserInventory.counts[ArmorHead] = ArHead_; }
	if(ArChes_ > UserInventory.counts[ArmorChest]){
		UserInventory.counts[ArmorChest] = ArChes_; }
	if(ArHand_ > UserInventory.counts[ArmorHands]){
		UserInventory.counts[ArmorHands] = ArHand_; }
	if(ArLegs_ > UserInventory.counts[ArmorLegs]){
		UserInventory.counts[ArmorLegs] = ArLegs_; }
	if(ArFeet_ > UserInventory.counts[ArmorFeet]){
		UserInventory.counts[ArmorFeet] = ArFeet_; }

	if(ArHead_ > 0 || ArChes_ > 0 || ArHand_ > 0 || ArLegs_ > 0 || ArFeet_ > 0){
		PlayerArmor = UserInventory.counts[ArmorHead] + UserInventory.counts[ArmorChest] + UserInventory.counts[ArmorHands] + UserInventory.counts[ArmorLegs] + UserInventory.counts[ArmorFeet];
		cout << endl << endl << "     New Armor Value: " << PlayerArmor; }
	else { cout << ""; }

	HIMi_ = 0; HI_ = 0; HIMa_ = 0; EMK_ = 0;
	ArHead_ = 0; ArChes_ = 0; ArHand_ = 0; ArLegs_ = 0; ArFeet_ = 0;
	goto LockerItems; }
else if(LockerAction == "Leave" || LockerAction == "2"){
	goto LockerItems; }
else{
	cout << " Invalid command.";
	goto LockerSelection; }


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
LockerItems:
if(y == 1){
	ArmoryLocker1.counts[HIMi] = HIMi_;
	ArmoryLocker1.counts[HI] = HI_;
	ArmoryLocker1.counts[HIMa] = HIMa_;
	ArmoryLocker1.counts[EMK] = EMK_;
	ArmoryLocker1.counts[ArmorHead] = ArHead_;
	ArmoryLocker1.counts[ArmorChest] = ArChes_;
	ArmoryLocker1.counts[ArmorHands] = ArHand_;
	ArmoryLocker1.counts[ArmorLegs] = ArLegs_;
	ArmoryLocker1.counts[ArmorFeet] = ArFeet_;
	goto ArmorySelection; }
else if(y == 5){
	ArmoryLocker5.counts[HIMi] = HIMi_;
	ArmoryLocker5.counts[HI] = HI_;
	ArmoryLocker5.counts[HIMa] = HIMa_;
	ArmoryLocker5.counts[EMK] = EMK_;
	ArmoryLocker5.counts[ArmorHead] = ArHead_;
	ArmoryLocker5.counts[ArmorChest] = ArChes_;
	ArmoryLocker5.counts[ArmorHands] = ArHand_;
	ArmoryLocker5.counts[ArmorLegs] = ArLegs_;
	ArmoryLocker5.counts[ArmorFeet] = ArFeet_;
	goto ArmorySelection; }
else if(y == 8){
	ArmoryLocker8.counts[HIMi] = HIMi_;
	ArmoryLocker8.counts[HI] = HI_;
	ArmoryLocker8.counts[HIMa] = HIMa_;
	ArmoryLocker8.counts[EMK] = EMK_;
	ArmoryLocker8.counts[ArmorHead] = ArHead_;
	ArmoryLocker8.counts[ArmorChest] = ArChes_;
	ArmoryLocker8.counts[ArmorHands] = ArHand_;
	ArmoryLocker8.counts[ArmorLegs] = ArLegs_;
	ArmoryLocker8.counts[ArmorFeet] = ArFeet_;
	goto ArmorySelection; }
else{
	cout << endl << endl << "An error has occurred." << endl << endl;
	system("pause");
	return 0;
}
Last edited on
I would be tempted to consider something along these lines:
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
#include <iostream>

using namespace std;

struct Inventory
{
	int HIMi;
	int HI;
	int HIMa;
	int EMK;
	int ArmorHead;
	int ArmorChest;
	int ArmorHands;
	int ArmorLegs;
	int ArmorFeet;
	int Item_Types;
};


int main()
{
	Inventory UserInventory;
	Inventory ArmyLocker[8];

	int x = 1; // number between 0-7 set by ArmyAction

	if(ArmoryLocker[x].HIMi > 0)
		cout << endl << "   This locker contains " << ArmoryLocker[x].HIMi << (ArmoryLocker[x].HIMi == 1 ? " Health Injector (Minor)." : " Health Injectors (Minor).");

	if(ArmoryLocker[x].HI > 0)
		cout << endl << "   This locker contains " << ArmoryLocker[x].HI << (ArmoryLocker[x].HI == 1 ? " Health Injector." : " Health Injectors.");

	if(ArmoryLocker[x].HIMa > 0)
		cout << endl << "   This locker contains " << ArmoryLocker[x].HIMa << (ArmoryLocker[x].HIMa == 1 ? " Health Injector (Major)." : " Health Injectors (Major).");

	if(ArmoryLocker[x].EMK > 0)
		cout << endl << "   This locker contains " << ArmoryLocker[x].EMK << (ArmoryLocker[x].EMK == 1 ? " Emergency Medical Kit." : " Emergency Medical Kits.");

	if(ArmoryLocker[x].ArmorHead > 0)
		cout << endl << "   This locker contains a Helmet with an Armor Value of " << ArmoryLocker[x].ArmorHead;

	if(ArmoryLocker[x].ArmorChest > 0)
		cout << endl << "   This locker contains a Chestpiece with an Armor Value of " << ArmoryLocker[x].ArmorChest;

	if(ArmoryLocker[x].ArmorHands > 0)
		cout << endl << "   This locker contains Gloves with an Armor Value of " << ArmoryLocker[x].ArmorHands;

	if(ArmoryLocker[x].ArmorLegs > 0)
		cout << endl << "   This locker contains Leggings with an Armor Value of " << ArmoryLocker[x].ArmorLegs;

	if(ArmoryLocker[x].ArmorFeet > 0)
		cout << endl << "   This locker contains Boots with an Armor Value of " << ArmoryLocker[x].ArmorFeet;



}
Then something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	HIMi_ = ArmoryLocker#.counts[HIMi];
	HI_ = ArmoryLocker#.counts[HI];
	HIMa_ = ArmoryLocker#.counts[HIMa];
	EMK_ = ArmoryLocker#.counts[EMK];
	ArHead_ = ArmoryLocker#.counts[ArmorHead];
	ArChes_ = ArmoryLocker#.counts[ArmorChest];
	ArHand_ = ArmoryLocker#.counts[ArmorHands];
	ArLegs_ = ArmoryLocker#.counts[ArmorLegs];
	ArFeet_ = ArmoryLocker#.counts[ArmorFeet];
	ArmoryLocker#.counts[HIMi] = 0;
	ArmoryLocker#.counts[HI] = 0;
	ArmoryLocker#.counts[HIMa] = 0;
	ArmoryLocker#.counts[EMK] = 0;
	ArmoryLocker#.counts[ArmorHead] = 0;
	ArmoryLocker#.counts[ArmorChest] = 0;
	ArmoryLocker#.counts[ArmorHands] = 0;
	ArmoryLocker#.counts[ArmorLegs] = 0;
	ArmoryLocker#.counts[ArmorFeet] = 0; 

Can be reduced to this:
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
struct Inventory
{
	int HIMi;
	int HI;
	int HIMa;
	int EMK;
	int ArmorHead;
	int ArmorChest;
	int ArmorHands;
	int ArmorLegs;
	int ArmorFeet;
	int Item_Types;
};

const Inventory EmptyInventory = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

int main()
{
	Inventory UserInventory;
	Inventory ArmyLocker[8];

	int x = 2; // select from ArmoryAction

	// Just 2 lines
	Inventory temp = ArmyLocker[x];

	ArmyLocker[x] = EmptyInventory;


}
Last edited on
You wouldn't even need int Item_Types or the 10th 0 in the array, from the looks of that. Let me fiddle around with that idea and see how it turns out. A reduction like that would be very significant.

I'm guessing that Inventory x takes the array { 0, 0, 0, 0, 0, 0, 0, 0, 0 } and puts each one into HIMi, then HI, then HIMa, and so on? I've never really done a whole lot with arrays, but they're a good addition.

I'm also still a bit confused about the variables in the identifiers. It didn't work the way I was trying to do it (I switched the brackets out for parentheses), so I figured it was because it had to wait for user input. Yet your example does the same. What is the difference? Maybe I'm reading it wrong.


Edit: At any rate, I was too lazy to reconfigure my entire inventory system to account for a single array declaration, so instead I just used your idea to make Inventory Empty and Temp to replace the temporary variables and the long list of setting everything equal to 0. Cut down quite a lot.
Last edited on
Topic archived. No new replies allowed.