### Baffled by lack of iterations

I would consider myself a beginner to C++. I took a couple of classes in high school that dealt with simple C++, but that was 12 years ago. I haven't really touched it since.

I wanted to dive back in and create a simple console application that would calculate the odds of dice rolls for the Star Wars: Edge of the Empire role playing game. For the most part, it was working pretty well, but I got stuck on this particular loop. It is supposed to go through 2 iterations, but it seems that the variable "n" is incrementing prematurely.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include using namespace std; int main() { int abi_num [2] = {0,0}; int green = 2; for (int n = 0; n < green; n++) { cout << n << " " << abi_num [n] << endl; cout << "Green: " << green << endl; if (n = green - 1) // Is this the last green die being rolled? { cout << "This is the last die!" << endl; if (abi_num [n] = 8) //You are here because there are no yellow or blue dice following this { //Has this green die come to the end of its increments? abi_num [n] = 1; //If so, start back at 1 } else { abi_num [n] = abi_num [n] + 1; } } else if (abi_num [n + 1] = 8) //This is not the last green die being rolled. { //Oh, well, has the next green die come to the end of it's increments? cout << "This is not the last die, but the next die equals 8!" << endl; if (abi_num [n] = 8) //If so, has this green die come to the end of its increments? { abi_num [n] = 1; //If so, start back at 1 } else { abi_num [n] = abi_num [n] + 1; //If not, add one to its current value } } else { cout << "This is not the last die and the next die does not equal 8... Lame!" << endl; abi_num [n] = abi_num [n]; } cout << "End of " << n << " iteration(s)." << endl; } return 0; }``````

Here is the resulting output:

-----------------
0 0
Green: 2
This is the Last die!
End of 1 iteration(s).
-----------------

I would expect "n" to still be equal to 0 at the end of the first pass. Here is the output that I expected:

-----------------
0 0
Green: 2
This is not the last die and the next die does not equal 8... Lame!
End of 0 iteration(s).

1 0
Green: 2
This is the Last die!
End of 1 iteration(s).
-----------------

Any suggestions in fixing this would be appreciated.
Line 15: `(n = green - 1)` You are assigning n value of 1 here. Your compiler should warn about it. Remember = is an assigment. Comparsion is ==
You are using the assignment operator '=' instead of the is-equal-to operator '=='
`if (n = green - 1)`
here the result of green-1 is assigned to the variable n, and if the result is non-zero, the condition is true.

Similarly on lines 18, 27 and 30 you need '==' instead of '='
Last edited on
Nice! I should've remembered that. Thank you to both of you!
Topic archived. No new replies allowed.