Answer to Question #292176 in C++ for ali

Question #292176

 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’).


1
Expert's answer
2022-01-30T13:43:14-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog