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

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

    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

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    if (answer == 'y');
    Here's the error.
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Wow. Thanks!

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. Engine <=> DX/OGL | c++ ?
    By darkcloud in forum Game Programming
    Replies: 6
    Last Post: 05-13-2005, 12:19 AM
  5. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM