Either my loop is bad or my function is messed up

This is a discussion on Either my loop is bad or my function is messed up within the C Programming forums, part of the General Programming Boards category; In my program I call this function: Code: int turnAgain(int status) // function: turnAgain // pre: Player must have rolled ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    Either my loop is bad or my function is messed up

    In my program I call this function:
    Code:
    int turnAgain(int status)
    // function: turnAgain
    // pre: Player must have rolled a non disqualifying point combination 
    // post: returns a valid (y or n) answer
    {
    	char ans;
    	
    	if (status == 1)
    	{
    		printf("TURNAGAIN Would you like to risk the points for this round and continue? (y or n)? ");
    		getchar();
    		ans = getchar();
    	} //while (tolower(ans) != 'y' && tolower(ans) != 'n');
    	return tolower(ans);
    }
    after it completes determining the points and status for the roll(this is in the main):

    Code:
          {
    		    die1 = rollDie();
    		    die2 = rollDie();
    		    sum  = (die1 + die2);
                        printf("Die 1 is a %d.\n", die1);
    		    printf("Die 2 is a %d.\n", die2);
          
    		    //award points (rolled a double but not double 1's)
                       if (turnPoints(die1, die2) == doublesPoints)
                      {
    			    do stuff
    			    status = 1;
                      }
    		 
    		    //award points (rolled double 1's)
    		    else if (turnPoints(die1, die2)  == onesPoints)
                     {
    			    do stuff
    			    status = 1;
                     }
    		 
    		    //award  no points (one of the two dice = 1)
    		    else if (turnPoints(die1, die2) == noPoints)
                     {
    			    do suff
    			    status = 0;
                     }
    		 
    		    //award points (rolled singles)
    		    else if (turnPoints(die1, die2) == singlesPoints)
                    {
    			    do stuff
    			    status = 1;
                    }
    			
    			 if (turnAgain(status) == 'y');
    	}//end loop	 
    		 		 
    		 //total the points for the player and switch to other player
    		 if (turnAgain(status) == 'n');
    		 {
    			 total = (tempTotal + total);
    			 if (currentPlayer == 1)
    			 {
    			    p1Total = updatePlayerTotal(currentPlayer, tempTotal,
    			    p1Total);
    			 }
    			 else
    			 {
    			    p2Total = updatePlayerTotal(currentPlayer, tempTotal,
    			    p2Total);
    			 }
    			 currentPlayer = switchPlayer(currentPlayer); 
    		 }
    The code currently executes the loop once. When the function turnAgain is called, it will ask if you want to continue, but if you enter 'y', it will just keep asking until you enter 'n'. Then the code for (turnAgain(status) == 'n') prints like it should. I need it to keep looping back up to roll the dice again, etc. until a status = 0 condition happens or until I tell it to stop. Do I need another set of {} somewhere, or is my function faulty? What have I done wrong here?

    Thanks a bunch,
    crazychile

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Quote Originally Posted by crazychile View Post
    Code:
          {
    		    die1 = rollDie();
    		    die2 = rollDie();
    		    sum  = (die1 + die2);
                        printf("Die 1 is a %d.\n", die1);
    		    printf("Die 2 is a %d.\n", die2);
          
    		    //award points (rolled a double but not double 1's)
                       if (turnPoints(die1, die2) == doublesPoints)
                      {
    			    do stuff
    			    status = 1;
                      }
    		 
    		    //award points (rolled double 1's)
    		    else if (turnPoints(die1, die2)  == onesPoints)
                     {
    			    do stuff
    			    status = 1;
                     }
    		 
    		    //award  no points (one of the two dice = 1)
    		    else if (turnPoints(die1, die2) == noPoints)
                     {
    			    do suff
    			    status = 0;
                     }
    		 
    		    //award points (rolled singles)
    		    else if (turnPoints(die1, die2) == singlesPoints)
                    {
    			    do stuff
    			    status = 1;
                    }
    			
    			 ... Makes no sense...
    			 if (turnAgain(status) == 'y');
    	}//end loop	 
    		 		 
    		 //total the points for the player and switch to other player
    		 if (turnAgain(status) == 'n'); // Oops
    		 {
    			 total = (tempTotal + total);
    			 if (currentPlayer == 1)
    			 {
    			    p1Total = updatePlayerTotal(currentPlayer, tempTotal,
    			    p1Total);
    			 }
    			 else
    			 {
    			    p2Total = updatePlayerTotal(currentPlayer, tempTotal,
    			    p2Total);
    			 }
    			 currentPlayer = switchPlayer(currentPlayer); 
    		 }
    See comments.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    After trying some combinations...

    I'm guessing where you're going with this is I need to do an if else. But then how do I make it go back to the top of the loop for a y answer?

    thanks,
    crazychile

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No, where we're going with this is to please don't put a semicolon after your if statement, just like the other threads that you've posted.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    And you have an if statement, but nothing to do if the condition is true... Weird, if you ask me.
    If the water is boiling then... do nothing?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    ok the ; is a bad habit. I've done it in the past so that the file compiles. I'm starting to learn that if it doesnt compile that it might mean that something else needs to be fixed first.

    I don't know what to put after the if statement to make it go back up to the top of the loop. However, when I do this:

    Code:
    while (turnAgain(status) != 'n')	 
    		 {
    		    die1 = rollDie();
    ....remainder of the loop
    it has the effect of making it go through the loop again (I have to comment out the remaining code in the file to make it do this at this point) but I'm wondering if the if/else at the bottom of the loop would be better.

    This is the basic outline of what i want to happen when I finally finish this:

    1. roll dice and accumulate points until:
    a. you roll a combination that eliminates your points
    b. you chose to quit and total your score for the round

    2. if a or b happen, it switches to the other player after tallying the points (if there are any)
    3. the other player does the same thing until a or b happen
    4. this goes on until someone has enough points to win. (you have to decide to stop before this condition is checked)
    5. Then it announces the winner and spews out the totals for each player

    So would an if else at the bottom of that loop be the best way to do it, and if so, how should I code it to make it return to the top of the loop?

    Thanks everyone.
    crazychile

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Why not make a flowchart first? That way, you can write down what happens and where, to get an overview of how the logic is supposed to be done.
    Then you choose an appropriate loop depending on how your flowchart looks like and translate it to code.
    It simply cannot get any easier.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    By "at the bottom of the loop", do you mean you want something like this?
    Code:
    do {
      // roll the die and stuff
    } while (!status && turnagain(status) == 'y')
    Why not do so?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Ah, why do people spoil solutions all the time?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Quote Originally Posted by tabstop View Post
    By "at the bottom of the loop", do you mean you want something like this?
    Code:
    do {
      // roll the die and stuff
    } while (!status  && turnagain(status) == 'y')
    Why not do so?
    Thank you. That is the general idea of what I'm trying to do. But when I put the while loop at that location it wont compile.

    pig_v6.c:209: error: parse error before '}' token
    pig_v6.c:251: error: parse error before 'return'

    I've moved the statement to other locations (same with the "do") and get similar results. here is the current structure of my main:

    Code:
    int main (void)
    {
       // variable declarations
    
    
       // do // play at least one game I started with the do statement here
       {
    		 //give option to print the rules only at the beginning of the game
    		 
    	 do I just moved the do statement here, not sure if this iscorrect
          {
    		    die1 = rollDie();
    		    die2 = rollDie();
    		    sum  = (die1 + die2);
                        printf("Die 1 is a %d.\n", die1);
    		    printf("Die 2 is a %d.\n", die2);
          
    		   
              if (turnPoints(die1, die2) == doublesPoints)
              {
    			    do stuff
              }
    		 
    	  else if (turnPoints(die1, die2)  == onesPoints)
              {
    			    do stuff
              }
    		 
              else if (turnPoints(die1, die2) == noPoints)
              {
    			    do stuff and dont satisfy the condition to loop again 
              }
    		 
              else if (turnPoints(die1, die2) == singlesPoints)
              {
    			    do stuff
              }while (status != 0 && turnAgain(status) == 'y')
    			
          }it doesnt like the while statement here either
             //this is line 209
    	 if (turnAgain(status) == 'n')//(answer == 'n')
    	 {
    		 do stuff 
    	 }
    		 //a bunch of stuff i commented out for now
    
       } while (answer != 'n')
    
       return 0;
    }// this is line 251
    I know you guys are frustrated with me and I appreciate your help a lot. I do have a flow diagram for this but I still have a very hard time with this. any additional help would be very much appreciated.
    crazychile

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Then what does your flowchart look like?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    for the main it looks like this:
    Code:
    {
       variable declarations
       
       give option to print rules
    
       do
        {
           {    
             roll dice
             award points per roll combination until condition is met
           }
           print the totals so far
           switch player and repeat loop
    
           if either player reaches 100 points
           {
             announce the winner and print totals
           }
           ask to play another game
           if yes then repeat do
        }
    }
    The design has been refined a bit as I went along. My instructor has made us turn in design documents that are generic in nature but not actual flow charts per se.

    Thank you,
    crazychile

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    do / while loops need to have semicolons after them.

    Code:
    do {
    
    } while (condition);

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    You can see that the logic has flaws.
    Above all else, replace your "...until condition is met" and "...and repeat loop" (etc) with real loops and conditions.
    Like
    BEGIN LOOP
    Roll dice
    Award points per roll combination
    If points == something REPEAT LOOP
    END LOOP
    But it really should be written on paper so you can see if a certain condition is false or true, it repeats, and you can see that by tracing the pointer that is written to it.

    And then it seems fishy that you would ask if the user wants to play again when a game is not finished (the question is outside the if).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Quote Originally Posted by Elysia View Post
    You can see that the logic has flaws.
    Above all else, replace your "...until condition is met" and "...and repeat loop" (etc) with real loops and conditions.
    Like
    BEGIN LOOP
    Roll dice
    Award points per roll combination
    If points == something REPEAT LOOP
    END LOOP
    But it really should be written on paper so you can see if a certain condition is false or true, it repeats, and you can see that by tracing the pointer that is written to it.

    And then it seems fishy that you would ask if the user wants to play again when a game is not finished (the question is outside the if).
    Thank you Elysia. I'm going to scratch out another version now. I get messed up with how to start and close loops. What is the rule for DO? I want it to do the main from the rules onward but I also dont want it to repeat the rules every round. What are the rules for starting and closing a DO?

    thank you,
    crazychile

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function (modules) and loop help.
    By GCNDoug in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 12:43 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21