Thread: compilation problem related to a function - any suggestions

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

    compilation problem related to a function - any suggestions

    I get the following compiler errors when I try to compile my program:

    pig_v5.c: In function 'main':
    pig_v5.c:190: error: parse error before '{' token
    pig_v5.c:195: error: parse error before 'answer'
    pig_v5.c:196: warning: comparison between pointer and integer
    pig_v5.c: At top level:
    pig_v5.c:228: error: parse error before 'while'

    When I comment out the code around 195 and 196, the problem "moves" to other parts in the main where I use "answer". Something is messed up with that function and I cant see it.

    My code is below with line 195 denoted in the comments. Can anyone help me with this so I can get this to compile?

    Thanks in advance,
    Code:
    #include <stdio.h>   // for interactive input and output
    #include <stdlib.h>  // for the rand() function
    #include <ctype.h>   // for tolower() macro
    #define SIDES 6
    
    typedef enum {doublesPoints, onesPoints, noPoints, singlesPoints} status;
    
    
    //the rules of the game
    void prn_rules(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");
    }		
    
    
    int rollDie ()
    // function: rollDie
    // pre: none
    // post: generates a random number for the die 
    {
       int d;
    
       d = rand() % SIDES + 1;
       return d;
    }
    
    
    status turnPoints(int die1, int die2)
    // function: turnStatus
    // pre: 
    // post: returns the status of the roll (doublesPoints, onesPoints, no Points, singlesPoints)
    {
       if (die1 == die2 && !(die1 == 1 && die2 == 1))
    		return doublesPoints;
       
    	else if (die1 == 1 && die2 == 1)
    		return onesPoints;
    		
    	else if ((die1 == 1 || die2 == 1) && !(die1 == 1 && die2 == 1))
    		return noPoints;
    	
    	else
    		return singlesPoints;
    }
    
    char turnAgain()
    // function: turnAgain
    // pre: none
    // post: returns a valid (y or n) answer
    {
    	char ans;
    	
    	do
    	{
    		printf("Would you like to risk the points for this round and continue? (y or n)? ");
    		ans = getchar();
    		getchar();
    	} while (tolower(ans) != 'y' && tolower(ans) != 'n');
    	return tolower(ans);
    }
    
    //to switch the current player when the turn is over
    int switchPlayer(int currentPlayer)
    {
    	if (currentPlayer == 1)
    		return 2;
    	else
    		return 1;
    }
    
    
    char playAgain(int choice)
    // function: playAgain
    // pre: none
    // post: returns a valid (y or n) answer
    {
        char ans;
    
        do
        {
           if (choice == 1)
             printf("Play another game (y or n)? ");
           if (choice == 2)
             printf("Do you want to risk it all and roll again (y or n)? ");
           ans = getchar();
           getchar();
        } while (tolower(ans) != 'y' && tolower(ans) != 'n');
        return tolower(ans);
    }
    
    //to determine if the game is over
    int gameOver (int p1Total, int p2Total)
    {
    	if (p1Total || p2Total >= 100)
    	{
    		return 1;
    	}
    	else
    	{
    		return 0;
    	}
    }
    
    //The final print statement with all the totals
    void finalOutput(int p1Total, int p2Total)
    {
    	printf("\n");
    	printf("WE HAVE A WINNER!!!\n");
    	printf("Player 1 your final score was %d\n", p1Total);
    	printf("Player 2 your final score was %d\n", p2Total);
    }
    
    int updatePlayerTotal(int currentPlayer, int turnTotal, int playerTotal)
    {
    
       printf("Player %d, your total at the start of this turn was %d .\n",
       currentPlayer, playerTotal);
       playerTotal = playerTotal + turnTotal;
       printf("Your total at the end of this turn is %d ,\n", playerTotal);
       return playerTotal;
    }
    
    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
    		 prnRules();
           //roll and show results
    		 makePlay();
    		 die1 = rollDie();
    		 die2 = rollDie();
           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)
           {
    			 printf(" You rolled a double %d!\n ", die1);
    			 printf(" That's worth %d points.\n", (sum * 2));
    			 tempTotal = (tempTotal + total + (sum * 2));
           }
    		 
    		 //award points (rolled double 1's)
    		 else if (turnPoints(die1, die2) == onesPoints)
           {
    			 printf(" You rolled a double 1!\n ");
    			 printf(" That's worth 25 points.\n");
    			 tempTotal = (tempTotal + total + 25);
           }
    		 
    		 //award  no points (one of the two dice = 1)
    		 else if (turnPoints(die1, die2) == noPoints)
           {
    			 printf("Sorry, you rolled a single 1. You do not earn any points this round\n");
    			 printf("Your current total is %d\n", total);
    			 tempTotal = 0;
    			 total = total + tempTotal;
    			 switchPlayer; 
           }
    		 
    		 //award points (rolled singles)
    		 else (turnPoints(die1, die2) == singlesPoints)
           {
    			 printf("Your roll totals %d points.\n", sum);
    			 tempTotal = tempTotal + (total + sum);
           }
    		 
    		 answer = playAgain(2); //LINE 195
           while (turnAgain == 'y'); //LINE 196
           {
              // roll again
    			 makePlay();
              printf("Die 1 is a %d.\n", die1);
    			 printf("Die 2 is a %d.\n", die2);
           }
    		 
    		 //total the points for the player and switch to other player
    		 if (answer == 'n')
    		 {
    			 total = (tempTotal + total);
    			 if (currentPlayer == 1)
    			 {
    			    p1Total = updatePlayerTotal(currentPlayer, tempTotal,
    			    p1Total);
    			 }
    			 else
    			 {
    			    p2Total = updatePlayerTotal(currentPlayer, tempTotal,
    			    p2Total);
    			 }
    			 // return p1Total, p2Total;
    			 currentPlayer = switchPlayer(currentPlayer); 
    		 }
    		  		 
    		 if (gameOver(p1Total, p2Total) == 1) 
    			 finalOutput (p1Total, p2Total);
    
           // see if they want to play another game
           answer = playAgain(1);
    
       } while (answer != 'n');
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Code:
    		 answer = playAgain(2); //LINE 195
           while (turnAgain == 'y'); //LINE 196
           {
              // roll again
    			 makePlay();
              printf("Die 1 is a &#37;d.\n", die1);
    			 printf("Die 2 is a %d.\n", die2);
           }
    You don't want that semicolon.

    Also, don't mix spaces and tabs, or else velociraptors will eat you.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Thank you. But removing the semi colon didn't change anything. Same errors.

    My instructor just warned me about velociraptors. I need to redo my spacing. (it looks fine until I post it here)

    Because of the error I get on line 196, it makes me thing that there might be something up with the playAgain and/or turnAgain functions near the top of the program. But I'm not able to tell what it is. Any other suggestions?

    Thanks for the help,
    crazychile

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What happened to looking at the first line with an error?
    Code:
    else (turnPoints(die1, die2) == singlesPoints)
    Why do you think that makes sense?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Quote Originally Posted by tabstop View Post
    What happened to looking at the first line with an error?
    Code:
    else (turnPoints(die1, die2) == singlesPoints)
    Why do you think that makes sense?
    Ok. I'm not sure that it does make sense. But the compiler didn't yell at me when I tried the same thing on line 179 (the else if ), so I didn't look at it too critically (yet). This is my first program using functions. What is wrong with what I did?

    Thank you,
    crazychile

  6. #6
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Quote Originally Posted by crazychile View Post
    Ok. I'm not sure that it does make sense. But the compiler didn't yell at me when I tried the same thing on line 179 (the else if ), so I didn't look at it too critically (yet). This is my first program using functions. What is wrong with what I did?

    Thank you,
    crazychile
    This has nothing to do with functions other than the fact that you are getting return information from a function.

    Either you need to use an else if there like you said you did earlier. Or you need to rethink what you are trying to do there, because it doesn't make any sense.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by crazychile View Post
    Ok. I'm not sure that it does make sense. But the compiler didn't yell at me when I tried the same thing on line 179 (the else if ), so I didn't look at it too critically (yet). This is my first program using functions. What is wrong with what I did?

    Thank you,
    crazychile
    If it was the same thing as line 179, I wouldn't have pointed it out.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Thank you. I did not suspect the "else" would be a problem. That has taken care of most of the errors. I'll continue to try and work through the rest.

    Thanks again everyone,
    crazychile

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by crazychile View Post
    My instructor just warned me about velociraptors. I need to redo my spacing. (it looks fine until I post it here)
    It usually does when the IDE tends to indent with both spaces and tabs. If you would just use one, there would be no problems.
    Of course, there are certain rules that allows you to mix both and still make it look good, but let us not get into that. Keep it simple.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM