3

I'm kind of inexperienced with C++, and I'm converting a program that I wrote in C to C++. I have a RollDice function that takes numbers that I read in from a text file and uses them to generate the number. This is the function in C:

void rollDice(Move *move, GameState *game_state) {
    int diceNum1 = 0;
    int diceNum2 = 0;
    int randomNumber1 = 0;
    int randomNumber2 = 0;
    randomNumber1 = game_state->randomNums[game_state->current_roll]; //gets the random number from the array randomNum (which holds the numbers from the text file), at index "current_roll"
    game_state->current_roll++; //increments so the next random number will be the next number in the array
    diceNum1 = 1 + (randomNumber1 % (1 + 6 - 1));
    randomNumber2 = game_state->randomNums[game_state->current_roll];
    game_state->current_roll++;
    diceNum2 = 1 + (randomNumber2 % (1 + 6 - 1));
    move->dice_sum = diceNum1 + diceNum2;
    printf("You rolled a %d!\n", move->dice_sum);
}

This works just how I want it to when I run it. Now, when converting my program to C++ I had to change things around. My parameters are now pass by reference and I made a vector to store the list of random numbers from the text file:

void rollDice(Move& move, GameState& game_state) {
    std:: vector<int> randomNums = game_state.getRandomNums();
    int current_roll = game_state.getCurrentRoll();
    int diceNum1 = 0;
    int diceNum2 = 0;
    int randomNumber1 = 0;
    int randomNumber2 = 0;
    randomNumber1 = randomNums.at(current_roll);
    current_roll++;
    diceNum1 = 1 + (randomNumber1 % (1 + 6 - 1));
    randomNumber2 = randomNums.at(current_roll);
    current_roll++;   //this line is grayed out and says "this value is never used"
    diceNum2 = 1 + (randomNumber2 % (1 + 6 - 1));
    move.dice_sum = diceNum1 + diceNum2;
    std:: cout << "You rolled a " << move.dice_sum << "!\n";
}

My code is telling me that the second time I increment current_roll it is unused. This didn't happen for my C code, so why is it happening here and how can I fix it? I'm completely lost.

  • But I need that line of code there to let the program know to increment the number for the next time the dice is rolled. Is there a way I can implement this somehow? – Jihane Eter Feb 10 '18 at 22:28
  • You really don't. Once you exit the method, that value is lost. And you don't use it in your method after that line. – O.O.Balance Feb 10 '18 at 22:30

1 Answers1

6

It's never used because you write to the variable, but never read from it. Having a variable that you never read is effectively meaningless.

Presumably your game_state.getCurrentRoll function returns an integer, when you store this, you store the value (rather than a reference to the value), thus incrementing it doesn't increment the current roll inside the game_state, instead you should add a function to your game_state called makeRoll for example which increments the game_states internal current_roll value.

This is different from your C code which increments the current_roll value directly using game_state->current_roll++ (alternatively you could make game_state.current_roll public and increment it the same way as in your C code).

From your comment I assume you have some class:

class GameState {
private:
    int current_roll;
    ...
public:
    int getCurrentRoll() {
        return current_roll;
    }
    ...
}

All you'd need to do is add another function to your class to increment the current_roll:

class GameState {
private:
    int current_roll;
    ...
public:
    int getCurrentRoll() {
        return current_roll;
    }
    void makeRoll() {
        current_roll++;
    }
    ...
}

Then you can call it as normal.


Regarding your new issue in the comments regarding the error:

parameter type mismatch: Using 'unsigned long' for signed values of type 'int'.

This is because the signature of at is std::vector::at( size_type pos ); That is, it expects a value of type size_type which is an unsigned integer type, rather than int as you're using which is signed. This post may be helpful.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
  • Thank you. So let's say I make a function that internally increments the current_roll value like you suggested-- something like "int makeRoll(int current_roll)". Since I would be making this in my game_state class, would I have to make it similar to a getter/setter function since my objects are private? – Jihane Eter Feb 10 '18 at 22:39
  • 1
    @JihaneEter see edit, my C++ is a little rusty so may not be bang on. There's no need for the added function to return anything or take any parameters as `current_roll` is within the instantiated class – Nick is tired Feb 10 '18 at 22:44
  • I think that should do it. Thank you! Kind of another unrelated problem, but within the same function of RollDice, I keep getting a note at this line: "randomNumber1 = randomNums.at(current_roll);", saying "parameter type mismatch: Using 'unsigned long' for signed values of type 'int'." It's referring to current_roll. Any ideas? Both my variables are of type int – Jihane Eter Feb 10 '18 at 22:53
  • 1
    @JihaneEter added some info which may help, you could try using the `randomNums[current_roll]` instead but again my C++ is rusty – Nick is tired Feb 10 '18 at 23:02