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

Click For Summary
SUMMARY

The discussion revolves around creating a simple Odd/Even game in C for an introductory programming class aimed at Engineering majors. The game requires users to input three numbers, calculates their sum, and determines if the sum is even or odd, repeating this process three times. The initial implementation failed to reset the counter and sum after each game iteration, leading to incorrect output. After suggestions from fellow programmers, the user successfully modified the code to reset these variables, resulting in a functioning game.

PREREQUISITES
  • Basic understanding of C programming syntax
  • Familiarity with control flow statements: "if", "while", and "do while"
  • Knowledge of variable declaration and initialization in C
  • Experience with input/output functions in C, specifically printf and scanf
NEXT STEPS
  • Explore advanced control flow techniques in C, such as "switch" statements
  • Learn about debugging techniques in C to identify and fix logical errors
  • Investigate how to modularize code by creating functions for repeated tasks
  • Study best practices for commenting and documenting code for better readability
USEFUL FOR

This discussion is beneficial for beginner programmers, particularly Engineering majors learning C, as well as educators looking for practical examples of control flow and game logic in programming.

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

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
6K
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K