Function Problem

My function can't execute properly.

Page 1 of 1

5 Replies - 5373 Views - Last Post: 26 March 2006 - 09:27 PM Rate Topic: -----

#1 lahuerita2004   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-February 06

Function Problem

Posted 26 March 2006 - 08:39 PM

I have a problem with this casino game. My function for craps doesn't execute properly. The craps game has



1) Roll a pair of fair six-sided dice.
2) If you roll a 7 or 11, you win!
3) If you roll a 2, 3, or 12, you lose.
4) Otherwise, record what you've rolled. Let this sum be k; also known as your point.
5) If you rolled a point, continue rolling the pair of dice until you get either your point (k) or a sum of seven on the two dice.
6) If k comes up first, you win!
7) If 7 comes up first, you lose.

can anyone tell me what i did wrong?


   
 #include <stdio.h>
 #include <time.h>
 #include <stdlib.h>    
 
 
 #define CHIP_BUYING 11     //Cost to buy chips.
 
 #define CHIP_SELLING 10    //Cost to sell chips.
 
 
 //Prints Main menu.
 
 int menu ();
 
 //Generates two random numbers in between 1 and 6 inclusive and returns their sum.
 
 int pairofdice ();
 
 //Plays the game of Craps
 
 int craps();
 
 //Plays the game of Arup's dice.
 
 int arupsdice();
 
 //Buys chips from the user during the game.
 
 int buyschips(int *cash_ptr);
 
 //Allows user to sell chips back to the casino.
 
 int sellchips(int numchips);
 
 //Prints out a report detailing the number of chips and the amount of cash the user has.
 
 void statusreport(int numchips, int cash);
 
 int main (void)  
 {
  
   
  //Declare variables. 
  
  int choice, start_cash = 1000;    //Choice of user and cash that user is offered to play by casino. 
  
  int numchips = 0;                // Number of chips that user will have during casino game. 
  
  int cash;
  
  int cash_ptr;                    //Pointer to cash that holds amount of money user wants to spend. 
   
  int cash_amount = 0;            // Cash obtained when chips are sold.

  int bet_chips= 0;               // How many chips user wants to bet. 

  int game;                       // Begins the casino game selected;
  
  int PLAYER_WON, PLAYER_LOST;
  
  // Seed the random number generator.
  srand(time(0));
  
  cash = start_cash;
  
  // Offer menu to user and store choice.

  choice = menu ();
   
 // Loop until the user chooses to quit. The loop is broken once user chooses choice 6 to quit. 

 while (choice !=6) {

      if (choice == 1){
   
       	 numchips += buychips(&cash);
 	 
   	 choice = menu ();  
     
          }
  
      if (choice == 2){
  
    cash_amount = sellchips(numchips);
    
    numchips -= (cash_amount/CHIP_SELLING);
    
    cash += cash_amount;
    
    choice = menu ();

      }
  
    
     if (choice == 3){

         printf("How many chips would you like to bet?\n\n");
         scanf("%d", &bet_chips);

         //Nullify transaction if no chips are bet.

         if ( bet_chips == 0 || bet_chips > numchips)
 
         printf("Sorry that is not allowed. No game played.\n\n");

         // Else execute transaction.

          else {
            
             game = craps();
 
             if (game == PLAYER_WON){
              
                    printf("You win!");
                    
                    numchips += bet_chips;
   	 }
                else{ 
                   
                   printf("Sorry, you have lost!\n");
           
                   numchips -= bet_chips;
   	 }
          
  //If player doesn't have enough chips or money then program will end.   
      if ( numchips <= 0 && cash <= 0){
  
      printf("You can not play anymore.  Not enough chips and cash.\n");
   	 
       choice = 6;
    
     }
           }

            
      choice = menu (); 
           
         }
  
  
  if (choice == 4){
   
         printf("How many chips would you like to bet?\n\n");
         scanf("%d", &bet_chips);

         //Nullify transaction if no chips are bet.

         if ( bet_chips > numchips)
 
   	 printf("Sorry that is not allowed. No game played.\n\n");

         // Else execute transaction.

          else {
            
             game = arupsdice();
 
             if ( game == PLAYER_WON){
              
                    printf("You win!");
                    
                    numchips += bet_chips;

                }
                else{ 
                   
                   printf("Sorry, you have lost!\n\n");
           
                   numchips -= bet_chips;

                }
            //If player doesn't have enough chips or money then program will end.   
    if ( numchips <= 0 && cash <= 0){
    
         printf("You can not play anymore.  Not enough chips and cash.\n\n");
   	 
   	 choice = 6;
    
     }

            }

            
      choice = menu (); 
           

  }
  
  if (choice == 5) {
  
     statusreport( numchips, cash);
  
     choice = menu ();
  
  }
   
	

   }
   
	cash = cash + (numchips * CHIP_SELLING);
  
	printf("After selling your chips, you have $%d.  Thanks for playing!\n", cash);
	

 return 0;
}  //Ends the main function.

 
//Pre-conditions: none
 //Post-conditions: Main menu will be printed and a valid choice answer
 // (1 thru 6) will be returned.

