1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming for Engineers Help. having trouble with Nested repetition statement.

  1. Oct 11, 2012 #1
    1. The problem statement, all variables and given/known data
    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.

    2. Relevant 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.

    3. 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.
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Oct 11, 2012 #2

    jbunniii

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Re: C programming for Engineers Help. having trouble with Nested repetition statement

    You aren't resetting "counter" after the first win/loss.
     
  4. Oct 11, 2012 #3
    Re: C programming for Engineers Help. having trouble with Nested repetition statement

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C programming for Engineers Help. having trouble with Nested repetition statement.
  1. C++ program help! (Replies: 1)

  2. C program help needed (Replies: 3)

  3. Having trouble with c (Replies: 2)

Loading...