Question

#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0;...


#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;
}

works fine for everything but numbers out of the range im getting a program timed out error

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The reason why you get out of the range if the input number is not in range between 1 - 11 then it print out message

Out of range; rejected.

What the program does is . ?

it takes number 1 - 11 and sum up the number , if the number is not in range prints Out of range; rejected.,

if the sum of number exceed 21 . breaks the while loop and prints out the sum

Sample output:

Please enter a number between 1 and 11: 26 26 out of range; rejected. Please enter a number between 1 and 11: 1 Please enter

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT