Thomas Robillard Thomas Robillard - 3 months ago 26
C++ Question

Accessing C++ Vector as number

So basically I am trying to have a list of games, and give the user the option to delete it. It looks something like this:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
//setup
vector<string> games;
games.push_back("Team Fortress 2");
games.push_back("Skyrim");
games.push_back("Portal");
vector<string>::iterator myIterator

//main
int delItem;
cin >> delItem; // Consumes a number to represent the game to be deleted

while (iter != games.end()) // Displays all the games
{
cout << j << ") " << *iter << endl;
iter++;
j++;
}

while ((delItem <= games.begin()) || (delItem > games.end())) // can only be 1, 2, or 3
{
cout << "\nThat input is incorrect. Please try again: ";
getline (cin, delItem);
}
myIterator = (games.begin() + (delItem - 1);
games.erase(myIterator); // Deletes the item
cout << "\nThe game has been deleted.";

return 0;
}


So when I display the list, it looks like this:


1) Team Fortress 2

2) Skyrim

3) Portal


A user can type the number preceding the game and that will be selected to delete it. What I am trying to do, is to prevent the user from enter a number higher or lower than those three numbers. I attempt this in the second while loop, but it looks like games.begin() is not a number. I know that this is probably a dumb mistake that I just barely missed, but any help would be great.

Answer

There are several problems with your code.

You are reading the user's input before you have even printed out the available options for the user to choose from. You need to reverse those operations.

You are using variables that have not been declared.

When the user enters an invalid number, you are calling std::getline() to get a new number, but std::getline() outputs to a std::string, not to an int.

Try something more like this instead:

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

using namespace std;

int main()
{
    //setup
    vector<string> games;
    games.push_back("Team Fortress 2");
    games.push_back("Skyrim");
    games.push_back("Portal");
    vector<string>::iterator myIterator;

    //main

    int j = 1;
    for (myIterator = games.begin(); myIterator != games.end(); ++myIterator) // Displays all the games
    {
        cout << j << ") " << *myIterator << endl;
        ++j;
    }

    cout << "\nPick an item to delete: ";

    int delItem;
    do
    {
        if (cin >> delItem) // Consumes a number to represent the game to be deleted
        {
            if ((delItem >= 1) && (delItem <= games.size())) // can only be 1, 2, or 3
                break;
        }

        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin.clear();

        cout << "\nThat input is incorrect. Please try again: ";
    }
    while (true);

    /*
    alternatively:

    int delItem;
    do
    {
        string line;
        if (!getline(cin, line)) {
            // error!
            return 0;
        }

        istringstream iss(line);
        if (iss >> delItem) // Consumes a number to represent the game to be deleted
        {
            if ((delItem >= 1) && (delItem <= games.size())) // can only be 1, 2, or 3
                break;
        }

        cout << "\nThat input is incorrect. Please try again: ";
    }
    while (true);
    */

    myIterator = games.begin() + (delItem - 1);
    games.erase(myIterator); // Deletes the item
    cout << "\nThe game has been deleted.";

    return 0;
}