Engineering Major? Test Your Programming Skills in this Odd/Even Game!

AI Thread Summary
The discussion revolves around creating a simple game for an introductory programming class, where players enter three numbers to determine if their sum is even or odd. The initial code successfully prompts for numbers but incorrectly repeats the win/loss message three times without resetting the counters. The user sought assistance to ensure the game plays correctly three times before displaying the final score. After feedback, it was identified that resetting the counter and sum variables was necessary for proper functionality. The program is now working as intended, with plans for further code refinement.
Vagabond7
Messages
50
Reaction score
11

Homework Statement


This is an introductory programming class for Engineering majors.

So I am making a simple game. It requires that you enter say, 3 numbers, then test to see if the sum of the numbers is even or odd. If it is even you win, odd, you lose. The game repeats several times, and at the end it tells you how many times you won. That is the goal.

Homework Equations



I can use "If Else" "while" and "do while" repetition statements to construct this game, but I could potentially use any kind of switch or break statement if somebody thinks that is useful.

The Attempt at a Solution


Here is the program as it is right now

#include <stdio.h>
int main ()
{
unsigned int counter;
int Win;
int sum;
int number;
unsigned int counterB; // variables I am using

Win = 0;
sum = 0;
counter = 1;
counterB = 1; // initializing counters and setting initial values

do { // unsucessful do/while statement to run game multiple times
while ( counter <= 3 ) { // part of the game that works
printf( "Enter A Number" );
scanf( "%d", &number );
sum = sum + number;
counter = counter + 1;
}
if ( sum % 2 == 0 ) {
printf ( "You win!" );
Win = Win + 1;
}
else {
printf ("You lose!" ); //end of the part of the game that works
}
} while ( ++counterB <= 3 );
printf ( "You won this many times : %d", Win); // the end condition informing you of your meaningless victories
getch ();
return 0;
} //program ending


When you run the program, it asks you for the numbers and plays the game correctly. But at the end is just says "you win!" or "You lose!" 3 times and then tells you that you won or lost 3 times. I built the game part itself first, and it works fine. The "do while statement" however doesn't do as intended.

I've tried different ways of doing it, but none of them get it to play the game from the beginning successfully 3 times, then print your score. Any programmers that can help me out?

Also, I do intend to clean up the code and have more notes once it works, this is just the rough draft.
 
Physics news on Phys.org


You aren't resetting "counter" after the first win/loss.
 


Awesome sauce! That did it. I also needed to reset the sum as well. Thank you very much for your insight! Program is working properly now.
 

Similar threads

Back
Top