im still not sure what im doing wrong. i tried changing it a little but it still doesnt output anything when i call cout at the end. it should be a random combination of 'XO'.
first, in value_assigner, the second if is redundant. If e is even, O, else X. Much simpler to write
//eliminate e, and after moving srand, the whole function becomes
g = 'X';
if ( (rand()%300+1) == 0)
g = 'O';
return g;
srand should only be called once in a program for normal uses. Call it in main.
switches should have a default case.
the main function is extra convoluted. If this is place-holder code for future work, ok, but if this is what you want, simply do this:
int main()
{
srand(time(0));
char x; //and honestly this can be removed as well, but I left it for now.
cout << value_assigner(x) << value_assigner(x);
return 0;
}
Finally, the actual bug and cause of your problems (the above is just cleanup, you are doing way too much work for what needs doing across the board. Keep it simple where you can!)
change it to
x = value_assigner(x);
y = value_assigner(y);
OR if you want it to work the way you have it, do this INSTEAD
void value_assigner(char &g)
ok i understand that its really bad looking and i will clean it up once i get the correct output. but the problem im still getting is the output is always XX or OO. its never a combination. i can have XX or OO but it shouldnt be the output everytime.
srand is what seeds rand. srand(time(0)) seeds rand with the current second in time. If you call srand twice within the same second, you will be reseeding it with the same value, and it will cause rand() to generate the same output each time.
Looking at the latest code you've posted - there's no point at all passing g as an argument to the function. You're returning the value your function generates anyway, so why do you also need to take an argument for the function to modify?
there is a c++ newer random tool, but some more info..
srand basically sets a starting point. using time randomizes this so its not the same every time you run your program.
if you just did this
srand(3);
and printed the first 10 random values from rand as a program
it would print the same 10 values every time forever.
that can be useful when debugging or if you need a stream that is the same every time. It is not terribly random when you need actual random values, though, so after debugging you set it to time(0).
calling it twice in a row very quickly is the same as calling it with srand(3); srand(3); because time(0) changes slowly relative to execution speeds. You reset the stream and get the same number twice every time.
but that isnt the bug per-se. The bug, as I said above, was not assigning your values at all. The srand bug just makes you get 2 of the same value every time you run.
The pointer thing is, like the rest of the code, the most complicated solution you could find.
look.
void value_assigner(char &g)
{
g = 'X';
if(rand()%2 == 0) //the 300+1 is not making it any more random or doing anything at all.
g = 'O';
}