mardi 31 octobre 2017

Working with do-while loops

So this is my 4th project, and my project is this: Create a program to allow a user to play a modified “craps” games. The rules will be as follows:

1) User starts with $50.00 as their total. 2) Ask the user for their bet (maximum bet is their current total). 3) Throw a pair of dice.

a) If the total value is 7 or 11, the user is an instant winner. The user has the amount bet added back into their total. Ask the user if they wish to play again and if so they start at step two above. b) If the total value is 2, 3, or 12, the user is an instant loser. The user has the amount bet deducted from their total. Ask the user if they wish to play again and if so they start at step two above. c) If the total is anything else, remember this total as the “point” and roll again.

i) If the new total is equal to the “point”, the user wins and the process is the same as winning in (a) above ii) If the new total is a 7, the user loses. And the process is the same as losing in (b) above. iii) If the new total is anything else the user must roll again and again try to match the “point” of the first roll.

This is the code I have so far. Everything works fine like it's supposed to, except the program loops itself without me inputting 'y' or 'Y'. It keeps looping itself until the program gets a total (dice_total) that allows you to win or lose, then ask you to if you want to restart the game. Any other total would just cause the program to keep looping. Also, this is my first time using do-while loops, so I am not sure if I did it correctly.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <time.h>
#include <string>
using namespace std;

int main()
{
//Declaring Variables**********
char again = 'y';
char play = 'r';
double starting_amount = 50.00;
double bet_amount = 0.00;
int dice1;
int dice2;
int dice_total;

//*****************************
srand(time(NULL));// Setting true random seed
//*****************************
//Restart the game, if you win or lose.
while (again == 'y' || again == 'Y')//Beginning of the again while loop
{
    //Setting the execution of the program***************************************************************
    cout << "You currently have this amount of money to bet $" << starting_amount << endl;
    cout << "How much do you want to bet?" << endl;
    cin >> bet_amount;
    double current_amount = starting_amount - bet_amount;
    cout << "You are betting $" << bet_amount << " and you have $" << current_amount << " left" << endl;
    //***************************************************************************************************

    do
    {//Beginning of do-while
        int point = 0;
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;
        dice_total = dice1 + dice2;

        cout << "Throwing dices, standby........." << endl;
        cout << "You got " << dice1 << " and " << dice2 << " for a total of " << dice_total << endl;
        //If statements to set the win or lose settings.
        if (dice_total == 7 || dice_total == 11)
        {
            cout << "You are a winner congrats :D Would you want to play again? <Press y to play again>" << endl;
            cin >> again;
        }
        else if (dice_total == 2 || dice_total == 3 || dice_total == 12||current_amount==0.00)
        {
            cout << "You lost, well better luck next time!!!<Press y to play again" << endl;
            cin >> again;
        }
        else
        {
            point++;
            cout << "Your score is " << point << " would you want to keep playing? <Press r, to keep playing" << endl;
        }

    }//End of do
    while (play == 'r' || play == 'R');
        cout << "Game has ended, would you want to restart the whole game?" << endl;
        cin >> again;
    }//End of the again while loop

}

Aucun commentaire:

Enregistrer un commentaire