Arduino program is setting a variable equal to 0 automatically?

So just as a breakdown of the code, I'm making a device that chooses a random number, either 0 or 1 (it's supposed to represent each side of a coin), and the user has to guess what number the program picked (which side of the coin basically) by pressing one of two buttons, with each one corresponding to 0 and 1 respectively. If the two numbers are the same, meaning the user guessed correctly, a green light will go on and if the two numbers are different, meaning the user guessed incorrectly, a red light will go on. I have two variables that represent the numbers picked by the program and by the user, with coinvalueactual being the one picked by the program and coinvalueguessed being the one picked by the user. The variables answer and answer2 each correspond to the buttons, and I have 2 if statements at the top of the loop that change the value of coinvalueguessed to 0 or 1 based on which button was pressed. However, after lots of testing I realized that no matter which button is pressed, coinvalueguessed is being automatically set to 1 and I can't figure out why. Any guesses? And also, everything else works.

void setup() {
 // put your setup code here, to run once:
pinMode(2,INPUT);
pinMode(9,INPUT);
pinMode(3,OUTPUT);
pinMode(8,OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}

void loop() {
 // put your main code here, to run repeatedly:
int coinvalueactual = random(0,2);
int coinvalueguessed;
int reset = 0;
int answer = digitalRead(2);
int answer2 = digitalRead(9);
delay(5000);
Serial.print(coinvalueactual);
if (answer == LOW)
{
 if (answer2 == HIGH)
 {
 Serial.print(answer2);
 coinvalueguessed = 0;
 }
}
if (answer == HIGH)
{
 if (answer2 == LOW)
 {
 Serial.print(answer);
 coinvalueguessed = 1;
 }
}
if (coinvalueactual == 0)
{
 if (coinvalueguessed == 0)
 {
 delay(250);
 Serial.print("Right");
 digitalWrite(3, HIGH);
 digitalWrite(8, LOW);
 //something to turn the servo 
 reset = 1;
 }
}
if (coinvalueactual == 0)
{
 if (coinvalueguessed == 1)
 {
 delay(250);
 Serial.print("Wrong");
 digitalWrite(8, HIGH);
 digitalWrite(3, LOW);
 //something to turn the servo 
 reset = 1;
 }
}
if (coinvalueactual == 1)
{
 if (coinvalueguessed == 0)
 {
 delay(250);
 Serial.print("Wrong");
 digitalWrite(8, HIGH);
 digitalWrite(3, LOW);
 //something to turn the servo 
 reset = 1;
 }
}
if (coinvalueactual == 1)
{
 if (coinvalueguessed == 1)
 {
 delay(250);
 Serial.print("Right");
 digitalWrite(3, HIGH);
 digitalWrite(8, LOW);
 //something to turn the servo 
 reset = 1;
 }
}
if (reset = 1)
{
 delay(5);
 exit(0);
}
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

if (reset = 1)
{

Oops.

How would that be the problem? reset is initially 0, and if one of the 4 if statements is satisfied reset gets set equal to 1. The program resets, that's what's supposed to happen. But the program doesn't reset until AFTER coinvalueguessed receives a value, which it hasn't yet, because none of the if statements can be satisfied if there is no value for coinvalueguessed yet.

The question is what are you trying to do within the the if(conditional)
Are you comparing to items? Or are you changing the rest equal to 1?

Comparison Operators

== (equal to)
https://www.arduino.cc/en/Reference/If

your code you are making the reset = 1 which is always true inside the if(conditional)

You've got a lot of delays for a sketch that is reading switches. How are the switches wired? I think your issue is how you read the switches.

I can see simpler logic as well. Both the guess and the "coin toss" can be either 0 or 1. Sum them. The possible sums are 0, 1 and 2. Both 0 and 2 are correct. If the sum is 1 the guess is wrong. I also see a state machine that is either flipping a coin, waiting for the guess or displaying the result.

At the beginning you have a delay(5000) and then you read the Buttons.
Do you realize that you will not detect a button press which occurs during the delay(5000) but will only read the Buttons once, just a microsecond over 5 seconds after the Loop starts?

I think you want to replace that delay(5000) with something that waits until a button press is detected.

answer = 0; // Initialize answer to not pressed
answer2 = 0; // Initialize answer2 to not pressed
while(! (answer || answer2)) // while not (answer or answer2)
{ answer = digitalRead(2); // Read one button to answer
answer2 = digitalRead(9); } // Read other button to answer2

//At this Point, some button has been pressed so evaluate everything.

Of course, you are locking the machine up and probably shouldn't do that but once you get it to work you can work on not locking the machine. Locking the machine is not really bad if you absolutely have nothing else for it to be doing anyway and don't mind power consumption being at a constant 100%.

JaBa:
At the beginning you have a delay(5000) and then you read the Buttons.
Do you realize that you will not detect a button press which occurs during the delay(5000) but will only read the Buttons once, just a microsecond over 5 seconds after the Loop starts?

I think you want to replace that delay(5000) with something that waits until a button press is detected.

answer = 0; // Initialize answer to not pressed
answer2 = 0; // Initialize answer2 to not pressed
while(! (answer || answer2)) // while not (answer or answer2)
{ answer = digitalRead(2); // Read one button to answer
answer2 = digitalRead(9); } // Read other button to answer2

//At this Point, some button has been pressed so evaluate everything.

Of course, you are locking the machine up and probably shouldn't do that but once you get it to work you can work on not locking the machine. Locking the machine is not really bad if you absolutely have nothing else for it to be doing anyway and don't mind power consumption being at a constant 100%.

So I tried your suggestion (and fixed the reset = 1 thing) but it's still not working and coinvalueguessed is still automatically being set to 0, and the buttons are not being read. I think what may be happening is that it's reading one of the buttons as low, and then continuing on with the loop since the while statement is now satisfied, but that doesn't give the user any time to press the button and set it to high, which is the whole point of the code.

goodatthis:
So I tried your suggestion (and fixed the reset = 1 thing) but it's still not working...

I wonder if posting the new revision would increase your chances of receiving help?

Coding Badly is correct. It would be nice to see the program as it now stands. It also would be nice to know what the Serial.prints are showing you. Even better would be if you made a Change before posting both.

Right after reading the Buttons, where you now have the single line:
Serial.print(coinvalueactual);

Please Change it to read:
Serial.print(coinvalueactual);
Serial.print(answer);
Serial.print(answer2);

You haven't told us how you wired the pushbuttons. Without knowing that, we can't be sure that a digitalRead will ever return a high.

You said that the Buttons weren't being read. That is actually probably not true. The digitalReads at the top of your Loop are at least reading them once. The code I provided you is trying to read them constantly. You may very well not be getting the results you expect, but the Buttons are probably being read. That means there is a logic error somewhere and that we Need to find. Providing the new program, the wiring diagram, and the Output from the Serial Monitor will go a Long way towards achieving that Goal.