MHB Add Up Numbers: Get to 21 or Stop at 0

  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Numbers
Click For Summary
The discussion revolves around a C++ program that prompts users to enter a number between 1 and 11, stopping when the user inputs 0 or when the total exceeds 21. The initial code provided has issues, particularly with the logic for displaying messages at the end. The user seeks assistance to correct these problems. A revised version of the code is presented, which initializes the total correctly and uses a loop to handle user input effectively. In this version, the program checks for valid input, updates the total, and terminates when the conditions are met, displaying the final total to the user. Key improvements include proper initialization of variables and clear exit conditions for the loop.
MMOne
Messages
3
Reaction score
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
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;
}
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 22 ·
Replies
22
Views
3K
Replies
12
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
2K
Replies
10
Views
2K
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K