Write a program that uses a do-while loop to add integers by the user. In the loop condition, use a variable of type char, in which you can store the user’s answer to the question, “Do you want to enter another number?” when the loop is terminated, the program should output the total of all the inputs. To extend the program, add a nested while loop to ensure the user answers the “…another?” question sensible (with a ‘y’ or ‘n’).
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
char answer = ' ';
do {
cout<<("Enter number: ");
int number;
cin>>number;
sum += number;
answer = ' ';
while (answer != 'y' && answer != 'n') {
cout<<("Do you want to enter another number?: ");
cin>>answer ;
}
} while (answer != 'n');
cout<<"The total of all the inputs: " << sum<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment