質問

while(true)
{
    unsigned int option;
    cout<<"1 - Display the list\n";
    cout<<"2 - Add a game title to the list\n";
    cout<<"3 - Remove a game title from the list\n";
    cout<<"4 - Exit\n";
    cin>>option;

    if(option == 1)
    {
        if(gameTitles.empty())
        {
            cout<<"\nThere are no games to be displayed. Please try again after adding some games to the list.\n";
        }
        else
        {
            for(iter = gameTitles.begin(); iter != gameTitles.end(); ++iter)
            {
                cout<<*iter<<endl;
            }
        }
    }
    else if(option == 2)
    {
        cout<<"\nEnter the game's title:\n";
        cin>>newGame;
        gameTitles.push_back("newGame");
    }
    else if(option == 3)
    {
        cout<<"\nEnter a game to be removed:\n";
        cin>>removeGame;
        theIterator = find(gameTitles.begin(),gameTitles.end(),removeGame);
        theIterator = gameTitles.erase(theIterator);
    }
    else if(option == 4)
    {
        break;
    }
    else
    {
        cout<<"\nThe option is illegal. Please try again.\n";
    }
}

When I choose any 1, 3, 4 or illegal option, the loop brings me to the top and I have possibility to choose again. The problem arises when I am trying to use 2nd option. I just enter in an infinite loop. But I want to enter a game title and after it is added to my vector (I declared it earlier) and then have possibility to choose an option again.

役に立ちましたか?

解決

You don't show the type of newGame but I would guess that it is of type std::string and you enter a title with two words: the stream reads the first word and stops reading. The next thing you do is read an int which fails and leaves the value of option unchanged. From then on the stream won't do anything and just keeps reading.

The key error is not checking that the read attempt was successful before using the result: you always need to verify that your input was successful after reading and before using the result. When an input operation fails the stream goes into failure mode, i.e., std::ios_base::failbit gets set, and it converts to false instead of true (well, actually it converts to a null pointer vs. a non-null pointer but that's a detail irrelevant to this discussion). Once in failure state the stream won't do anything until the stream state is clear()ed. You you probably also need to ignore() the offending characters.

That is, you certainly should use something like

if (std::cin >> option) {
    // do something with the option
}
else {
    std::cout << "expected an integer: ignoring the line\n";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

To read the title you should probably read an entire line. Since the formatted input for the option will leave the newline character in the stream, you will first need to skip that character, though. That is, the input for the new title would look something like this:

if (std::getline(std::cin >> std::ws, newGame)) {
    // ...
}

The std::ws manipulator skips all leading whitespace. That is probably OK for your needs. If the string being read can have leading whitespace characters something different will be needed.

他のヒント

Before you try to cin again to the same variable newgame, put in a cin.ignore(). If I recall correctly, the first time you cin to a string (I'm assuming newgame is a string), it leaves a trailing \n so it will just automatically enter through in later prompts.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top