Jump to content
  • Advertisement
Sign in to follow this  
eektor

I need help with vectors, iterators, and strings

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
cin>>game will only read up till the first whitespace. To read the entire line, use getline(cin, game);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!