I have a concept question. I am attempting to store a data item in an ADT Bag. If I have the user input the data item himself (using the cin operator)into(lets say type string) a variable.
I also have set up an interactive menu so that the user can continue to add more entries every time they select option 2.
If the new entry is stored in the same string variable every time, does this over ride the previous entry that was stored in the bag?
Also, every time the user selects option 2, and inputs a new Entry into cin, the user is forced to hit Enter twice before the menu pops up again.
Why does this happen?
I have no information on BagInterface or LinkedBag, so the code posted here does not compile for me. It makes it hard to test.
If the new entry is stored in the same string variable every time, does this over ride the previous entry that was stored in the bag?
Yes it does overwrite the previous entry unless you have stored it in an array or a vector.
I believe the answer to your last question has to do with the getline statement on line 29. Using the '\n' as the delimiter the getline is waiting for the second enter to be pressed. Because the first enter allows the input to be stored in the variable.
Hope that helps,
Andy
P.S. You are missing the closing } of the switch statement.
I am not sure if it a compiler issue then, but when I remove line 27, getline does not work correctly. I am using Visual Studio 2013.
Now I am having an issue with adding the entry into the bag after it has been added into the vector.
The vector is initialized to vector<string> bagItems;
The error I am getting is:
error C2664: 'bool BagInterface<std::string>::add(const ItemType &)' : cannot convert argument 1 from 'std::vector<ItemType,std::allocator<_Ty>>' to 'const std::string &'
1 2 3 4 5 6 7 8 9 10
cout << "Type a string to add to the Bag: ";
cin >> newEntry;
cin.ignore(0);
getline(cin, newEntry);
for ( i = 0; i < bagItems.size(); i++)
{
bagItems[i] = newEntry;
bagItems = bagPtr->toVector();
bagPtr->add(bagItems);
}