0

I'm having a problem with my C++ code and I haven't really found anything online that describes why i'm having this problem. Here is my code:

/*
Write a program using vectors and iterators that allows a user to main-
tain 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.
*/

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<string> gamesList;
    gamesList.reserve(10);
    vector<string>::const_iterator iter;
    string menu = "1. List all games\n";
    menu += "2. Add a game title\n";
    menu += "3. Remove a game title\n";
    menu += "4. Quit\n";
    string newTitle = "", removeTitle = "";
    int choice = 0;

    while (choice != 4)
    {
        cout << menu;
        cout << "\nYour choice: ";
        cin >> choice;
        switch (choice)
        {
            case 1:
                for (iter = gamesList.begin(); iter != gamesList.end(); ++iter)
                {
                    cout << *iter << endl;
                }
                cout << "\nList capacity is " << gamesList.capacity() << endl;
                break;
            case 2:
                cout << "Please enter a game title :";
                cin >> newTitle;
                gamesList.push_back(newTitle);
                break;
            case 3:
                cout << "Which game title do you want to remove?\n";
                cin >> removeTitle;
                for (int i = 0; i < gamesList.size(); ++i)
                {
                    if (gamesList[i] == removeTitle)
                    {
                        gamesList.erase(gamesList.begin() + i);
                    }
                }
                break;
            case 4:
                cout << "Good bye!";
                break;
        }
    }
    return 0;
}

If I run the program and enter Pong, Breakout and Tetris to the list, it works fine. If I run the program and enter Half Life or any title longer than 8 characters, the program goes into an infinite loop. Any help would be greatly appreciated.

  • 1
    Are you sure it's the number of characters? Might the problem not be with names containing spaces? – François Andrieux Jun 02 '17 at 15:33
  • 2
    Your going to need [this](https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces) and because of that you will also need [this](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – NathanOliver Jun 02 '17 at 15:33
  • I understand now. Thank you both. Those links will be quite helpful. – user10001110101 Jun 02 '17 at 15:40

2 Answers2

2

The problem isn't the length, it's that you try to enter names with spaces in them. The input operator >> separates on space. So if you enter Half Life as a name, the input operator will only read Half.

You probably should use std::getline instead, to read the names.

As for the infinite loop, it's because since part of the name is still in the input buffer (with a leading space) then when you attempt to read a number for the menu entry, the input will fail, leave the input in the buffer, and you will not detect it and go into an infinite loop where you want to read an integer, fail and on and on and on...

Using std::getline will solve both problems. But if you want to make sure this won't happen again you have to add some error checking when reading the integer for the menu alternative. This can simply be something like

while (!(cin >> choice))
{
    // Input of menu alternative failed, ignore input until the end of the line
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

An std::istream::ignore reference.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
0

For starters you should include header <string> because there are used declarations from the header in the program.

#include <string>

This code snippet of removing an element from the vector

       case 3:
            cout << "Which game title do you want to remove?\n";
            cin >> removeTitle;
            for (int i = 0; i < gamesList.size(); ++i)
            {
                if (gamesList[i] == removeTitle)
                {
                    gamesList.erase(gamesList.begin() + i);
                }
            }

is wrong.

First of all according to the assignment you have to remove only one element. To remove an element you can use the just one statement without any loop

#include <algorithm>

//...

gamesList.erase( std::find( gamesList.begin(), gamesList.end(), removeTitle ) );

As for you problem then the oprator >> inputs characters until a white space character is encountered. Instead of the operator you should use function getline. Take into account that you will also need to use member function ignore to remove a new line character from the input bu=ffer.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268