Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++Ask the user for a number between 1 and 11; stop asking when the user enters

ID: 3865210 • Letter: C

Question

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: C++

Enter a number between 1 and 11 (0 to stop): 1

Enter a number between 1 and 11 (0 to stop): 12

Enter a number between 1 and 11 (0 to stop): -3

Enter a number between 1 and 11 (0 to stop): 11

Enter a number between 1 and 11 (0 to stop): 10

Your total was 6

Thanks for playing!

Expected Output

Enter a number between 1 and 11 (0 to stop): 1

Enter a number between 1 and 11 (0 to stop): 12

Please follow the directions!

Enter a number between 1 and 11 (0 to stop): -3

Please follow the directions!

Enter a number between 1 and 11 (0 to stop): 11

Enter a number between 1 and 11 (0 to stop): 10

Your total was 22

Thanks for playing!

Enter a number between 1 and 11 (0 to stop): 1

Enter a number between 1 and 11 (0 to stop): 12

Please follow the directions!

Enter a number between 1 and 11 (0 to stop): -3

Please follow the directions!

Enter a number between 1 and 11 (0 to stop): 11

Enter a number between 1 and 11 (0 to stop): 10

Your total was 22

Thanks for playing!

Explanation / Answer

#include <iostream>
#include <vector>

using namespace std;

int main()
{
int n;
int total = 0;
cout<<"Enter a number between 1 and 11 (0 to stop): ";
cin >> n;
while(n != 0){
if(n <= 0 || n > 11) {
cout<<"Please follow the directions!"<<endl;
}
else{
total = total + n;
if(total > 21){
break;
}
}
cout<<"Enter a number between 1 and 11 (0 to stop): ";
cin >> n;
  
}
cout<<"Your total was "<<total<<endl;
cout<<"Thanks for playing!"<<endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter a number between 1 and 11 (0 to stop): 1                                                                                                                                                                                                                         

Enter a number between 1 and 11 (0 to stop): 12                                                                                                                                                                                                                        

Please follow the directions!                                                                                                                                                                                                                                          

Enter a number between 1 and 11 (0 to stop): -3                                                                                                                                                                                                                        

Please follow the directions!                                                                                                                                                                                                                                          

Enter a number between 1 and 11 (0 to stop): 11                                                                                                                                                                                                                        

Enter a number between 1 and 11 (0 to stop): 10                                                                                                                                                                                                                        

Your total was 22                                                                                                                                                                                                                                                      

Thanks for playing!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote