Explain each of the following loops. Correct any problems you detect.
(a)
do
int v1, v2;
cout << "Please enter two numbers to sum:" ;
if (cin >> v1 >> v2)
cout << "Sum is: " << v1 + v2 << endl;
while (cin);
(b)
do {
// ...
} while (int ival = get_response());
(c)
do {
int ival = get_response();
} while (ival);
(a) fails to place the do
statement in a block.
do {
int v1, v2;
cout << "Please enter tow numbers to sum:" ;
if (cin >> v1 >> v2)
cout << "Sum is: " << v1 + v2 << endl;
} while (cin);
(b) attempts to initialise a new variable in the while
statement which is not
seen in the first iteration of the do
loop.
int ival;
do {
// ...
} while (ival = get_response());
(c) fails to declare ival outside of the scope of the do
loop.
int ival;
do {
ival = get_response();
} while (ival);