do while statement

closed account (EwCjE3v7)
Exercise:
Explain each of the following loops. Correct any problems you detect.
(a)
1
2
3
4
5
6
do
        int v1, v2; 
        cout << "Please enter two numbers to sum:";
        if (cin >> v1 >> v2)
            cout << "Sum is: " << v1 + v2 << endl;
while (cin);


But I do now understand what he meant by the cin condition

1
2
3
4
5
6
do {
        int v1, v2; 
        cout << "Please enter two numbers to sum:";
        if (cin >> v1 >> v2)
            cout << "Sum is: " << v1 + v2 << endl;
} while (cin); // how would you fix the cin condition? 


Last edited on
how would you fix the cin condition?


Is there something wrong with it?
Take int v1 and v2 out of the if statement. it should look like this:
cout << "Please enter two numbers to sum:";
cin >> v1 >> v2;
cout << "Sum is: " << v1 + v2 << endl;

Also, how do you want to end the loop?
closed account (EwCjE3v7)
No this is not my code, its from my book, and would cin even be a condition? like its not getting input to anything. So?

I do not know how they want us to end the loop. Thats the problem

EDIT:

Maybe the cin was for to see if the user wanted another go.

Maybe something like this

1
2
3
4
5
6
7
8
string again;
    do {
        int v1, v2;
        cout << "Please enter two numbers to sum:";
        if (cin >> v1 >> v2)
            cout << "Sum is: " << v1 + v2 << endl;
        cin >> string;
    } while (!again.empty() && again[0] != 'n');
Last edited on
Topic archived. No new replies allowed.