The increment int is reset at the end of the function

This is the feature in question. The specified variable count1. Before the return count1;function is mapped to reset count1 to 1 or 2. The result of the final line coutis n lines, where n = the number of attempts, including the correct answer. Each line displays a number, which is 1 more than the line below, to count1= either 1 or 2. I could not set the template to which it will be finally output.

The questions themselves are just placeholders.

What is happening on earth?

Note. I am a very new programmer, and I know that there are more efficient ways to do what I do that I did not recognize. I am open to suggestions, but my understanding of these suggestions will most likely be hampered by my unfamiliarity with C ++

int q1(int count1)                      //q1() is always fed a value of 1.
{
    using namespace std;
    if (count1 <= 3)                    //User gets 3 tries to answer for full credit.
    {                                   //count1 is returned in order to determine user score.

        cout << "What is 2+2 \n";       //problem appears to occur between here and the end of this if statement.
        double a1;
        cin >> a1;

        if (a1 == 4)
        {
            cout << "Yup. You know what 2+2 is. \n\n";
        }
        else
        {
            wrong();                    //wrong() is a single line void function using std::cout and nothing else.
            q1(++count1);
        }
    }
    else
    {
        cout << "You have used all three tries. Next question. \n\n";
        ++count1;                       //count1 is incremented for an if statement in int main()
    }       
    cout << count1 << "\n";             //This line is strictly for debugging
    return count1;
}

cout : 5
4
3
2
\n 5432

EDIT:

, - , , , .

q1(++count1) count1 = q1(++count1);

, , . ?

+4
1

count1 1 ( ). , 1, count1 .

, count1 1 ( ). , count1, , ( q1()) 2, count1. , .

:

, count1, . ( , )

, , while :

int q1(int count1)
{
    using namespace std;
    while (count1 <= 3) //Run as long as user has chances
    {
        cout << "What is 2+2 \n";
        double a1;
        cin >> a1;

        if (a1 == 4)
        {
            cout << "Yup. You know what 2+2 is. \n\n";

            //Using `break` you stop the running `while` so the next
            //step is for the function to return
            break;
        }
        else
        {
            wrong();

            //By incrementing `count1` the next time the `while` runs
            //if user ran out of tries it will not enter the loop, so
            //it will return `count1` which would most likely be 4
            count1++;
        }
    }

    //Here the function is about to return, so you check if user won or lost
    if (count1 == 4)
        cout << "You have used all three tries. Next question. \n\n"; 

    //Debug
    cout << count1 << "\n";

    //Return
    return count1;
}
+3

Source: https://habr.com/ru/post/1618664/


All Articles