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

In summary, the conversation discusses a programming project in an introductory class for Engineering majors. The goal of the project is to create a simple game that involves entering numbers and determining if the sum of the numbers is even or odd. The conversation includes a code attempt and a discussion of troubleshooting the code to make it repeat the game multiple times and print the final score. The issue is resolved by resetting the "counter" and "sum" variables.
  • #1
Vagabond7
50
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
  • #2


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


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.
 

1. What is an engineering major?

An engineering major is a field of study that focuses on the application of scientific, mathematical, and practical knowledge to design, build, and maintain structures, machines, devices, systems, and processes.

2. What skills are necessary for an engineering major?

Some essential skills for an engineering major include critical thinking, problem-solving, analytical skills, creativity, teamwork, and strong mathematical and scientific abilities.

3. What programming languages are commonly used in engineering?

The most commonly used programming languages in engineering are C++, Java, Python, MATLAB, and FORTRAN. Different languages are used for different purposes, such as C++ for software development and MATLAB for data analysis and simulation.

4. What is the purpose of the Odd/Even Game in testing programming skills?

The Odd/Even Game is a simple coding challenge that tests a programmer's ability to use conditional statements and loops to solve a problem. It also assesses their understanding of basic programming concepts such as variables, data types, and logical operators.

5. How can one prepare for an engineering major and improve their programming skills?

To prepare for an engineering major and improve programming skills, one can take advanced math and science courses in high school, practice coding through online resources, participate in coding competitions, and seek internships or research opportunities in engineering-related fields.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
Back
Top