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 6660159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:07:14+00:00 2026-05-26T02:07:14+00:00

I have two functions where I’m adding random numbers to the total value. The

  • 0

I have two functions where I’m adding random numbers to the total value.

The problem is that every time I call the function and print the total, it does not add. If it generates 2 it will say the total is 2. After that if I call it again and it generates 5, it says the total is 5, and doesn’t add (it should be 7 if this happened.)

Everything looks fine here…

int human(int humanscore)
{
    int diceRoll= rand() % 7;
    cout << "player rolled: ";
    humanscore+= diceRoll;
    cout << diceRoll;
    cout << "human score is: " << humanscore;
}

int computer(int compscore)
{
    int diceRoll= rand() % 7;
    cout << "computer rolled: ";
    compscore+= diceRoll;
    cout << diceRoll;
    cout << "computer score is: " << compscore;
}
  • 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-05-26T02:07:14+00:00Added an answer on May 26, 2026 at 2:07 am

    You are modifying the value of the internal copy of the argument passed to the functions. If you want the change to be done on the external variable, change your function definitions to take references instead: int& score.

    Also note that rand() % 7 will give you a value in the range [0, 6]. A dice has values in the range [1, 6], you should use 1 + rand() % 6 instead.

    * Update: *

    This can be done with C++ references:

    int computer(int& compscore)
    {
        ...
        compscore += diceRoll;
        ...
    }
    
    int var = 0;
    computer( var );
    

    For this declaration, the function takes the actual variable var as an argument, and changes done to compscore within the function are reflected in the variable var as compscore and var for that particular invocation are just aliases to the same variable.

    In C the same effect is achieved with pointers:

    int computer(int* compscore)
    {
        ...
        *compscore += diceRoll;
        ...
    }
    
    int var = 0;
    computer( &var );
    

    This invocation of the function gives it the address of the variable which should be changed. For general use, you can assume that the first implementation using references will automatically generate a solution by the compiler similar to this last snippet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
I have two functions whose execution time I would like to compare: function subject_one()
I have two functions written in VB.NET: 1) The first function (call it GetValues())
I have two functions, one that creates a table like so: function return_formatted_results(result_no,addy,title,own_comment) {
Let's say for example that I have two functions with random code inside and
I have two functions to add and remove table rows that contain a form
I have two functions that have different enough logic but pretty much the same
I have two functions in an ActionScript class, they are: private function loaderCompleteHandler(event:Event):void {
I have two Python functions, both of which take variable arguments in their function
So I have two jquery functions that bassically do the same, but on 2

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.