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,...
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]); }...
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...
Why does my code print nothing in cout? I think my class functions are incorrect but...
Why does my code print nothing in cout? I think my class functions are incorrect but I don't see why. bigint.h: #include <string> #include <vector> class BigInt { private:    int m_Input, m_Temp;    std::string m_String = "";    std::vector<int> m_BigInt; public:    BigInt(std::string s)   // convert string to BigInt    {        m_Input = std::stoi(s);        while (m_Input != 0)        {            m_Temp = m_Input % 10;            m_BigInt.push_back(m_Temp);       ...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops...
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops to do the same thing this code does. Be sure that you understand how the code works and how the pointer arithmetic relates to the array expression form. Provide liberal comments to explain what your pointer arithmetic is computing. #include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { int arg_count = 0; for (arg_count = 0; arg_count < argc; arg_count++) printf("%s\n", argv[arg_count]); }
C#: If the code is the only thing that you can provide without the pictures of...
C#: If the code is the only thing that you can provide without the pictures of the form, then that is fine as well. Topics: hiding/showing controls, StatusStrip, custom event arguments, radio buttons Students will create an application that uses a dialog to create a spaceship object to populate a ListView. This ListView will be able to display in either large or small icon mode using a menu option to switch between them. Selecting a spaceship in the ListView will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT