Logical error no for

Asked

Viewed 49 times

3

Hi guys I’m trying to make a controlled loop for my program, so far it’s running normal only in the first round, in the second repeat it jumps the first pass of the go and starts in the second position if anyone 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;
}

Updating

Folks I will post this photo I hope you exemplify my question. Resolução do erro

Update with response (explanation of Gomiero in the response posted below)

Sharecode

  • 1

    Let me get this straight...at the first execution he spins the two is straight, only on Monday he jumps the first and executes only the second? If not, try to clarify.

  • Almost ... so the first is the first time he reads the 5 Guess right and prints straight, then calls the askplay_again... if I answer Yes he starts the do again but he already starts asking to read the second Guess... in case he jumps up to the intro_game and already asks the Guess 2

  • I hope it wasn’t too complex

  • 1

    If the console output is not too large, I think it will help clarify the problem. Add it to your question and show at what instant the program started 'wrong''...

  • 1

    In case the variable J is not passing the correct value.... that’s it?

  • I put a photo to improve the explanation @Dungacardoso

  • 1

    I understand now. how strange, it increases the variables.. but the variable i and J of the second is not incremented.

  • then I’ve read and read again and nothing I could think of to do otherwise because for this kind of thing it doesn’t need much but if anyone has any idea of what is happening, I don’t know a better place to ask

  • 1

    I do not work with this language...more have tried to take out that Count++, that if it is a variable I have not seen statement, or then try to reset the variables after it instead of before.

  • Then every time q enters the login function it sets j=0 and when it enters the for it arrow i=0 in both for

  • 1
Show 6 more comments

1 answer

2


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

cin >> resp;

Within the function Ask_PlayAgain().

In the reading of the standard input, a character is "left over" after reading and, therefore, in the second execution of the getline(cin, Guess); he already reads this character "the most".

If the problem is this, the solution is to clear the input right after reading the variable resp (according to this reply):

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

To the numeric_limits functioning, it is necessary to include: #include <limits>.

  • Noooossa I don’t doubt in fact until I clarified the ideas I’ll test there and I’ll be right back

  • 1

    iiiihuuuuu Thanks @Gomiero for the help.Even for the clarity of the explanation, I tried to use the stdio’s fflush(stdin) to find out if it was the error. h and nothing. Thank you again

Browser other questions tagged

You are not signed in. Login or sign up in order to post.