Using C Ask the user for a number between 1 and 11 stop aski

Using C++:

Ask the user for a number between 1 and 11; stop asking when the user enters 0 OR when the total goes over 21. Display the total on the screen. For example:

*** DO NOT use DO loop

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.


#include <iostream>
#include <string>

using namespace std;

int main()
{
int num, sum = 0;
  
while(true) { // an infinite loop
  
cout << \"Enter a number between 1 and 11 (0 to stop): \" << endl;
cin >> num; //read the number from user
sum = sum + num; //add the num to sum
  
if(sum>21 || num==0){ //if the sum is greater than 21 or the user input number = 0, then display the total and exit the loop
cout << \"Your total was \" << sum << endl;
cout << \"Thanks for playing!\" << endl;
break; //exit the loop
}
  
}

}

------------------------------------------------------------

OUTPUT:

Enter a number between 1 and 11 (0 to stop):
4
Enter a number between 1 and 11 (0 to stop):
7
Enter a number between 1 and 11 (0 to stop):
8
Enter a number between 1 and 11 (0 to stop):
5
Your total was 24
Thanks for playing!

Using C++: Ask the user for a number between 1 and 11; stop asking when the user enters 0 OR when the total goes over 21. Display the total on the screen. For e

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site