Program crashes!

Hello fellow programmers! I am currently working on Michael Dawson's book: C++ Game Programming - Beginning. I have an exercise that says: "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." Well i wrote the program, but when i enter the name of the first game, after i press enter, it crashes. Any ideas?? Here is the code:
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
// Exercise 1, Chapter 4

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

using namespace std;

int main()
{
    // Intro & Info.
    cout<<"This program allows you to maintain a list of your favourite games.\n";
    cout<<"Enter your favourite game and press enter to store it's name.\n";
    cout<<"Press '0' to begin creating your list\n";
    cout<<"Enter 'mylist' to show your current list.\n";
    cout<<"Press 'q' to exit.\n";
    cout<<"Press '1' to sort your list.\n";
    cout<<"Press '2' to delete the last name entered.\n\n";
    
    // Setting strings & vectors
    vector<string> games; // The list of the games
    vector<string>::const_iterator iter;
    string option;
    
    
    // Setting the loop
    while (option != "q")
    {
          cin>>option;
          if (option == "0")
          {
          for (int i=0; ;++i)
          {
              cout<<"Enter the name of the game: ";
              cin>>games[i];
              if (games[i] == "q")
                 break;
              if (games[i] == "mylist")
              {
                         for (iter = games.begin(); iter != games.end(); ++iter)
                             cout<<*iter<<endl;
              }
              
              if (games[i] == "1")
              {
                           sort(games.begin(), games.end());
                           cout<<"Sorting done.\n";
              }
              
              if (games[i] == "2")
              {
                           games.pop_back();
                           cout<<"Last entry erased.\n";
              }
          }
          }
          else
             cout<<"Wrong choice. Write again.\n";
    }
    return 0;
}
closed account (D80DSL3A)
The vector games is empty. There are no valid elements so the program crashes at line 36 when you try to access one.
Use the push_back() function to add elements.

Instead of this:
1
2
cout<<"Enter the name of the game: ";
cin>>games[i];

Try:
1
2
3
4
cout<<"Enter the name of the game: ";
string temp;
cin>>temp;
games.push_back(temp);

I think it's a bit odd that you're storing the users action choice ("q", "mylist", "1", "2") in the list of games. I'm not sure your code will do what you want.
Line 20: When you start the program, the vector is empty. It contains no strings.

Line 36: You're trying to write into games[0] the first time through the loop, but games[0] doesn't exist, hence the crash.

You need to push a string onto the vector before you can reference it.
1
2
3
4
5
 
  string temp;
  cin >> temp;
  games.push_back (temp);
  // Now you can reference games[0] 

ok i will try it, but @fun2code, what do you suggest about the user action choices then?
A best practice of programming is to avoid using the same variable for two urelated purposes. In this case, you trying to use the same variable to determine what to do and for the name of a game. Those are unrelated.

It's better to have two different variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  char action;
  string title; 
  
  cout << "What do you want to do (1=Enter title, 2=..., Q=Quit)?";
  cin >> action;
  switch (action)
  { case '1':  cout << "Enter title: "; 
                    cin >> title;
                    games.push_back (title);
                   break;
     case '2':  // do something else
 ...
     case 'q': return 0;
   }

alright, thanks, i will try it that way and tell you if it woreked or not :)
i wrote it all again and it didn't crash, but when i enter the name of the game, the program gets the name and then closes. I wrote it with the switch method.
Would have to see your code to make any suggestions.
Here it is:
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
// Exercise 1, Chapter 4

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

using namespace std;

int main()
{

    cout<<"\tThis program allows you to maintain a list of your favourite games.\n";
    cout<<"\t\t\t\tMade by Franz.\n\n";

    vector<string> games; // The list of the games
    vector<string>::const_iterator iter;
    string option;
    
    char action;
    string title;
    
    cout<<"What do you want to do (1=Enter title, 2=show the list,\n";
    cout<<"3=sort your list, 4=delete the last name you entered, q=quit)?\n\n";
    cin>>action;
    switch (action)
    { case '1': cout<<"Enter title: ";
                cin>>title;
                games.push_back (title);
                break;
      case '2': for (iter=games.begin(); iter != games.end(); ++iter)
                    cout<<*iter<<endl;
                    break;
      case '3': sort (games.begin(), games.end());
                cout<<"Sorting done.\n";
                break;
      case '4': games.pop_back();
                cout<<"Last entry erased.\n";
                break;
      case 'q': return 0;
                break;
      }
}
i wrote it all again and it didn't crash, but when i enter the name of the game, the program gets the name and then closes.

Well, yeah. There's nothing in your code to make it do anything other than process one user action and then exit. You'll need to add a loop to make it repeat those parts of the code that you want to repeat.
@MikeyBoy like a while() loop??
That would probably be the most suitable one. Work out which bits of your code you want to repeat, and put those bits inside the loop. Also, work out under what condition you want your loop to continue looping, and when you want it to quit the loop and move on.
Okay, thanks.
Topic archived. No new replies allowed.