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.