Question

In: Computer Science

(C++) Why does my code not continue looping? it only loops twice. Can someone take a...

(C++) Why does my code not continue looping? it only loops twice. Can someone take a look at my code so far:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

void cls(void);

void cls(void) {
system("cls||clear");
return;
}

int main() {
int BankBalance = 0;
char quit;
int wager = 0;
int inputWager = 0;
int sum = 0;
int diceRoll = 0;
int rollPoint = 0;
int point = 0;
int dice1 = 0;
int dice2 = 0;
char playerResponse;

srand(time(0));

cout << "Hey there buddy. Welcome to the game of Craps!!! Glad you can make it :D\n\n";
cout << "Let's start with the boring rules:\n\n ";
cout << "A player rolls two dice. each die has six faces." << endl;
cout << "These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated." << endl;
cout << "If the sum is 7 or 11 on the first throw, the player wins. if the sum is 2, 3, or 12 on the first throw (called 'craps'), the player loses (i.e. the 'house' wins)." << endl;
cout << "If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's 'point'." << endl;
cout << "To win, you must continue rolling the dice until you 'make your point'. The player loses by rolling a 7 before making the point.\n\n";

cout << "Please enter your initial Bank Balance: $";
cin >> BankBalance;

while (cin.fail()) {
cin.clear();
cin.ignore(256, '\n');

cout << "Invalid input detected, please enter a valid amount: $";
cin >> BankBalance;
}

cout << "\nCurrent bank balance: $" << BankBalance << endl;
cout << "Total wager so far: $" << wager << endl;
cout << "Please enter your wager: $";
cin >> inputWager;

while (inputWager < 0 || inputWager > BankBalance) {
cout << "Not a valid wager!!" << endl;
cout << "Current bank balance: $" << BankBalance << endl;
cout << "Total wager so far: $" << wager << endl;
cout << "Please enter your wager: $";
cin >> inputWager;
}

wager = wager + inputWager;

//BankBalance = BankBalance - inputWager;

//sum = diceRoll;

if (sum == 7) {
cout << "\nYou lose the game!!!" << endl;

BankBalance = BankBalance - inputWager;

cout << "\nBank Balance: $" << BankBalance << endl;
cout << "Roll again? (y/n)";
cin >> playerResponse;
if (playerResponse == 'n' || playerResponse == 'N') {
cout << "PEACE!!" << endl;
return -1;
}
}
else if (sum == point) {
cout << "\nYou won the game!!" << endl;
BankBalance = BankBalance + 2*wager;
cout << "\nBank Balance: $" << BankBalance << endl;
}

dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
sum = dice1 + dice2;

cout << "Sum: " << sum << " (Die 1: " << dice1 << " Die 2: " << dice2 << ")" << endl;

cout << "\n\nCurrent bank balance: $" << BankBalance << endl;
cout << "Please enter your initial wager: $";
cin >> wager;

while (wager <= 0 || wager > BankBalance) {
cout << "This is not a valid wager!!" << endl;
cout << "current bank balance: $" << BankBalance << endl;
cout << "Please enter your initial wager: $";
cin >> wager;
}

//BankBalance = BankBalance - wager;
//sum = diceRoll;

if (sum == 7 || sum == 11) {
cout << "You won the game!" << endl;

BankBalance = BankBalance + 2*wager;

cout << "\nBank Balance: $" << BankBalance << endl;
cout << "\nDo you wish to continue? (y/n)";
cin >> playerResponse;
if (playerResponse == 'n' || playerResponse == 'N') {
cout << "See ya!!" << endl;
return -1;
}
}
else if (sum == 2 || sum == 3 || sum == 12) {
cout << "Craps!!!" << endl;

BankBalance = BankBalance - wager;

cout << "\nBank Balance: $" << BankBalance << endl;
cout << "\nCONTINUE? (y/n)";
cin >> playerResponse;
if (playerResponse == 'n' || playerResponse == 'N');
cout << "Peace!!" << endl;
return -1;
}
else {
cout << "\nYou need to roll to make your point (" << sum << ")..." << endl;
//system("pause");
//point = sum;
//rollPoint(point, wager);
}

return 0;
}

