Write a program that continues to ask the user for a number

Write a program that continues to ask the user for a number between one and eleven (inclusive) until the total of the numbers is greater than 21. Be sure to reject any number that is not between 1 and 11 (inclusive).

So far I have:

#include using namespace std;

int main()

{

int number;

int sum = 0;

while(true)

{

cout << \"Please enter a number between 1 and 11: \";

cin >> number;

if (number >= 1 && number <= 11)

{ cout << number << endl; }

else { cout << number << endl; cout << \"Out of range; rejected.\" << endl; }

sum = sum + number;

if(sum > 21) break;

}

cout << \"The total is \" << sum << endl; return 0; }

However the last set of numbers should equal to

Please enter a number between 1 and 11: 12

Out of range; rejected.

Please enter a number between 1 and 11: -1

Out of range; rejected.

Please enter a number between 1 and 11: 2

Please enter a number between 1 and 11: 3

Please enter a number between 1 and 11: 4

Please enter a number between 1 and 11: 5

Please enter a number between 1 and 11: 6

Please enter a number between 1 and 11: 7

The total is 27

And my code does not take into consideration to disregard the first 2 sets of numbers as they are rejected, how to fix this problem??

My output is..

Please enter a number between 1 and 11: 12

Out of range; rejected.

Please enter a number between 1 and 11: -1

Out of range; rejected.

Please enter a number between 1 and 11: 2

Please enter a number between 1 and 11: 3

Please enter a number between 1 and 11: 4

Please enter a number between 1 and 11: 5

The total is 25

Please enter a number between 1 and 11: 12

Out of range; rejected.

Please enter a number between 1 and 11: -1

Out of range; rejected.

Please enter a number between 1 and 11: 2

Please enter a number between 1 and 11: 3

Please enter a number between 1 and 11: 4

Please enter a number between 1 and 11: 5

The total is 25

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 number;
int sum = 0;
  
while(true) {
cout << \"Please enter a number between 1 and 11: \";
cin >> number;
if (number >= 1 && number <= 11) {
cout << number << endl;
sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case
} else {
cout << number << endl; cout << \"Out of range; rejected.\" << endl;
}
  
if(sum > 21)
break;
}
  
cout << \"The total is \" << sum << endl;
return 0;
}

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

OUTPUT:

lease enter a number between 1 and 11: 6
6
Please enter a number between 1 and 11: -15
-15
Out of range; rejected.
Please enter a number between 1 and 11: 12
12
Out of range; rejected.
Please enter a number between 1 and 11: 7
7
Please enter a number between 1 and 11: 7
7
Please enter a number between 1 and 11: 1
1
Please enter a number between 1 and 11: 1
1
The total is 22

Write a program that continues to ask the user for a number between one and eleven (inclusive) until the total of the numbers is greater than 21. Be sure to rej
Write a program that continues to ask the user for a number between one and eleven (inclusive) until the total of the numbers is greater than 21. Be sure to rej
Write a program that continues to ask the user for a number between one and eleven (inclusive) until the total of the numbers is greater than 21. Be sure to rej

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site