loop of some type

ok so basically I need some help with my schooling.

I am creating a simple math equation.

however I need a loop or something bc i do believe there is a faster way than if else repeatedly.

so the gist of the program is that the user only get 3 guesses at the problem if its wrong cout incorrect if correct return to previous menu to start over.

i would put a for loop but i dont understand exactly how i would stop the loop say if the person got the math right on the second chance

while (ans1 != quest1)
{

cout << "Incorrect, Please try again" << endl;
cin >> ans1;

}
if (ans1 == quest1)
{

cout << "Correct" << endl;
cout << "" << endl;
}

hope fully this makes since. if anyone needs more of an explanation or i am simply overcomplicating this please let me know.
Hello fiber1kenobi,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



It is usually best to post complete code that can be compiled and tested. Also some one else may see something that you have not.

Here is an idea that may work, but without the rest of the code I have no way to test it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int tries{};

while (tries < 3)
{
    if (ans1 != quest1)
    {
        cout << "\n     Incorrect, Please try again\n";
        cin >> ans1;

        tries++;
    }

    if (ans1 == quest1)
    {
        cout << "\n   Correct\n\n";

        break;  // <--- Or set "tries" to 3.
    }
}


Andy
Andy,

My apologies I only posted the the section to see if what I was doing was incorrect and be it that it is a simple code, it is also very long. First post and still learning the ways.

I can post the entire code from Dev if you would like to see it all. Let me know.

In the meantime I will try to incorporate what you added and see if it fixes my issue at hand.

Last edited on
*update*

Andy the code you wrote worked but did not work in my compiler. here is what it was,
extended initializer lists only available with -std=c++11 or -std=gnu++ 11.

I took out the carrets after tries and the error went away.

Now bc I am extremely new to this I dont know what it means but I can only assume that it is something with the IDE I am using.

Thank you for your help and showing me that loop instance.
Hello fiber1kenobi,

It also helps to mention what IDE and compiler you are using when making the post. If you know the version numbers that can help too.

The error message means that what you have is using a C++ standard before the 2011 standards.

You may be able to adjust the IDE to tell the compiler to use the C++11 standards, which is a good idea or consider finding an update to your IDE or try a different IDE that uses the C++11 standards. The C++14 standards would be better and C++17 is what is considered current.

You can always replace int tries{}; with int tries = 0;.

The {}s, or uniform initializer, became available from C++11 standards on.

As far as posting a large program I have seen people, including my-self, use 2 posts to cover everything. There is the option of https://pastebin.com/ or https://repl.it/ along with some others I believe.

I have seen a few that have used their Google account to store the code and then provide a link to it.

The whole code is not always necessary just enough that will compile and demonstrate the problem.

Andy
Here's a variation of Andy's code. I prefer this structure because the loop is only about the guessing. Once guessing is done, the code after the loop decides what to print. Also, I like to use for loops whenever possible because they separate the looping construct from the body of the loop.

This is all personal preference. Andy's code is just fine. I just wanted to show an alternative.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    // Loop until they get the answer of make 3 incorrect guesses
    for (int tries = 0; tries < 3; ++tries) {
        cout << "Guess the number: ";
        cin >> guess;

        if (guess == answer) break;

        cout << "Incorrect.\n";
    }

    // When you get here, you either exhausted the guesses or got the answer.
    if (guess == answer) {
        cout << "Correct!  Congratulations.\n";
    } else {
        cout << "Too many incorrect guesses. Sorry\n";
    }

Topic archived. No new replies allowed.