dimanche 20 janvier 2019

Do while loop will not trigger with the if statements I have in it

This is just one of the if statements in the loop but for space i just pasted this one, no matter if you respond y or n or change the while fact to 1 or 2 it will not restart the loop it will just end.

I have tried changing the while variable to 1 then tried y and n on it and the same result just ends. I have tried just making a do while loop on its own to make sure i am doing it and and same with the if statement and they both work alone but together they do no.

do
    {
        //first user input
        cout << "Input first number: ";
        cin >> userinput1;
        //user method input
        cout << "Input method to be applied ex.(/ , * , - , +): ";
        cin >> usermethod;
        //second user input
        cout << "Input second number: ";
        cin >> userinput2;
        //operation for the answer
        //plus
        if(usermethod == plus)
        {
            answer = userinput1 + userinput2; cout << "Answer:" << answer;
            cout << endl << "continue [y/n]:";
            cin >> res;
            if(res == y){cont = 1;}else if(res == n){cont = 2;}
            else{cout << "error" << endl;}
        }
    }
    while (cont = 2);

I expect it the prompt for [y/n] and then if you hit n it stops the loop and if you hit y it goes through the whole thing again allowing you to do one more calculation.

Aucun commentaire:

Enregistrer un commentaire