C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-01-2008, 08:02 AM   #1
Registered User
 
Join Date: Sep 2008
Posts: 35
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
crazychile is offline   Reply With Quote
Old 11-01-2008, 08:27 AM   #2
Chinese pâté
 
foxman's Avatar
 
Join Date: Jul 2007
Location: Canada
Posts: 406
Code:
if (answer == 'y'); 
Here's the error.
__________________
I hate real numbers.
foxman is offline   Reply With Quote
Old 11-01-2008, 08:37 AM   #3
Registered User
 
Join Date: Sep 2008
Posts: 35
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
crazychile is offline   Reply With Quote
Old 11-01-2008, 10:25 AM   #4
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-01-2008, 11:24 AM   #5
Registered User
 
Join Date: Sep 2008
Posts: 35
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
crazychile is offline   Reply With Quote
Old 11-01-2008, 11:33 AM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-01-2008, 12:01 PM   #7
Registered User
 
Join Date: Sep 2008
Posts: 35
Wow. Thanks!

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

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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