Add Up Numbers: Get to 21 or Stop at 0

  • Context: MHB 
  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Numbers
Click For Summary
SUMMARY

The discussion focuses on a C++ program designed to prompt users for numbers between 1 and 11, stopping when the user inputs 0 or when the cumulative total exceeds 21. The initial code provided by the user contained several errors, including incorrect variable initialization and control flow issues. A corrected version of the code was shared, which properly initializes the total variable and implements a loop that effectively handles user input and termination conditions. The final output displays the total correctly upon exiting the loop.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with control flow statements (if, while)
  • Knowledge of variable initialization and data types
  • Basic input/output operations in C++
NEXT STEPS
  • Explore C++ error handling techniques to manage user input more effectively
  • Learn about C++ functions to modularize the code for better readability
  • Investigate the use of arrays or vectors for handling multiple inputs
  • Study C++ debugging tools to troubleshoot and optimize code
USEFUL FOR

C++ beginners, programming students, and developers looking to improve their input handling and control flow in console applications.

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;

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

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

total = total + num;
while ((num == 0) || (total > 21)){
count << "Your total was " << total << endl;
count << "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;
}
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
Replies
12
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · 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