# Why do the rules for my game print when I tell them not to?

• 11-01-2008
crazychile
Why do the rules for my game print when I tell them not to?
My code uses the following function:
Code:

```// function: prn_rules // pre: none // post: prints the rules void prnRules(void) {         printf("%s\n",                         "The goal is to be the first player to reach 100 points.\n\n"                         "On a turn, a player rolls the two dice repeatedly until either a 1 is rolled \n"                         "or the player chooses to hold and stop rolling.\n\n"                         "If a 1 is rolled, that player's turn ends and no points are earned for the round.\n"                         "If the player chooses to hold, all of the points rolled during that round are added to their score.\n\n"                         "If a player rolls double 1s, that counts as 25 points.\n"                         "Other doubles are worth 2x double points, so that a 2-2 is worth 8 points; 3-3 is worth 12; \n"                         "4-4 is worth 16; 5-5 is worth 20; and 6-6 is worth 24.\n\n"                         "When a player reaches a total of 100 or more points, the game ends and that player is the winner.\n"); }```
Here is the section of my main that calls the function:
Code:

```int main (void) {   // variable declarations   int sum;                        // for sum of two dice   char answer;                    // for yes/no questions   int tempTotal = 0;   int p1Total;                    //Running total for player 1   int p2Total;                    //Running total for player 2   int total = 0;                    //total before assigning to player 1 or 2         int die1 = 0;   int die2 = 0;   int currentPlayer = 1;        // Start with Player 1     srand(time(NULL));            // seed random # generator     do // play at least one game     {                 //give option to view the rules                 printf("Welcome to the game of Pig. Would you like to view the rules? (y or n)?\n");                 answer = getchar();                 getchar();                 if (answer == 'y');       {                 prnRules();                 }```
That isn't the complete main, but just the first part that calls prnRules. The problem is, if I answer "n" to wanting to view the rules, it still prints them every time. What am I doing wrong?

Thank you,
crazychile
• 11-01-2008
foxman
Code:

`if (answer == 'y');`
Here's the error.
• 11-01-2008
crazychile
Thanks foxman!

It's not the first time I've done that. It might be time I made a post-it on that for the wall of shame.

crazychile
• 11-01-2008
Elysia
Have you turned up your compiler warnings to max? Many compilers would warn about that.
And your indentation is pretty poor, as well. You might want to fix that.
• 11-01-2008
crazychile
I know my indentation is messed up and I need to fix it. It looks fine on my screen but does strange stuff when I copy paste to the forum.

How do I set my compiler warnings to max?
I'm on a Mac running Xcode, that uses gcc.

Thanks for the tip.
crazychile
• 11-01-2008
laserlight
Quote:

Originally Posted by crazychile
I know my indentation is messed up and I need to fix it. It looks fine on my screen but does strange stuff when I copy paste to the forum.

You are probably mixing spaces and tabs. Stick to either one of them.

Quote:

Originally Posted by crazychile
How do I set my compiler warnings to max?
I'm on a Mac running Xcode, that uses gcc.

I do not know how to do that within Xcode, but for the command line warning options for gcc check the manual on Options to Request or Suppress Warnings. I typically specify the -Wall option.
• 11-01-2008
crazychile
Wow. Thanks!

It appears that all I need to do in the terminal window on a Mac is: gcc filename -Wall