Solutions

Expert Solution

// USE GOTO STATEMENT TO ROLL AGAIN

// OR USE WHILE LOOP TO ASK WHETHER U WANT TO ROLL AGAIN OR NOT

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

void cls(void);

void cls(void)
{
    system("cls||clear");
    return;
}

int main()
{
    int BankBalance = 0;
    char quit;
    int wager = 0;
    int inputWager = 0;
    int sum = 0;
    int diceRoll = 0;
    int rollPoint = 0;
    int point = 0;
    int dice1 = 0;
    int dice2 = 0;
    char playerResponse;
  
    srand(time(0));
  
    cout << "Hey there buddy. Welcome to the game of Craps!!! Glad you can make it :D\n\n";
    cout << "Let's start with the boring rules:\n\n ";
    cout << "A player rolls two dice. each die has six faces." << endl;
    cout << "These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated." << endl;
    cout << "If the sum is 7 or 11 on the first throw, the player wins. if the sum is 2, 3, or 12 on the first throw (called 'craps'), the player loses (i.e. the 'house' wins)." << endl;
    cout << "If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's 'point'." << endl;
    cout << "To win, you must continue rolling the dice until you 'make your point'. The player loses by rolling a 7 before making the point.\n\n";
  
    cout << "Please enter your initial Bank Balance: $";
    cin >> BankBalance;
  
    while (cin.fail())
    {
        cin.clear();
        cin.ignore(256, '\n');
      
        cout << "Invalid input detected, please enter a valid amount: $";
        cin >> BankBalance;
    }
  
    cout << "\nCurrent bank balance: $" << BankBalance << endl;
    cout << "Total wager so far: $" << wager << endl;
    cout << "Please enter your wager: $";
    cin >> inputWager;
  
    while (inputWager < 0 || inputWager > BankBalance)
    {
        cout << "Not a valid wager!!" << endl;
        cout << "Current bank balance: $" << BankBalance << endl;
        cout << "Total wager so far: $" << wager << endl;
        cout << "Please enter your wager: $";
        cin >> inputWager;
    }
  
    wager = wager + inputWager;
  
    //BankBalance = BankBalance - inputWager;
  
    //sum = diceRoll;
    start:              // i used goto statement to loop again      

        if (sum == 7)
        {
            cout << "\nYou lose the game!!!" << endl;
          
            BankBalance = BankBalance - inputWager;
          
            cout << "\nBank Balance: $" << BankBalance << endl;
            cout << "Roll again? (y/n)";
            cin >> playerResponse;
            if (playerResponse == 'n' || playerResponse == 'N')
            {
                cout << "PEACE!!" << endl;
                return -1;
            }
        }
        else if (sum == point)
        {
            cout << "\nYou won the game!!" << endl;
            BankBalance = BankBalance + 2*wager;
            cout << "\nBank Balance: $" << BankBalance << endl;
        }
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;
        sum = dice1 + dice2;
      
        cout << "Sum: " << sum << " (Die 1: " << dice1 << " Die 2: " << dice2 << ")" << endl;
      
        cout << "\n\nCurrent bank balance: $" << BankBalance << endl;
        cout << "Please enter your initial wager: $";
        cin >> wager;
      
        while (wager <= 0 || wager > BankBalance)
        {
            cout << "This is not a valid wager!!" << endl;
            cout << "current bank balance: $" << BankBalance << endl;
            cout << "Please enter your initial wager: $";
            cin >> wager;
        }
      
        //BankBalance = BankBalance - wager;
        //sum = diceRoll;
      
        if (sum == 7 || sum == 11)
        {
            cout << "You won the game!" << endl;
          
            BankBalance = BankBalance + 2*wager;
          
            cout << "\nBank Balance: $" << BankBalance << endl;
            cout << "\nDo you wish to continue? (y/n)";
            cin >> playerResponse;
            if (playerResponse == 'n' || playerResponse == 'N')
            {
                cout << "See ya!!" << endl;
                return -1;
            }
            else
            {
                goto start;
            }
        }
        else if (sum == 2 || sum == 3 || sum == 12)
        {
            cout << "Craps!!!" << endl;
          
            BankBalance = BankBalance - wager;
          
            cout << "\nBank Balance: $" << BankBalance << endl;
            cout << "\nCONTINUE? (y/n)";
            cin >> playerResponse;
            if (playerResponse == 'n' || playerResponse == 'N') //here i had corrected
            {
                cout << "Peace!!" << endl;
                return -1;
            }
            else
            {
                goto start;
            }
        }
        else
        {
            cout << "\nYou need to roll to make your point (" << sum << ")..." << endl;
            //system("pause");
            //point = sum;
            //rollPoint(point, wager);
            goto start;
        }
  
    return 0;
}


