Array Elements

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!
Last edited on
Hmm...create a class Item with two variables and store the Items in the array?
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.

http://cplusplus.com/reference/std/utility/pair/
http://cplusplus.com/doc/tutorial/structures/
Last edited on
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:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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:-

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Inventory
{
private:
int number;
string name;

public:
void setNumber(int newNumber);
int getNumber();
void setName(string newName);
string getName();
};

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);

//EXIT success
return 0;
}

Hope this helps

Steve






wow, that is very confusing... is there a way I could use a 2D vector for this? I could declare it like:

vector<vector<string>> V;

I have never done multidimensional anything before so some questions:

if I say:

V[0][0]= {{"1001"}, {"BREAD"}}

will it assign 1001 to the row and BREAD to the column? And then when I say:

cout << V[0][0] << endl;

will it print "1001BREAD" ?

and say I wanted to only print the number, how would I do that?
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?
So the answer is no.
V is basically like

string string string string ...
string string string string ...
...
...
...
string string string string ...
[/EDITED]


If you are thinking of something like this:
Items | ItemOne | ItemTwo | ItemThree ...
Names| (a row of names)...
No.s | (a row of numbers)...

Then I would say it is not a good way in which vectors/arrays are used.

A vector of objects would be a better choice.


and guys...please use [code][/code]...
Last edited on
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 [] ?
Well, you just did it. Put your codes between the [code] and the [/code].
thanks mate
Topic archived. No new replies allowed.