Add Up Numbers: Get to 21 or Stop at 0

  • MHB
  • Thread starter MMOne
  • Start date
  • Tags
    Numbers
In summary, the program prompts the user to enter a number between 1 and 11, stopping when the user enters 0 or when the total goes over 21. It then displays the total on the screen and ends the program.
  • #1
MMOne
3
0
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:

#include<iostream>
#include<cmath>
using namespace std;
int main(){

int num, total;

cout << "Enter a number between 1 and 11 (0 to stop): ";
cin >> num;
cout << num << endl;

while((num > 0)&&(num < 12)){
cout << num << endl;

total = total + num;
while ((num == 0) || (total > 21)){
cout << "Your total was " << total << endl;
cout << "Thanks for playing!" << endl;
}
}
return 0;
}

When I compile it will not provide the messages at the end. Not sure how to do this my code might just be sloppy. Any help would be appreciated.
 
Technology news on Phys.org
  • #2
This is how I'd code it. I didn't compile and run it, though.

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int num;
    int total = 0;
    int quit = 0;

    while (!quit) {
        cout << "Enter a number between 1 and 11 or 0 to quit." << endl;
        cin >> num;

        if (num == 0)
          quit = 1;
        else if (num < 1 || num > 11)
                cout << "Invalid number." << endl;
        else {
             total = total + num;
             if (total > 21)
               quit = 1;
        }
    }

    cout << "Your total was " << total << endl;
}
 

1. What is "Add Up Numbers: Get to 21 or Stop at 0"?

"Add Up Numbers: Get to 21 or Stop at 0" is a mathematical game where players try to reach a total of 21 by adding up numbers, but must stop at 0 to avoid going over 21.

2. How do you play "Add Up Numbers: Get to 21 or Stop at 0"?

To play "Add Up Numbers: Get to 21 or Stop at 0", players take turns choosing a number from 1-10 and adding it to the running total. The goal is to reach a total of 21 without going over. If a player reaches 0, they must stop and the next player takes their turn. The first player to reach 21 wins.

3. What is the strategy for winning at "Add Up Numbers: Get to 21 or Stop at 0"?

The strategy for winning at "Add Up Numbers: Get to 21 or Stop at 0" is to carefully choose numbers that will either lead to reaching 21 or stopping at 0. It is also important to pay attention to the numbers chosen by the other players and adjust your strategy accordingly.

4. Can the game "Add Up Numbers: Get to 21 or Stop at 0" be played with more than two players?

Yes, "Add Up Numbers: Get to 21 or Stop at 0" can be played with more than two players. However, the game may take longer with more players and the strategy may need to be adjusted accordingly.

5. How does "Add Up Numbers: Get to 21 or Stop at 0" relate to real-life applications?

"Add Up Numbers: Get to 21 or Stop at 0" can help improve mathematical skills such as addition, subtraction, and strategic thinking. It can also be used as a fun way to practice mental math and problem-solving. Additionally, the concept of stopping at a certain point can be applied to decision-making in real-life situations.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
9
Views
704
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
Replies
10
Views
960
  • Programming and Computer Science
Replies
3
Views
732
  • Programming and Computer Science
Replies
6
Views
8K
Back
Top