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>
int main()
{
int number = 0;
char answer = 'y', answer1 = 'y';
int sum = 0;
do
{
std::cout<<"Enter a number: ";
std::cin>>number;
sum += number;
std::cout<<"Continue? ";
std::cin>>answer;
do
{
std::cout<<"Want smth else: ";
std::cin>>answer1;
}while(answer == 'y');
}while(answer == 'y');
std::cout<<"Sum is: "<<sum<<std::endl;
}
Comments
Leave a comment