//Favorite Games
//User can make a list of their favorite games
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int gameloop=1;
while (gameloop!=0)
{
vector<string> games;
vector<string>::iterator myiterator;
vector<int>::iterator iter;
cout<<"This program allows you to make a list of your favorite games.";
cout<<"\nIt's really exciting.\n\n";
cout<<"Your list currently has "<<games.size()<< " games in it.\n\n";
cout<<"Perhaps you'd like to add a game?\n";
cout<<"If you want to add a game, hit 1.\n";
cout<<"If you want to remove a game from your list, hit 2.\n";
cout<<"If you want to look at your list hit 3.\n";
cout<<"If you want to leave hit 4.\n";
int menuchoice;
cin>>menuchoice;
cin.ignore();
if (menuchoice==1)
{
cout<<"Please enter a game: ";
string gameadd;
cin>>gameadd;
games.push_back(gameadd);
cin.ignore();
}
if (menuchoice==2)
{
cout<<"\n\nPlease enter a game to be removed: ";
string gamename;
for(int i=0; i<games.size(); ++i)
cout<<games;
cin>>gamename;
cin.ignore();
if (games.empty()==true)
{
continue;
}
*myiterator=gamename;
games.erase(myiterator);
}
if (menuchoice==3)
{
cout<<"\n\nYour games are: \n\n";
for(int i=0; i<games.size(); ++i)
cout<<games;
}
if (menuchoice==4)
{
cout<<"\n\nThank you for using my program!";
cout<<"\nGoodbye.";
gameloop=0;
}
}
cin.ignore();
return 0;
}
Vector this and that
Okay yesterday night I hit a landmine with for loops and some people helped me out. Now I've found another landmine so i'm back again.
To summarize i've been reading C++ Game Programming by Michael Dawson and I sped through Ch.4 today and I'm working on the end of chapter excercises.
What I'm trying to do is get a menu where you can input 1,2,3,4 to (1)add a game name to your list, (2)remove a game name from said list, (3)look at the list, and (4)quit. And if only life was that simple. I have a problem that when you add a game it doesn't go into a vector like I want it to. And I have no idea why. [dead]
I'm not too sure that even if I could add a game, that option 2(delete game from list) would work either. But I can't test it anyways until I get the adding to the list down.
Someone help me out please.
I tried quite a bit of random things to get it working, so if you see something that shouldn't be there tell me. I tried to clean up the mess I made of it before posting it here.
Thanks guys
Well, the first thing I noticed is that your vector is created within the game loop, meaning that each time you check for inputs, the vector is created, modified, and deleted before the next round.
You probably want to declare the game vector outside of the while loop.
You probably want to declare the game vector outside of the while loop.
...int main(){ int gameloop=1; vector<string> games; while (gameloop!=0) {...
Thanks, can't believe I didn't think about that.
Ok now all that's left is when I try to remove a game from the list I get a program crash error.
Can you help me out with that?
Ok now all that's left is when I try to remove a game from the list I get a program crash error.
Can you help me out with that?
You never set myiterator to anything and then you call
*myiterator=gamename;
games.erase(myiterator);
"*myiterator = gamename" will set the string of where it is pointing to and make it gamename. Since myiterator was never set to something, doing *myiterator will cause your program to crash.
You should do something like
myiterator = find(games.begin(), games.end(), gamename);
if (myiterator != games.end())
games.erase(myiterator);
find() is declared in <algorithm>. Since you have that and namespace std already declared, you should be good to go.
You could also remove the if (games.empty()==true) continue; check as long as you have the if (myiterator != games.end()) check.
*myiterator=gamename;
games.erase(myiterator);
"*myiterator = gamename" will set the string of where it is pointing to and make it gamename. Since myiterator was never set to something, doing *myiterator will cause your program to crash.
You should do something like
myiterator = find(games.begin(), games.end(), gamename);
if (myiterator != games.end())
games.erase(myiterator);
find() is declared in <algorithm>. Since you have that and namespace std already declared, you should be good to go.
You could also remove the if (games.empty()==true) continue; check as long as you have the if (myiterator != games.end()) check.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement