how to permanently store data in a vector?

Hi, first of all, i think this is a great forum for people who are just starting out, thanks to all the people who come here and give advice.

Yesterday i started learning vectors and now ive got a little problem. This is the excersise a tutorial wants me to do:

Write a program using vectors and iterators that allows a user to maintain a list of his or her favorite games. The program should allow the user to list all game titles, add a game title, and remove a game title.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
int main()
{
	Graphics('*', "MY GAME LIST");
	bool quit = false;
	while ( !quit )
	{
		
		
		int nInp;
		cout << endl << "1) list games 2) add game 3) delete game 4) quit : ";
		cin >> nInp;
		cout << endl;
		
		vector<string> games;
		games.push_back("World of Warcraft");
		vector<string>::const_iterator iter;

		switch( nInp )
		{
			

			case 1:
			{
				for(iter = games.begin(); iter != games.end(); iter++)
					cout << *iter << endl;
				break;
					
			}
			case 2:
			{
				string sInp = "";
				cout << "Enter the name of the game: ";
				cin >> sInp;
				cout << endl;
				games.push_back(sInp);
				break;
								
			}
			case 3:
			{
				
			break;	
			}
			case 4:
			{
				cout << "See you soon!" << endl;
				quit = true;
				break;
				
			}
			default:
			{
				cout << "Invalid input, please try again..." << endl;
				break;
			}
		}
	
	}
}

void Graphics(char x, string title)
{
	int stringSize = title.size();
	int evenUneven = stringSize % 2;
	int fill1 =  ((78 - stringSize) / 2);
	int fill2 = fill1;
	
	if (evenUneven != 0)
		fill2 += 1;

	for(int i = 0; i < 80; i++)
		cout << x;
	
	cout << x;
	
	for(int i = 0; i < fill1; i++)
		cout << " ";
	
	cout << title;
	
	for(int i = 0; i < fill2; i++)
		cout << " ";
	cout << x;
	
	for(int i = 0; i < 80; i++)
		cout << x;
	
	cout << endl;	
}


Allthough i can add a game to the vector and when i list the vector in the same "case scope"(case 2) the newly added entry shows up, but as soon as i get back to the menu and try to list the vector the input is gone. I think this has something to do with the scope of the switch statement but im not sure.

Thanks!
Yes, it is a scope issue.
You are declaring vector<string> games; within the while loop, so each time it goes round the loop it goes out of scope and is recreated...
Move the declaration next to the declaration of quit and it this will then work.
That doesnt work either, when i add something and try to relist it it doesnt show up, even when i move the declaration out of the while loop.
If I move lines 14 and 15 to after line 2 it works fine for me.

BTW, line 33 should read getline( cin, sInp ); and you should add a
cin.ignore( numeric_limits<streamsize>::max(), '\n' ); after line 11. You'll need to #include <limits> too.


Hmm... one more hint. You can get rid of the loops in Graphics() by using the appropriate string constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Graphics(char x, string title)
{
	int stringSize = title.size();
	int evenUneven = stringSize % 2;
	int fill1 =  ((78 - stringSize) / 2);
	int fill2 = fill1;
	
	if (evenUneven != 0)
		fill2 += 1;

	cout << string( 81,    x   )
	     << string( fill1, ' ' )
	     << title
	     << string( fill2, ' ' )
	     << string( 81,    x   )
	     << endl;	
}


Hope this helps.
Last edited on
Doh, i copied and pasted the vector out of the while loop but forgot to remove the original, thats why it didnt work. thanks!

I dont really understand why i should change line 33 and add cin.ignore( numeric_limits<streamsize>::max(), '\n' ); <-- what does that even do?

the program runs fine without the changes. Oh and thanks for Graphics() hint, the code looks a lot cleaner now.
Last edited on
Yes, it works fine as is, but when interfacing with the user you can always expect that the user expects to press ENTER after every input. Unless you can guarantee that you'll never mix >> with something that expects a newline (like getline()) then you are fine, but it is better to program it using the correct (not simple and wrong "school" or "textbook") constructs. Do it right to begin with and you'll never have to wonder why the input stream didn't behave the way you thought it would.

If you want to know what cin.ignore() does:
http://www.cplusplus.com/reference/iostream/istream/ignore.html

It gets rid of the newline and any other garbage the user entered from clogging up the input stream and leaves it in a state that you can verify as valid.

Hope this helps.
Topic archived. No new replies allowed.