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:
// Exercise 1, Chapter 4
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
usingnamespace 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;
}
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.
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;
}
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.
// Exercise 1, Chapter 4
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
usingnamespace 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.
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.