Multiple variables=themselves

Dec 4, 2012 at 5:59pm
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <stdlib.h>
#include <time.h>

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;
}
Dec 4, 2012 at 6:07pm
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 Dec 4, 2012 at 6:08pm
Dec 4, 2012 at 6:14pm
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 Dec 4, 2012 at 6:17pm
Dec 4, 2012 at 6:15pm
Your understanding is correct. I was referring to that you didn't have dice[5] == 6 in the if statement.
Dec 4, 2012 at 6:23pm
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 Dec 4, 2012 at 6:26pm
Dec 4, 2012 at 6:58pm
So, no more support?
Dec 4, 2012 at 7:33pm
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.
Dec 5, 2012 at 12:53pm
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.