int menu()
{
int answer = 0;

//Menu will loop until valid answer is given.

while ( answer != 1 && answer != 2 && answer != 3 && answer !=4 && answer !=5 && answer !=6)
{

    printf("Welcome to the Casino. Here are your choices:\n");
    printf("1: Buy chips.\n");
    printf("2: Sell chips.\n");
    printf("3: Play Craps.\n");
    printf("4: Play Arup's Game of Dice.\n");
    printf("5: Status Report\n");
    printf("6: Quit\n");

    scanf ("%d", &answer);

 }

return answer;
}

// Precondition: None.
// Postcondition: Generates two random numbers in between 
//                one and six inclusive and returns their sum.
int pairofdice(){
     
   //Declare local variables.
 
   int rand1, rand2, sum;


  //Start the rolling process of the two dice.

   rand1 = 1 + rand ()%6;

   rand2 = 1 + rand ()%6;

   sum = rand1 + rand2;

 
   return sum;

}

// Precondition: None.
// Postcondition: Plays one game of Craps and returns 
//                PLAYER_WON if the player won and 
//                PLAYER_LOST if they lost.

int craps(){

 //declare variables

 int  answer, point, k, PLAYER_WON, PLAYER_LOST;

 
 int temp_roll;                     //temporary roll that will execute 2nd roll.

 
//Execute the first roll
  
  printf("Press 'r' and hit enter for your first roll.\n");
  scanf("%d", answer);
  getchar();                      // This will get the user's answer.

  
  point = pairofdice();


  if (point == 7 || point == 11){
    
     printf("You rolled %d.\n", point);
   
     return PLAYER_WON;
   }

  else if (point == 2 || point == 3 || point == 12){
     
     printf("You rolled %d.\n", point);
     
     return PLAYER_LOST;
    
   }

  else {
   
      printf("You rolled %d.\n", point);

      k = point;
      
      //Execute second roll of dice
      
      printf("Press 'r' and hit enter for your next roll.\n");
      scanf("%d", answer);
      getchar();                      // This will get the user's answer.
     
      
  temp_roll = pairofdice();
    
      while (temp_roll != k || temp_roll != 7){
   
    temp_roll = pairofdice();
             
    if(temp_roll == k){
                
                printf("You rolled %d.\n", point);
                
                return PLAYER_WON;
            }
            
            else if (temp_roll == 7){
             
                printf("You rolled %d.\n", point);  
                   
                return PLAYER_LOST;
            }
        }

      } 
   }


// Precondition: None.
// Postcondition: Plays one game of Arup's game of dice and
//                returns PLAYER_WON if the player won and 
//                returns PLAYER+LOST if they lost.
int arupsdice(){
 
 //declare variables

 int  answer, roll1, k, PLAYER_WON, PLAYER_LOST;

 
 int temp_roll;                     //temporary roll that will execute 2nd roll.

 
//Execute the first roll
  
  printf("Press 'r' and hit enter for your first roll.\n");
  scanf("%d", answer);
  getchar();                      // This will get the user's answer.

  
  roll1 = pairofdice();


  if (roll1 == 11 || roll1 == 12){
    
     printf("You rolled %d.\n", roll1);
   
     return PLAYER_WON;
   }

  else if (roll1 == 2){
     
     printf("You rolled %d.\n", roll1);
     
     return PLAYER_LOST;
    
   }

  else {
   
      printf("You rolled %d.\n", roll1);

      k = roll1;
      
      //Execute second roll of dice
      
      printf("Press 'r' and hit enter for your next roll.\n");
      scanf("%d", answer);
      getchar();                      // This will get the user's answer.
     
      
      while (temp_roll != k){
  
  temp_roll = pairofdice();

            if(temp_roll > k){
                
                printf("You rolled %d.\n", roll1);
                
                return PLAYER_WON;
            }
            
            else if (temp_roll <=k){
             
                printf("You rolled %d.\n", roll1);  
                   
                return PLAYER_LOST;
            }
        }

      } 
}
 

