No logical error

3
Hi everyone, I'm trying to do a controlled loop for my program, so far it's normal, only in the first round, in the second repetition it jumps the first pass of the for and starts in the second position if someone wants to see my code :

void Intro_Game();
void Login_Entrace();
bool Ask_PlayAgain();
string User_Login(string);
constexpr int WORLD_LENGHT = 5;

int main()
{
    bool PlayAgain_Count = false;

    do
    {
        system("cls");
        Intro_Game();
        Login_Entrace();
        PlayAgain_Count = Ask_PlayAgain();
    } while (PlayAgain_Count);

    return 0;
}

void Intro_Game()//Intro Game
{
    cout << "Welcome to Bulls & Cow\n";
    cout << "Can you guess the " << WORLD_LENGHT << " isogram I'm think of?\n\n";
}
//entrace of data process fase
void Login_Entrace()
{
    string x[5];
    int j = 1;

    //begin fase
    for (int i = 0; i < WORLD_LENGHT; i++)
    {
        cout << "Enter with Guess " << j << ": ";
        x[i] = User_Login(x[i]);
        system("cls");
        j++;
    }

    j = 0;

    for (int i = 0; i < WORLD_LENGHT; i++)
    {
        ++j;

        cout << "Guess " << j << "  name is: " << x[i] << "\n\n";
    }//end fase
}

bool Ask_PlayAgain()
{
    string resp;

    cout << "Do wanna play again?\n";
    cin >> resp;

    if (resp[0] == 'Y' || resp[0] == 'y')
        return true;

    if (resp[0] == 'N' || resp[0] == 'n')
    {
        system("cls");
        cout << "You say no. =(\n";
        return false;
    }

    if(resp[0] != 'N' || resp[0] != 'n' && resp[0] != 'Y' || resp[0] != 'y')
    {
        system("cls");
        cout << "Wrong awnser please try again\n";
        return Ask_PlayAgain();
    }
}

//Read user login name
string User_Login(string Guess)
{
    getline(cin, Guess);

    return Guess;
}
  

Update

I'm going to post this photo, hope you can illustrate my question.

  

Updatewithresponse(explanationofGomieroinresponsepostedbelow)

ShareCode

    
asked by anonymous 13.02.2016 / 19:54

1 answer

2

I'm not sure I understand the problem correctly, but the error is probably on the line:

cin >> resp;

Within the function Ask_PlayAgain() .

In reading the standard entry, a character is "left" after the reading, so in the second run of getline(cin, Guess); it already reads that "more."

If the problem is this, the solution is to clear the entry just after reading the variable resp (as this #

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

For numeric_limits to work, you must include: #include <limits> .

    
13.02.2016 / 22:41