Related Solutions

I have a problem with my code. It does not run. Please can someone check the...
I have a problem with my code. It does not run. Please can someone check the code and tell me where I went wrong? This is the question: Program Specification: Write a C program that takes the length and width of a rectangular yard, and the length and width of a rectangular house (that must be completely contained in the yard specified) as input values. Assuming that the yard has grass growing every where that the house is not covering,...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
Can someone tell me how to fix warning msg in my code of C ++? I...
Can someone tell me how to fix warning msg in my code of C ++? I got run-time error for this question please help me asap! Errors are: In function 'void bfs(int, int)': warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int j = 0; j < adj[pppp].size(); j++){ ^ In function 'int main()': warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d %d %d", &a, &q, &c, &N, &m); ^...
Code in c++, do not use loops, make it as simple as it can be. Thanks!...
Code in c++, do not use loops, make it as simple as it can be. Thanks! Background Well it has finally happened; AMC’s “The Walking Dead” has become a reality. The zombie apocalypse took place. It has been a couple of years since we have started to “rebuild” our society, and now is the time for you to be able to shine. We have come across technology that will allow for us to get back to life as it once...
can someone translate this pseudo code to actual c++ code while (not the end of the...
can someone translate this pseudo code to actual c++ code while (not the end of the input) If the next input is a number read it and push it on the stack else If the next input is an operator, read it pop 2 operands off of the stack apply the operator push the result onto the stack When you reach the end of the input: if there is one number on the stack, print it else error
In the following code down below I am not getting my MatrixElementMult right. Could someone take...
In the following code down below I am not getting my MatrixElementMult right. Could someone take a look at it and help fix it? Also, when I print out the matrices I don't want the decimals. I know it's a format thing but I'm new to C and not getting it. Thanks! #include <stdlib.h> #include <stdio.h> #include <math.h> #define N 8 typedef struct _Matrix { double element[N][N]; } Matrix; void PrintMatrix(Matrix a){ int i,j; for(i=0;i<N;i++){ for(j=0;j<N;j++){ printf(" %.1f ",a.element[i][j]); }...
Run the following code and explain what the code does Run this code at least twice...
Run the following code and explain what the code does Run this code at least twice and take screenshots Explain what the code does What is the running result supposed to be What caused this issue? #include <thread> #include <iostream> using namespace std; const unsigned int NTHREADS = 20; const int ITERS = 10000000; int counter; void increment() {        for (int i = 0; i < ITERS; i++)                      counter++;               } void decrement() {        for (int i...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
Python programming: can someone please fix my code to get it to work correctly? The program...
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks! # Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it...
can someone finish and check my code on main. cpp? Its not working for me even...
can someone finish and check my code on main. cpp? Its not working for me even though im sure my code make sense is it possible to output each function to show they work. this is supposed to be a vector class library made from allocated memory i have included templated functions in the class file to help create the rest of the functions. Thank you so much note: i did not include main.cpp because it  was empty- im hoping someone...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT