Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am working on this blackjack game.. now this code is not completed, but i am facing problem to call function inside another function ,for example how to use parametrs corectly.
for example: in chickcoputercards function i know the way i called chickcomputercards in incorrect .. how to do this the write way ?

my code is:
C#
the game is basicly, the computer gets two random cards , and a special card, if the two cards+special card <21 the computer gets extra cards untill the total sum is>21..

then the player gets two random cards , and he can chose if he can chose if he wants extra cards , at the end who has total sum that is closer to 21 but not bigger than 21 wins !

there is more rules , but now i am trieng to solve these problems first.. 


What I have tried:

C
#include <stdio.h>
#include <stdlib.h>

#define MIN_BET 1
#define INITIAL_MONEY 1000
/* put your #defines and typedefs here*/

int rand_range (int low, int high) // RAND_MAX assumed to be 32767
{
    static unsigned int next = 1;
    next = next * 1103515245 + 12345;
    return ((unsigned int)(next/65536) % 32768) % (high - low + 1) + low;
}

void srand_range (unsigned int seed)
{
    for (int i = 0; (unsigned int)i < seed; i++) {
        rand_range(i,i+1);
    }
}

int bett(int initialMoney)
  {
      int riskedMoney;
      printf("You currently have %d fake dollars. How much do you want to bet?\n",initialMoney );
      scanf("%d",&riskedMoney);
      while((riskedMoney>initialMoney)&&(riskedMoney<min_bet mode="hold" />      {
         printf("Your bet must be at least MIN_BET and at most %d, try again:\n",initialMoney );
         scanf("%d",&riskedMoney);
      }
      return(riskedMoney);
  }

int chosecomputercards(int first_card,int second_card)
{
    int sum=0;
    first_card=rand_range(1,13);
    second_card=rand_range(1,13);
    printf("The computer got %d and %y.",first_card,second_card);
    sum=first_card+second_card;
    return(sum);
}

int chickcomputercards(int sum,int special_card,int bet)
{
    int extra_card;
    sum=chosecomputercards(first_card,second_card);
    while((sum+special_card)<21)
    {
        extra_card=rand_range(1,13);
        if((sum+special_card+extra_card)<=21)
        {
           printf("Extra card:%d.",extra_card);
           sum+=extra_card;
        }
    }
    if((sum+special_card)>21)
    {
        printf("The computer got more than 21\n");
        printf("You have won %d fake dollars!\n",bet);
    }
    return(sum);
}

int choseplayercard(int first_card,int second_card)
{
    first_card=rand_range(1,13);
   second_card=rand_range(1,13);
    printf("You got %d and %d",first_card,second_card);
    return(first_card+second_card);
}

int chickplayersum(int sum,int special_card,int bet)
{
    int extra_card;
    int money;
    bet=bett(money)
    sum=choseplayercards;
    char player_answer;
    while(sum<21)
    {
    printf("The current sum is %d. Do you want another card? (Y / N)\n",sum);
    scanf("%c"&player_answer);
    while(player_answer=='Y')
        {
           extra_card=rand_range(1,13);
           printf("Extra card:%d.\n",extra_card);
           sum=sum+extra_card;
        }
    if(player_answer=!'Y')
    return(sum);
    }
    if(sum>21)
    printf("The computer has won this round… you lose &d fake dollars!\n",bet)
}

chickwinner()
{
achickcomputercards(int sum,int special_card,int bet)}

int main()
{
  int money=INITIAL_MONEY;
  int seed=0;
  int bet=0;
  int special_card;
  int first_card,second_card;
  int computer_cards;
  int total_computer_cards;
  int player_cards,total_player_cards;
  printf("Welcome to CS intro Blackjack!\n");
  printf("Start by entering a random seed:\n");
  scanf("%d",&seed);
  srand_range(seed);
  while(money>=1)
  {
  bet=bett(money);
  computer_cards=chosecomputercards(first_card,second_card);
  special_card=find_special_card;
  total_computer_cards=chickcomputercards(computer_cards,special_card,bet);
  player_cards=choseplayercard(first_card,second_card);
  total_player_cards=chickplayersum(player_cards,special_card);

  return 0;
}
Posted
Updated 18-May-16 14:34pm
v3

1 solution

"To write function insider other function" is one thing "call function inside another function" is totally, totally different.

C does not support wring "function inside function"; this is called "inner function" and is supported by number of other (more powerful ;-)) languages.

As to the call "call function inside another function", the situation is just the opposite: all calls you do in you C code is the "calls insider another function". Moreover, every function you write, if called, is called "calls insider another function", excluding the entry-point function ("main"), but even this function is called behind the scene by some runtime environment which you don't directly use of modify when you develop an application.

This way, your question is clearly reduced to a more general question, "how to call a function?" This question can be answered by learning the basics of C and general programming. In particular, please see:
C Functions[^],
Function call by Value in C[^],
Function call by reference in C[^].

If you have more specific problems related to your code, you have to ask other questions, well… more specific to your code. But before you do… if you have some compilation errors, collect precise and comprehensive error information; if you have runtime errors, do the same, but it would be better to use C++ (you can put all your code in C++ project) and observe the exception and then provide comprehensive exception information. In all cases, add some comments to the lines where you got the error or exception is thrown and refer to those comments in your description of the problem.

—SA
 
Share this answer
 
Comments
CPallini 19-May-16 2:27am    
5.
Sergey Alexandrovich Kryukov 19-May-16 8:43am    
Thank you, Carlo.
—SA
james7777 19-May-16 8:05am    
ok.. so basicly in chickcomputercards function when i caled chosecomputercards i should define the parmetrs ,
int first_card;
int second_card;
and after that call the function
james7777 19-May-16 8:26am    
thank you for guiding me , if i faced another problems that i couldn't fix i am gonna ask .
Sergey Alexandrovich Kryukov 19-May-16 8:56am    
Sure.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900