### Multiple variables=themselves

I want the loop to continue until i gets the output 6 6 6 6 6 6, but I'm getting outputs like 5 2 4 4 6 1 instead. (I think the problem is line 15 where i create the arrays or line 18 where i looks whether everything=6)

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include using namespace std; int main () { start: time_t start,end; time (&start); int count = 0; gen: srand (time(0)); int dice [6] = {rand () % 6 + 1, rand () % 6 + 1, rand () % 6 + 1, rand () % 6 + 1, rand () % 6 + 1, rand () % 6 + 1}; count = count + 1; if (dice[0] == 6, dice[1] == 6, dice[2] == 6, dice[3] == 6, dice[4] == 6) { goto done; } else goto gen; done: float dif; time (&end); dif = difftime (end,start); cout << "it took " << dif << " seconds\n"; cout << "it took " << count << " tries\n\n"; cout << dice[0] << " " << dice[1] << " " << dice[2] << " " << dice[3] << " " << dice[4] << " " << dice[5] << "\n\n"; cin.get(); return 0; }``````
You have to use && instead of , in the if condition. Did you forget to check if dice[5] is 6? Since you are using an array you could make use of for loop to avoid having to repeat so much code everywhere.
Last edited on
so `int dice [6]` creates from dice [0] and up to dice [6]?
As i understood it, it created six array-varable-thingys: dice[0] - dice[5]

Sorry noticed first now, yes I forgot `dice[5] == 6`
I'll also make sure to look into the for loop.
Last edited on
Your understanding is correct. I was referring to that you didn't have `dice[5] == 6` in the if statement.
so `if (dice[0] == 6&& dice[1] == 6&& dice[2] == 6&& dice[3] == 6&& dice[4] == 6&& dice[5] == 5)` is correct, yes?
Also, I just noticed that the variables will only randomize every second when using `srand (time(0));` are there any way to randomize more often than this?
Last edited on
So, no more support?
Yes that is correct.

The problem is that when you call srand() with the same seed you will get the same sequence of numbers from rand(). time() returns the time in seconds so that's why you only get different numbers every second. The solution is to call srand() only once.
Thanks for all the help.
Got it working now, it only took 6402 tries before it got dice[0] == dice[1] and so on (That as the purpose of the program, I just changed it to 6 to see if it had anything to do with the problem)
Topic archived. No new replies allowed.