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

  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Numbers
AI Thread 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;
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top