I'm suppose to 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. Any number that is not between 1 and 11 is suppose to print "Out of range; rejected." However, whenever I run the input:
12
-1
2
3
4
5
6
7
The output is:
Please enter a number between 1 and 11: 12
Out of range; rejected.
Please enter a number between 1 and 11: -1
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
Can anyone help please? I know it's something minor I'm missing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
using namespace std;
int main() {
int userNum = 0;
int numTotal = 0;
while (numTotal <= 21) {
cout << "Please enter a number between 1 and 11: ";
cin >> userNum;
cout << userNum << endl;
if ((userNum > 11) || (userNum < 1)) {
cout << "Out of range; rejected." << endl;
cout << "Please enter a number between 1 and 11: ";
cin >> userNum;
cout << userNum << endl;
}
else {
numTotal = userNum + numTotal;
}
}
cout << "Total: " << numTotal << endl;
return 0;
}
| |