Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:33:11+00:00 2026-06-11T12:33:11+00:00

I am currently working on a dice game. Where The user rolls a pair

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:33:13+00:00Added an answer on June 11, 2026 at 12:33 pm

    After our comment chat/misunderstandings, I’ve taken the liberty of copying your code and modifying it (as little as possible to maintain your coding style – I would not recommend this style for any future projects) to produce the results you want. Let me know if it works (simple testing showed it worked, may have missed some other quirks)

    #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 firstRoll = 1;
    int die1 = 0;
    int die2 = 0;
    int dieTotalToMatch = 0;
    void Dice ()
    {
        // roll the first die
        die1 = (rand() % 6 ) + 1;
        // roll the second die
        die2 = (rand() % 6 ) + 1;
    
    }
    
    // iniating a second two pair dice function.
    void compDice()
    {
        Dice();
        dieTotalToMatch = die1 + die2;
    }
    
    
    
    // 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 );
    }
    
    void checkForWin ()
    {
    
        while (true)
        {
    
            int result = userGame();
            if (firstRoll)
            {
                dieTotalToMatch = result;
                firstRoll = 0;
                continue;
            }
            // int finalResult = dieTotal;
            if (result == dieTotalToMatch )
            {
                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.
        cin.ignore();
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a simple GUI application that rolls two dice. I
Currently working for the first time with JSON and with little experience of jQuery.
Im currently working on a game that uses multi touch to apply zoom to
Currently working on a game, and is converting it to retina. I am using
I'm currently working on learning some game development with Java. I'm working on the
Im currently working on making a flash platformer engine...but my collision detect needs some
Currently working on a snippet to input variables in which a user changes a
I'm currently working on creating a new C# project that needs to interact with
Im currently working with the new version of our lovely framework. Now where I
im currently working on a classification task using language modelling. The first part of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.