Sign in to follow this  
wellswells

List of favourite games issues

Recommended Posts

Hi guys, I'm quite new to C++ and have been working through Michael Dawson's 'Beginning C++ Programming'. What I've been reading thus far seems to make sense, I've been working through the examples and I think he explains them in the book rather well. I'm struggling though with some of the exercise he sets at the end of each chapter and there are no answers in the book. One such case is with a program for keeping track of your favourite games. He asks you to "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." Below is what I have got so far. The problems I'm having with the program are: a) if I enter a letter in as a menu choice, the program instantly falls into an infinite loop! which I cannot understand. b) when entering a game into the list, I am unable to enter more then 1 word (ie Final Fantasy) or the program gets stuck in an infinite loop again. c) To remove programs from the list, I've resorted to erasing the relevant games by selecting their position in the list. I was wondering how I could erase the games by simply writing the name of the game? If anyone could help with any of the problems I'm having I would be greatly appreciated. Or if anyone can point out any bad methods I've used that would be great as well. This is my first attempt at using iterators and vectors in a program and am still getting my head around them. thanks in advance. ////////////////////////// // Favourite Games List // #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { vector<string> theList; // create a vector for the list of games. vector<string>::iterator myIterator; vector<string>::const_iterator iter; cout << "\n*** Welcome to your list of favourite games ***\n"; int choice; while (choice != 4) { cout << "\n\nPlease select an option from the menu below.\n\n"; cout << "\t(1) Add a game to the list.\n"; cout << "\t(2) Remove a game from the list.\n"; cout << "\t(3) List all of the games.\n"; cout << "\t(4) Exit the program.\n"; cout << "\n Choice: "; cin >> choice; switch (choice) { case 1: { string newGame; cout << "\nPlease type in the name of the game you would like to add: "; cin >> newGame; theList.push_back(newGame); cout << "\nThank you! Returning to the menu..."; break; } case 2: { string removeGame; int i; int gameNo; cout << "\nGames you can remove"; for (int i = 0; i < theList.size(); ++i) cout << "\n\t(" << i << ") " << theList[i] << "."; cout << "\n\nPlease enter the number of the game you would like to remove: "; cin >> i; cout << "\n...Removing " << theList[i] << " from the list..."; theList.erase(theList.begin() + i); break; } case 3: { int gameNo = 0; cout << "\nThe games in your list: "; for (iter = theList.begin(); iter != theList.end(); ++iter) cout << "\n\n\t(" << gameNo++ << ") " << *iter << "."; break; } case 4: cout << "\nThank you. Come again soon." << endl; break; default: { cout << "You made an illegal choice. Please try again.\n"; cout << "\n Choice: "; cin >> choice; } } } return 0; }

Share this post


Link to post
Share on other sites
Quote:
Original post by wellswells
a) if I enter a letter in as a menu choice, the program instantly falls into an infinite loop! which I cannot understand.



cout << "\n Choice: ";
cin >> choice;

while ( cin.fail() ) // If the user entered something other than an integer:
{
cin.clear(); // Clear cin's failure-state

string dummy; // Dummy string to eat the rest of the bad input
getline( cin, dummy, '\n' ); // Clear the input from cin

cout << "\nInvalid choice: \"" << dummy << "\"";
cout << "\nEnter choice: ";
cin >> choice; // Try again!
}

cin.ignore(); // Eat the new-line residue left from entering a number




In the case that something other than a number is read from the cin stream, the stream will set it self into fail mode and will ignore any new input until you've cleared its status and you've removed the bad input.


Quote:
Original post by wellswells
b) when entering a game into the list, I am unable to enter more then 1 word (ie Final Fantasy) or the program gets stuck in an infinite loop again.


Try: getline( cin, the_string, '\n' );
The reason why it would enter an infinite loop is because streams will treat "Final Fantasy" as "Final" "Fantasy", as in two entries; the first for your game name, the second for the menu choice.

Quote:

c) To remove programs from the list, I've resorted to erasing the relevant games by selecting their position in the list. I was wondering how I could erase the games by simply writing the name of the game?


The function find will do what you want:


cout << "\nEnter the name of the game you would like to remove: ";

cin.ignore();
getline( cin, removeGame, '\n' );

myIterator = find( theList.begin(), theList.end(), removeGame ); // Find the string, removeGame, from the beginning of the list to the end of the list
if ( myIterator == theList.end() ) // If it fails, myIterator will equal theList.end()
cout << "\nNot found!";
else
theList.erase( myIterator );






Also, you might want to initialize your choice variable before using it:


int choice = -1;

while (choice != 4)
{
// ...





[Edited by - _fastcall on January 20, 2009 11:06:54 PM]

Share this post


Link to post
Share on other sites
No problem!

There is one more gotcha, though: The newline remains in the input stream when reading a number, so you must eat the newline before using the stream again (otherwise cin will think that the newline is what the user inputted)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this