Data Stored in ADT Bag

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?

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
int main()
{
	int choice;
	//string *array = new string [];
	string newEntry, oldEntry, currEntry;
	string temp;
	BagInterface<string>* bagPtr = nullptr;
	bagPtr = new LinkedBag<string>();
	do
	{

		cout << "1 - Show Size" << endl;
		cout << "2 - Add New Entry" << endl;
		cout << "3 - Remove Entry" << endl;
		cout << "4 - Search for an Entry" << endl;
		cout << "5 - Give the Count of a Particular Entry" << endl;
		cout << "6 - Print the Bag Contents" << endl;
		cout << "7 - Clear" << endl;
		cout << "0 - Quit" << endl;
		cout << "Make Selection from Menu: " << endl;
		cin >> choice;
		cin.ignore(1);
		switch (choice)
		{
		case 2:
			cout << "Type a string to add to Bag: ";
			cin >> newEntry;
			cin.ignore(1);
			getline(cin, newEntry, '\n');
			bagPtr->add(newEntry);
			break;
	
	} while (choice != 0);


}
Hello neverStopLearning,

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.
Last edited on
Hello neverStopLearning,

I managed to get the program to work and found the answer.

On line 27 you ask for input then on line 29 you ask for input again. Line 29 is the second enter you are encountering.

I commented out line 27 and tested the getline with and without the '\n' and it worked either way.

Hope that helps,

Andy
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);
	}
Last edited on
Topic archived. No new replies allowed.