Thread: HELP! more frustated i cannot be.....

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    Unhappy HELP! more frustated i cannot be.....

    I been having problems getting this program to work the way i want it, its been 2 long days that i been modifing it and still i can't get it to run nicely.


    any help that you provide will be gladly accepted
    thank you for your help..

    [tag]

    [code]

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    class Craps
    {
    private:
    int sum, myPoint, die1, die2, workSum;
    double bet, bettotal;

    public:
    int rollDice(); // function prototype
    void internalGame();
    };

    //------------------------------------------------------------------------------
    int Craps::rollDice()
    {
    die1 = 1 + rand() % 6;
    die2 = 1 + rand() % 6;
    workSum = die1 + die2;
    cout << "Player rolled " << die1 << " + " << die2
    << " = " << workSum << endl;

    return workSum;
    }

    //------------------------------------------------------------------------------
    void Craps::internalGame()
    {

    cout <<"\a--------\a--------------------\a--------------\a" << endl;
    cout <<"Enter the amount you have available to play: $";
    cin >> bettotal;
    cout << "\n";

    do{

    cout << "Enter the amount you want to bet for this play: $" ;
    cin >> bet;

    while (bet>bettotal)
    {
    cout << "you cannot bet more than you have, \nplease enter "
    << "again the bet amount, less or equal to "<<bettotal<< endl;
    cin >> bet;
    }

    enum Status { CONTINUE, WON, LOST };
    Status gameStatus;

    srand(time( 0 ));
    sum = rollDice(); // first roll of the dice

    switch ( sum ) {
    case 7:
    case 11: // win on first roll
    gameStatus = WON;
    break;
    case 2:
    case 3:
    case 12: // lose on first roll
    gameStatus = LOST;
    break;
    default: // remember point
    gameStatus = CONTINUE;
    myPoint = sum;
    cout << "Point is " << myPoint << endl;
    break; // optional
    }

    while (gameStatus == CONTINUE ) // keep rolling
    {
    sum = rollDice();

    if (sum == myPoint) // win by making point
    gameStatus = WON;

    {
    if ( sum == 7 ) // lose by rolling 7
    {
    gameStatus = LOST;
    cout << "\nYou lost the amount of your bet $" << bet << endl;

    cout << "now you have $" << bettotal << " to play: ";
    bettotal=(bettotal-bet);
    }
    }
    }

    if ( gameStatus == WON )
    {
    cout << "Player wins\n";
    cout << "\nYou receive your bet,plus your the amount you won $:"
    << bet*2 <<"\n";
    cout << "it's a total of $" << bettotal+bet
    << " available to play \n";
    bettotal=bettotal+(bet*2);
    bettotal=bettotal+bet;

    }
    else
    {
    cout << "Player loses" << endl;

    cout << "\nYou lost the amount of your bet $"<<bet<<endl;
    bettotal=(bettotal-bet);
    cout << "now you have $"<<bettotal<<" to play: \n";

    bettotal=bettotal-bet;
    }}

    while( bettotal>0);
    }

    //------------------------------------------------------------------------------
    int main()
    {
    char playagain;
    Craps Execution;
    do{
    Execution.internalGame();
    Execution.rollDice();
    cout << "do you want to play again? Y or N ";
    cin >> playagain;
    }
    while( playagain='Y'||playagain=='y');
    return 0;
    }

    /*

    OUTPUT

    ------------------------------------------
    Enter the amount you have available to play: $50

    Enter the amount you want to bet for this play: $10
    Player rolled 4 + 3 = 7
    Player wins

    You receive your bet,plus your the amount you won $:20
    it's a total of $60 available to play
    Enter the amount you want to bet for this play: $30
    Player rolled 1 + 3 = 4
    Point is 4
    Player rolled 3 + 5 = 8
    Player rolled 1 + 3 = 4
    Player wins

    You receive your bet,plus your the amount you won $:60
    it's a total of $110 available to play
    Enter the amount you want to bet for this play: $30
    Player rolled 3 + 5 = 8
    Point is 8
    Player rolled 2 + 1 = 3
    Player rolled 6 + 2 = 8
    Player wins

    You receive your bet,plus your the amount you won $:60
    it's a total of $200 available to play
    Enter the amount you want to bet for this play: $200
    Player rolled 4 + 4 = 8
    Point is 8
    Player rolled 4 + 6 = 10
    Player rolled 5 + 6 = 11
    Player rolled 3 + 3 = 6
    Player rolled 5 + 5 = 10
    Player rolled 3 + 5 = 8
    Player wins

    You receive your bet,plus your the amount you won $:400
    it's a total of $460 available to play
    Enter the amount you want to bet for this play: $460
    Player rolled 4 + 6 = 10
    Point is 10
    Player rolled 4 + 3 = 7

    You lost the amount of your bet $460
    now you have $860 to play: Player loses

    You lost the amount of your bet $460 << IT REPEATS IT SELF, IT SHOULD
    now you have $-60 to play: ONLY DO IT ONCE, AND THEN ASK FOR NEW BET
    Player rolled 4 + 2 = 6 USING THE UPDATED BETOTAL, ALSO IT SHOULD NOT GO
    do you want to play again? Y or N BEYOND $ 0.

    */
    [/tag]
    Last edited by moenia; 04-29-2003 at 01:30 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1)I WANT IT TO KEEP TRACK OF BETTOTAL

    Move the start of your do-while loop below where you read in how much money they want to start with. You only want that to happen once, so don't put it inside the loop.

    2)IT SHOULD NOT ALLOW TO TAKE HIGHER BETS


    cout<<"Enter the amount you want to bet for this play: "<<endl;
    cin>>bet;

    while(bet > bettotal)
    {
    cout<<"you cannot bet more than you have, please enter again the bet amount: "<<endl;
    cin>>bet;
    }

    3)THE GAME SHOULD END AT WHEN THE USER HAS $0

    Continue the do-while loop only if the user answers yes AND they still have more money.

    Next time read the first and second post on the forum list before posting.
    Last edited by 7stud; 04-29-2003 at 01:54 AM.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    One piece of advice...

    Greetings,

    Don't mix C++ standard headers with the non-standard/deprecated headers. "iostream.h" is deprecated, the standard header is "iostream", and please do use the code tags, it makes it much easier for everybody to read your code thus makes it easier to provide help.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    class Craps
    {
    private:
        int sum, myPoint, die1, die2, workSum;
        double bet, bettotal;
        char playagain;
    
    public:
        int rollDice(); // function prototype
        void internalGame();
    };
    
    //------------------------------------------------------------------------------
    int Craps::rollDice()
    {
        die1 = 1 + rand() % 6;
        die2 = 1 + rand() % 6;
        workSum = die1 + die2;
        cout << "Player rolled " << die1 << " + " << die2
    	 << " = " << workSum << endl;
    
        return workSum;
    }
    
    //------------------------------------------------------------------------------
    void Craps::internalGame()
    {
        do
        {
    	cout <<"\a--------\a--------------------\a--------------\a" << endl;
    	cout <<"Enter the amount you have available to play: ";
    	cin >> bettotal;
    	cout << "\n";
    
    	cout << "Enter the amount you want to bet for this play: " << endl;
    	cin >> bet;
    
    
    	if (bet>bettotal)
    	{ 
    	    cout << "you cannot bet more than you have, please enter"
    		 << "again the bet amount: "<< endl;
    	    cin >> bet;
    	}
    
    	enum Status { CONTINUE, WON, LOST };
    	Status gameStatus;
    
    	srand(time( 0 ));
    	sum = rollDice(); // first roll of the dice
    
    	switch ( sum ) {
    	    case 7: 
    	    case 11: // win on first roll
    		gameStatus = WON;
    		break;
    	    case 2: 
    	    case 3: 
    	    case 12: // lose on first roll
    		gameStatus = LOST;
    		break;
    	    default: // remember point
    		gameStatus = CONTINUE;
    		myPoint = sum;
    		cout << "Point is " << myPoint << endl;
    		break; // optional 
    	    }
    
    	while (gameStatus == CONTINUE ) // keep rolling
    	{ 
    	    sum = rollDice();
    
    	    if (sum == myPoint) // win by making point
    		gameStatus = WON; 
    
    	    { 
    		if ( sum == 7 ) // lose by rolling 7
    		{ 
    		    gameStatus = LOST;
    		    cout << "\nYou lost the amount of your bet $" << bet << endl;
    		    bettotal=(bettotal-bet);
    		    cout << "now you have $" << bettotal << " to play: ";
    		}
    	    }
    	}
    
    	if ( gameStatus == WON )
    	{
    	    cout << "Player wins\n";
    	    cout << "\nYou receive your bet: $" 
    		 << bet <<" plus your the amount you won $" 
    		 << bet <<"\n";
    	    cout << "it's a total of $" << bettotal+bet
    		 << " available to play \n";
    	    bettotal=bet+bet;
    	    cout << "Please enter new the bet amount :";
    	    cin >> bet;
    	}
    	else
    	{
    	    cout << "Player loses" << endl;
    	    cout << "\nYou lost the amount of your bet $"<<bet<<endl;
    	    bettotal=(bettotal-bet);
    	    cout << "now you have $"<<bettotal<<" to play: \n";
    	    cout << "do you want to play again? Y or N ";
    	    cin >> playagain;
    	}
        }
        while ( playagain=='Y' || playagain=='y');
    }
    
    //------------------------------------------------------------------------------
    int main()
    { 
        Craps Execution;
    
        Execution.internalGame();
        Execution.rollDice();
    
        return 0;
    }
    See, much sexier this way!
    Now, apply 7stud's suggestions to your code and it should work the way you intended it to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Frustated with mess table printing
    By benedicttobias in forum C Programming
    Replies: 6
    Last Post: 04-16-2009, 05:50 PM