I am currently working on a dice game. Where The user rolls a pair of dice first, so lets say he rolled and Dice 1 = 2 and Dice 2 = 3. So the total is 5 now. Now, he needs to get 5 (total) again in order to win, if he didnt get 5 in his next move then he rolls again and the game continues. But he looses if at any point of time, he rolled a total of two.

So,please tell me how do I store value of the first roll and compare it with the next move(s). I tried something, but it does not seem to work.

    #include<iostream>
    #include<ctime>      // for the time() function
    #include<cstdlib>    // for the srand() and rand() functions
    using namespace std;

    // Declare variables
    //int compInput;
    int userInput;
    int die1 = 0;
    int die2 = 0;
    int dieTotal = 0;
    int Dice ()
    {
        // roll the first die
        die1 = (rand() % 6 ) + 1;
        // roll the second die
        die2 = (rand() % 6 ) + 1;


    }

    // iniating a second two pair dice function.
    int compDice()

    {
        Dice();
        dieTotal = die1 + die2;
        return (dieTotal);
    }



    // User Rolling the dice and calucalting the total here

    int userGame()
    {
        cout << "\nUser turn --- Press 2 to roll" << endl;
        cin >> userInput;

        if ( userInput == 2 )
        {
            Dice ();
            cout << "\nThe user rolled        Dice 1 = " << die1 << " and Dice 2 = " << die2 << endl;
            cout << "Total = " << die1 + die2 << endl;
        }

        else {
            cout << "Wrong input.";
            //userGame();
        }
        return (die1 + die2 );
    }

    int checkForWin ()
    {
        while (true)
        {

            int result1 = compDice();
            int result = userGame();

            // int finalResult = dieTotal;
            if (result == result1 )
            {
                cout << "\nUser won. Computer looses....m " << endl;
                break;
            }

            else if (result == 2)
            {
                cout << "\nUser looses. Computer won." <<endl;
                break;
            }

            else
            {
            }
        }
    }

    // Calling for the checkForWin() function in main and the srand.
    int main ()
    {
        cout << "This is the Dice game. " << endl;

        // set the seed
        srand(time(0));
        checkForWin(); // Initiating the game.
        return 0;
    }
#include <windows.h>
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int Die_1 = 0, Die_2 = 0;

void RollDice()
{
    Die_1 = (rand() % 6 ) + 1;
    Die_2 = (rand() % 6 ) + 1;
}

int ComputerRolls()
{
    RollDice();
    return (Die_1 + Die_2);
}


int UserRolls()
{
    char UserInput = 0;
    std::cout<<"Press two to roll"<<std::endl;
    cin>> UserInput;
    cin.ignore();

    if (UserInput == '2')
    {
        RollDice();
        std::cout<<"You Rolled: "<<Die_1<<" And: "<<Die_2<<std::endl;
        std::cout<<"For a grand total of: "<<Die_1 + Die_2<<std::endl;
        return (Die_1 + Die_2);
    }
    return -1;
}

void WinnerResult(int UserResult, int ComputerResult)
{
    if (UserResult == ComputerResult)
    {
        std::cout<<"You Tied with the computer!"<<std::endl;
    }
    else if (UserResult > ComputerResult)
    {
        std::cout<<"You Won!"<<std::endl;
    }
    else
        std::cout<<"You Lost! The computer rolled a grand total of: "<<ComputerResult<<std::endl;
}

int main()
{
    WinnerResult(UserRolls(), ComputerRolls());
}

So,please tell me how do I store value of the first roll and compare it with the next move(s). I tried something, but it does not seem to work.

The same way to do it with any value -- create a variable to store the first roll and use that to compare all subsequent rolls.

@triumphost, thats not how I intended the program to run.

@Waltp .. i am doing it here -

int result1 = compDice();
int result = userGame(); 

or where else do you mean? And the first roll?

You asked

So,please tell me how do I store value of the first roll and compare it with the next move(s).

I gave you a suggestion about how to do it. You responded with

@Waltp .. i am doing it here -

int result1 = compDice();
int result = userGame();

So what's the problem? Use those results and compare the subsequent rolls.

If you notice

if (result == result1 )

I am comparing it. But its not fucntioning right. For instance if the total is 6 for the first move and when the user gets 6 again, the game continues. Instead he should be shown with the "you won ...." message.

So what are you comparing above?

I suggest you change your variable names to something worthwhile. result and result1 are not indicative of their use. resultPerson and resultComputer define closer what the values hold -- but you can still come up with better names. Many times that helps you 'see' what you are trying to do.

You have something you've called a "first roll". What would be a good name for that variable? Do you need the "first roll" more than once? Is it important to keep the "first roll" separate from subsequent rolls? Plan accordingly.

oK, I understand that. And I have changed the names. And yes, first rolls needs to be stored. And needs to be compared with the next rolls. Thats the plan, but either I am making a mistaked in the loop or somewhere else.??

here is the output, http://pastebin.com/Q0pfGGrN . See that the first roll total was 8. But when 8 came the next time, it just continued with the game..

Not going to any pastebin. If you can waste the time loading onto pastebin, you can certainly take less time and copy/paste your info here.

Did you make changes to the code? Do we know what the code looks like now?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.