I need help with vectors, iterators, and strings

Started by
2 comments, last by Jemburula 18 years, 7 months ago
So I finally started my journey to learning C++ and in the book I'm reading there was an exercise to create a program that stores your very own game list. The user can add and remove the games from the list. So here's my source:
// Game List Program
// A program that adds and removes game titles from a list

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

using namespace std;

int main()
{
    vector<string> GameList;
    vector<string>::iterator MyIterator;
    vector<string>::const_iterator iter;
    string game;
    
    cout << "\nWelcome to Game List!\n\n";
    cout << "This program will allow you to add and remove game titles from you're very own Game List.\n";
    
    while(true)
    {
         int choice = 4;
         
         while(choice < 1 || choice > 3)
         {
             cout << "\nPlease select a choice.\n";
             cout << "1.  Add a game to your list.\n";
             cout << "2.  Remove a game from your list.\n";
             cout << "3.  Quit the program and exit.\n";
             cin >> choice;
         }
         
         switch (choice)
         {
                case 1:
                     {
                           cout << "\nPlease enter the game title you wish to add to your list:\n";
                           cin >> game;
                           GameList.push_back(game);
                           cout << "\nYour game list:\n";
                           for (iter = GameList.begin(); iter != GameList.end(); ++iter)
                               cout << *iter << endl;  
                           break;
                     }
                case 2:
                     {
                           int number = 1;
                           cout << "\nPlease enter the number of the game you wish to remove.\n";
                           for (iter = GameList.begin(); iter != GameList.end(); ++iter)
                           {
                                cout << number << " " << *iter << endl;
                                number++;
                           }
                           int remove;
                           cin >> remove;
                           GameList.erase(GameList.begin() + remove - 1);
                           for (iter = GameList.begin(); iter != GameList.end(); ++iter)
                               cout << *iter << endl;
                           break;
                     }
                case 3:
                     break;
                default:
                     cout << "\nSomething is wrong.\n";
                     break;
         }
         
         if (choice == 3)
            break;
    }
    
    cin.get();
    cout << "Press the enter key to exit.";
    cin.get();
    
    return 0;
}

Well my problem is everything seems to work ok until when adding a game to the list, if I was to enter a space in between characters the program would go haywire. For instance try adding "Final Fantasy" to your game list. I was wondering how do I get around this and what is causing this problem. Any help will be appreciated.
Advertisement
cin>>game will only read up till the first whitespace. To read the entire line, use getline(cin, game);

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Hey thanks that works. I guess I might have gotten a little bit ahead of myself.
But if you didn't, you wouldn't of learnt that ;)
What we do in life... Echoes in eternity

This topic is closed to new replies.

Advertisement