tags:

views:

277

answers:

4

How do you declare a vector in c++ while allowing user input to define the vector's name? Okay, after reviewing your responses, here is more detail; Here is the error message from VS08 C++ console application -

Error 2 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::istream' to 'char *' e:\c++\project 5\project 5\project 5.cpp 58 project 5

Here is the code:

void addRecord()
{
     vector<char>recordName;
     vector<inventory>recordNameUser;
     cout << "Name the record you want to store it as\n";
     cin.get(cin, recordName);
     cout << "Enter the item description\n";
     cin.get(cin, recordNameUser.itemDescription);
     cout << "Enter the quanity on hand\n";
     cin >> recordNameUser.quanityOnHand;
     cout << "Enter the wholesale cost\n";
     cin >> recordNameUser.wholesaleCost;
     cout << "Enter the retail cost\n";
     cin >> recordNameUser.retailCost;
     cout << "Enter the date of the inventory (mm/dd/yyyy)\n";
     cin >> recordNameUser.inventoryDate;
}
+2  A: 

You mean you want the variable's name to be read from the user? You can't do that, and there isn't really a reason to; variable names only exist for the programmer's convenience; they don't even exist in the executable unless necessary

Michael Mrozek
+5  A: 

What are you really trying to do?

Normally, users don't care about the variable names. What you can do is store different vectors with different user defined keys:

map<string, vector<int> > user_vectors;
while (true) {
  string key = GetNameFromUserInput();
  int value = GetValueFromUserInput();
  user_vectors[key].push_back(value);
}

From your edited problem description, you really don't have a need for vectors at all.

map<string, inventory> inventory_map;
while (!done) {
  string item_name;
  cin >> item_name;
  inventory item;
  cin >> item.itemDescription;
  cin >> item.quantityOnHand;
  ...;
  inventory_map[item_name] = item;
}
for (map<string, inventory>::const_iterator it = inventory_map.begin();
     it != inventory_map.end(); ++it) {
   cout << "Inventory contains: " << it->first
        << " described as: " << it->second.description;
}
Stephen
+3  A: 

The names you give to variables while you code, doesnt exist anymore when you compile the code. Maybe you can use a map, and search the vector for its user given name.

This is a PSEUDO-CODE:

map<string, vector*> myMap;
string name;
vector a;
cin >> name;
myMap.insert(name, &a);

later when you want to retrieve the userdefined variable use this:

string name;
cin >> name;
vector v = *myMap[name];

you may want to do some validations before each step, like being sure the name provided by the person belongs to the map


noinflection
Blech, talk about an easy way to leak memory. I would recommend not using new'd pointers as the value type in a map.
Seth Johnson
Leak memory? Possibly worse than that. As shown, the vector `a` is on the stack, not new'ed. Taking its address, and storing it in a `map` for use later on will break once `a` 's scope is left.
MSalters
"pseudo-code" doesn't mean "C++ code that I didn't take time to think all the way through"
Tyler McHenry
+3  A: 

You want to have users give you a name and to be able to associate that with a vector of things? That's what a std::map is for, with a std::string as the key type and a std::vector as the payload type.

Donal Fellows