// Precondition: cash is the address of the variable 
//               storing the amount of money the user  
//               wants to spend on chips.
// Postcondition: The number of chips purchased is returned
//                and the variable storing the amount of 
//                money the user paid for chips is adjusted
//                to equal the change left over after the
//                transaction.

int buychips( int *cash_ptr){
    
	
   int buy_cash, temp_chips;
  
   printf("How much cash do you want to spend for chips?\n");
   scanf("%d", &buy_cash);  
	printf("\n");
	
	
	//Nullify transaction if the buyer wants to spend more money than allowed.
  
   if( buy_cash > *cash_ptr)
    
      printf("Sorry, you do not have that much money. No chips bought.\n\n");
  
   //Execute transaction.
  
   else{ 
  
      temp_chips = (buy_cash / CHIP_BUYING);
 
      *cash_ptr -= buy_cash;

  }
 
 return temp_chips;
    
 }

// Preconditions: numchips > 0.
// Postconditions: Returns the cash obtained for selling 
//                 numchips number of chips.

int sellchips(int numchips){


 int sell_chips;            // Amount of chips being sold. 
 int cashobtained;          //Amount of cash obtained after sell of chips.
  
  printf("How many chips do you want to sell?\n");
  scanf("%d", &sell_chips);
  printf("\n");
  

       //Nullify transaction if user doesn't enters wrong amount of chips.
 
        if ( sell_chips > numchips)

           printf("Sorry, you do not have that many chips. No chips sold.\n\n");
   
        //Execute transaction

        else 
     
           cashobtained= numchips * CHIP_SELLING;
  
  return cashobtained;
  
}

// Precondition: The first parameter is the number of 
//               chips the user has, the second is how
//               much cash they currently have.
// Postcondition: A report detailing the number of chips 
//                and the amount of cash the user has is
//                printed.

void statusreport(int numchips, int cash){

  printf("You currently have $%d left and %d chips.\n", cash, numchips);
  
 }





Is This A Good Question/Topic? 0
  • +

Replies To: Function Problem

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Function Problem

Posted 26 March 2006 - 08:49 PM

Can you be a little more specific about the problem? What is going on with the function that it does not work? Right off the bat, it looks as if you have declared two local variables (PLAYER_WON and PLAYER_LOST), and are using those as your return values, but you do not seem to assign them values at any point.
Was This Post Helpful? 0
  • +
  • -

#3 lahuerita2004   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-February 06

Re: Function Problem

Posted 26 March 2006 - 08:57 PM

oh i see what you mean.... i will fix that. My problem is that my function for the craps game doesn't really execute properly on the else statement.. I think that i might have wrote the while statement wrong for step 5 of my craps game.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Function Problem

Posted 26 March 2006 - 09:00 PM

Try changing this:
(temp_roll != k || temp_roll != 7)


to this
(temp_roll != k && temp_roll != 7)


You'll also have to move the checks for 7 and k outside the while loop, or change the while loop condition.
Was This Post Helpful? 0
  • +
  • -

#5 lahuerita2004   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-February 06

Re: Function Problem

Posted 26 March 2006 - 09:09 PM

1) Roll a pair of fair six-sided dice.
2) If you roll a 7 or 11, you win!
3) If you roll a 2, 3, or 12, you lose.

4) Otherwise, record what you've rolled. Let this sum be k; also known as your point.
5) If you rolled a point, continue rolling the pair of dice until you get either your point (k) or a sum of seven on the two dice.
6) If k comes up first, you win!
7) If 7 comes up first, you lose.


the selected part is the one that i am having problems with. my out come is coming out wrong .
Was This Post Helpful? 0
  • +
  • -

#6 lahuerita2004   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-February 06

Re: Function Problem

Posted 26 March 2006 - 09:27 PM

i took out the temp_roll before the while and kept the change you suggested...seems to be working now..


Thanks so much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1