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.
#include <iostream>
usingnamespace 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;
}
}
elseif (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.
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 '='