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

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
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...
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]); }
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....
Language: C++ NEEDS TO WORK IN VISUAL BASIC The code is broken and loops in a...
Language: C++ NEEDS TO WORK IN VISUAL BASIC The code is broken and loops in a few places please fix it #include<iostream> using namespace std; //function declaration void EnterRents(int*, int); void displayRents(int*, int); void selectionSort(int*, int); int sumRents(int* temp, int size) {    int sum = 0;    for (int i = 0; i < size; i++)    {        sum += *(temp + i);    }    return sum; } void Displaymemory(int* temp, int size) {    /*int...
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...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT