I am trying to write a program for an inventory. Each item needs to have a name and a number associated with it. I have no idea about how to go about creating the arrays for this. Is there any way that I can make an array element have more than one character/integer in it? For example can I make Names[0]= "Bread" and Names[1]= "cheese"? And would it be smart to create a second array for the numbers? Can I make Numbers[0]= 1001 and Numbers[1]= 1002?
Eventually, I will have to sort, search, and count the number of items in the inventory. Any ideas? Thanks!
s there any way that I can make an array element have more than one character/integer in it?
You could either create an array of pairs with type string and int or a struct/class with 2 data members and make an array of those. I'm assuming you have to use arrays for this assignment, otherwise I'd suggest using a map.
Is there an easy way I could use multidimensional arrays to do this? And could someone maybe post a light example, I am just totally stumped on this. This is what I have so far:
void menuSelection(int &selection, int i, int Items[]);
void performSelection(int &selection, int Items[], int &i);
int main(int argc, char *argv[])
{
int selection;
int Itemnumbers[];
string Itemnames[];
int i;
i=0;
menuSelection(selection, i, Items);
performSelection(selection, Items, i);
system("PAUSE");
return EXIT_SUCCESS;
}
void menuSelection(int &selection, int i, int Items[])
{
cout << "1. Load Inventory" << endl;
cout << "2. Add Item" << endl;
cout << "3. Search for Item" << endl;
cout << "4. List Items" << endl;
cout << "5. Save Inventory" << endl;
cout << "0. End Program" << endl;
cout << "Number of Items in Inventory: " << i << endl;
cout << endl;
cout << "Enter the number of the action you would like to do: " << endl;
cin >> selection;
}
void performSelection(int &selection, int Items[], int &i)
{
if (selection==1)
{
ifstream inputFile;
string filename;
string line;
cout << "Enter the filename where the inventory is stored:" << endl;
cin >> filename;
inputFile.open(filename.c_str());
}
if (selection==2)
{
}
}
Under selection==2 I need to add an item to the inventory that has both a number and a name associated with it. So I am stumped about if there is a way I can create an "array of arrays" in a sense that can be sorted and searched later on. Or am I totally on the wrong track here??
Hey, First of all I see you have a piece of code that says int i; then i = 0; this is no biggy but you can declare and initialize this variable on one line like this: int i = 0;
Ok you don't really explain what it is you want type of inventory you want to make but for a name and a number you could do this:-
void Inventory::setNumber(int newNumber)
{
number = newNumber;
}
int Inventory::getNumber()
{
return number;
}
void Inventory::setName(string newName)
{
name = newName;
}
string Inventory::getName()
{
return name;
}
int main()
{
//Now just make a vector of this class type in main()
vector<Inventory> inventory;
//Make some variables
Inventory userInput;
int numberInput;
string nameInput;
//loop for how many inventory items you want I just made
//it a constant 5 but you might want a more dynamic number
for(int i = 0;i < 5;i++)
{
cout << "Type in a name" << endl;
cin >> nameInput;
cout << "Type in a number" << endl;
cin >> numberInput;
//store these values to userInput
userInput.setName(nameInput);
userInput.setNumber(numberInput);
//add this to the next element of the vector
inventory.push_back(userInput);
}
//pause before EXIT
cout << "Press ENTER to continue";
cin.ignore(cin.rdbuf()->in_avail() + 1);
vector<vector<string>> is a 2D vector (or simply a vector of vectors) storing strings. V[0][0] = {{"1001"}, {"BREAD"}}; is therefore not valid as the RHS is not a string.
[EDITED]
will it assign 1001 to the row and BREAD to the column?
Hey like wmheric said a vector of objects would be best for what you are trying to achieve which is why I wrote out a class which contains an int and a string and then made a vector of this class.
Otherwise you cannot have an int and a string only one or the other.
example
vector<int> age;
vector<string> name;
In the vector age only integer can be stored and in the vector string only strings can be stored.
Using a combination of a class and vectors is very powerful. I would suggest you spend some time learning how to do both as it will benefit your programming.
How exactly does work wmheric?
Do I just write then write my program and then at the end without the [] ?