Adding files to arrays?

I'm writing a program that asks the user for input, and then takes that input, looks at another array, and puts the data into the new array. However, I realized I have no idea how to do this correctly, because the information that was just written to the array will get erased because I can't check to see which arrays have data in them already.

An example to better show what I'm talking about.
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
struct Character{
	string name;
	string weapon;
	string armor;
	string Class;
	string inv;
	int invamt;
} inventory[28];

struct Store{
	string itmname;
	float itmcost;
	int itmstock;
	int itmno;
	string itmdesc;
}  miscitems[3], weapons[12], armor[6];

int item;
				int mwa;
				cout << "What would you like to look at?" << endl << "1. Misc. Items" << endl << "2. Weapons" << endl << "3. Armor" << endl;
				cin >> mwa;
				if(mwa==1)
				{
					x = 0;
					cout << setw(30) << "Misc Items" << endl;
					cout << setw(3) << "No." << setw(10) << "Name" << setw(12) << "Cost" << setw(6) << "Stock" << setw(16) << "Description" << endl;
					while(x<3)
					{
						cout << left << setw(2) << miscitems[x].itmno << ". " << setw(16) << miscitems[x].itmname << "  " << setw(3) << miscitems[x].itmcost << "  " << setw(3) << miscitems[x].itmstock << "  " << miscitems[x].itmdesc << endl;
						x++;
					}
					cin >> item;
					if(item=miscitems[item-1].itmno)
					{
						int amtbuy;
						int fcost;
						cout << "So you'd like to buy: " << miscitems[item-1].itmname << "? We currently have: " << miscitems[item-1].itmstock << " in stock. Each one costs "
							<< miscitems[item-1].itmcost << " gp. So how many would you like?" << endl;
						cin >> amtbuy;
						if(fcost = miscitems[item - 1].itmcost * amtbuy>gold)
						{
							cout << "Sorry, I'm afraid you don't have enough gold for that.";
							x = 0;
						}
						else
						{
							gold -= (amtbuy*miscitems[item-1].itmcost);
							miscitems[item-1].itmstock -= amtbuy;
							cout << "You now have " << gold << " gp left.";
							x = 0;
						}
					}
				}
				else if(mwa==2)
				{
					x = 0;
					cout << endl << setw(30) << "Weapons" << endl;
					cout << setw(3) << "No." << setw(10) << "Name" << setw(12) << "Cost" << setw(6) << "Stock" << setw(16) << "Description" << endl;
					while(x<12)
					{
						cout << left << setw(2) << weapons[x].itmno << ". " << setw(16) << weapons[x].itmname << "  " << setw(3) << weapons[x].itmcost << "  " << setw(3) << weapons[x].itmstock << "  " << weapons[x].itmdesc << endl;
						x++;
					}
					cin >> item;
					if(item=weapons[item-1].itmno)
					{
						int amtbuy;
						int fcost;
						cout << "So you'd like to buy: " << weapons[item-1].itmname << "? We currently have: " << weapons[item-1].itmstock << " in stock. Each one costs "
							<< weapons[item-1].itmcost << " gp. So how many would you like?" << endl;
						cin >> amtbuy;
						if(fcost = weapons[item - 1].itmcost * amtbuy>gold)
						{
							cout << "Sorry, I'm afraid you don't have enough gold for that.";
							x = 0;
						}
						else
						{
							gold -= (amtbuy*weapons[item-1].itmcost);
							weapons[item-1].itmstock -= amtbuy;
							cout << "You now have " << gold << " gp left.";
							x = 0;
						}
					}
				}
				else if(mwa==3)
				{
					x = 0;
					cout << endl << setw(30) << "Armor" << endl;
					cout << setw(3) << "No." << setw(10) << "Name" << setw(12) << "Cost" << setw(6) << "Stock" << setw(16) << "Description" << endl;
					while(x<5)
					{
						cout << left << setw(2) << armor[x].itmno << ". " << setw(16) << armor[x].itmname << "  " << setw(3) << armor[x].itmcost << "  " << setw(3) << armor[x].itmstock << "  " << armor[x].itmdesc << endl;
						x++;
					}
						cin >> item;
					if(item=armor[item-1].itmno)
					{
						int amtbuy;
						int fcost;
						cout << "So you'd like to buy: " << armor[item-1].itmname << "? We currently have: " << armor[item-1].itmstock << " in stock. Each one costs "
							<< armor[item-1].itmcost << " gp. So how many would you like?" << endl;
						cin >> amtbuy;
						if(fcost = armor[item - 1].itmcost * amtbuy>gold)
						{
							cout << "Sorry, I'm afraid you don't have enough gold for that.";
							x = 0;
						}
						else
						{
							gold -= (amtbuy*armor[item-1].itmcost);
							armor[item-1].itmstock -= amtbuy;
							cout << "You now have " << gold << " gp left.";
							x = 0;
						}
					}
				}
				else
				{
					cout << "Invalid entry, returning to storefront." << endl;
					shop();
					}


Since I have no idea how to do what I'm asking for, I wasn't able to put in any code for it.
But what I'm asking, is after the user agrees to purchasing an item, I want it to be input into the inventory[28] array. Is there any way to check if a data cell of an array is filled, and if it is, check the next one?

..Sorry about the lack of comments in my code

Thanks!

Oh, yeah. Any variables I used in my code that isn't defined in the example, I have it defined somewhere else in the function, I just didn't copy that part over.

Last edited on
The way you're designing this is a bit confusing, at line 8, do you want to create an array of 28 variables of type Character? i.e
1
2
3
inventory[0].name = "matsom"; 
inventory[0].weapon = "shotgun";
//etc. 



But your question seems like you want your character to have access to an inventory containing the items that he purchased? i.e
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Character{
	string name;
	string weapon;
	string armor;
	string Class;
	string inv;
	int invamt;
        string inventory[28];//<---this?
};
//then you make an instance of your struct
Character matsom;//which makes the need for string member 'name' a bit redundant
matsom.weapon = "laser gun";
if(matsom.inventory[0] == "") //do something after confirming that it's empty 

Last edited on
Yeah, I do, so that the array stores all of the characters information, so I can make a save file at a later date.

I'm not sure what Character matsom; is doing. I understand that matsom is just the name you chose, but how is this more useful? What I want to do, is store all the information about each item in the array. Like, say the user went to the store, and bought 5 health packs. I want the array to take the information that the user bought health packs, and now has 5 of them, so that they can use each one.
Last edited on
well, okay, I guess I (kind of) understand what you want, so, to answer your question
Is there any way to check if a data cell of an array is filled, and if it is, check the next one?

this depends on your array type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//simple built-in types
int iArray[10] = {0};
if(iArray[0] == 0) //do something here
else if(iArray[1] == 0) //do something else
//etc.

//class/struct -standard string
string strArray[5] = {""};
if(strArray[0].empty()) //do something
else if(strArray[1].empty()) //do another

//user-defined types
Character inventory[28];
if(inventory[0].name.empty()) //make use of empty() member of the std string class
if(inventory[0].invamt == 0) //invamt is integer type 

this is probably the most elementary way to do what I think you want to do, so, you just need to create a member function of your struct (like std::string's empty()) that checks for those instances
Topic archived. No new replies allowed.