# Thread: Either my loop is bad or my function is messed up

1. ## 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');
}```
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. Originally Posted by crazychile
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);
}```

3. 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. 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. 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?

6. 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. 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.

8. 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. Ah, why do people spoil solutions all the time?

10. Originally Posted by tabstop
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

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. Then what does your flowchart look like?

12. 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
}
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. do / while loops need to have semicolons after them.

Code:
```do {

} while (condition);```

14. 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).

15. Originally Posted by Elysia
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