samedi 1 juin 2019

How to re-initialize a vector in a while-loop?

I am doing some exercises in . I am trying to make a simple game which requires continuous input in a vector from the user.

I tried to reinitialize the vector. I used in the while(1) loop and tried to clear() it too.

vector<int> user;                 // initialize vector
while (1)
{                                 
    for (int guess; cin >> guess;)// looping
    {                             // user input
        user.push_back(guess);
    }
    if (user.size() != 4)         // check if user put exactly 4 numbers
    {         
        cerr << "Invalid input";
        return 1;
    }
    //...                        // doing some stuff with "int bulls"
    if (bulls == 4)
    {
        break;
    }
}    // now need to go back with emty vector, so that the user can input guesses again 

In my terminal, it is looping forever or it is stopping in a condition I put for invalid input.

Aucun commentaire:

Enregistrer un commentaire