Click here to Skip to main content
15,910,130 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am working on a blackjack game , i am still a biganner and i am having some problems with writing functions .
i want to have something like this ...

for example:
Welcome to CS intro Blackjack!
Start by entering a random seed:
5
You currently have 1000 fake dollars. How much do you want to bet?
1300
our bet must be at least 1 and at most 1000, try again:
0
Your bet must be at least 1 and at most 1000, try again:
1000


What I have tried:

C++
int main()
{
  int money=0;
  int seed=0;
  int bet=0;
  printf("Welcome to CS intro Blackjack!\n");
  printf("Start by entering a random seed:\n");
  scanf("%d",&seed);
  bet=bett(money);

  return 0;
}
int bett(int x)
  {
      int y=0;
      printf("You currently have %d fake dollars. How much do you want to bet?",x);
      scanf("%d",&y);
      while(y>x)
      {
         printf("Your bet must be at least 1 and at most %d, try again:",x);
          scanf("%d",&y);
      }
      return(y);
  }
Posted
Updated 16-May-16 14:33pm
v3

1 solution

First you have to figure the logic of your program.
I guess this is the first assignment in a school project, so not too complicated yet.

First you have two input limitations: the entered bet must be more than 1 and less than available funds.
Hard coded values is always a bad idea, so start with declaring a constant for the lower limit:
For example
C++
#define MIN_BET 1


If you look at function bett(int x), your logic there is not correct.
You call the function with the variable money, but you have initialized money to zero.
You need to assign an initial value to money first and this should also be a constant.
C++
#define INITIAL_MONEY 1000


then assign
C++
money = INITIAL_MONEY;
bet = bett(money);
or
bet = bett(INITIAL_MONEY);


Also learn from the beginning to use reasonable names for your variables. Naming them x, y and z is a recipe for confusion.
C++
x could be called initialMoney
y could be called riskedMoney

As this is C, you need to define a prototype for your functions before the main() function. Or put the main function last.
C++
// Use descriptive names for functions as well
int GetBetFromPlayer(int initialMoney);

int main()
{
}


In the while loop, you only have one condition, and you need two right.
You need to check that the riskedMoney is within both the upper and lower limits.
so
C++
while(y>x)

should be
C++
while ((riskedMoney < MIN_BET) && (riskedMoney > initialMoney))

You might make a note of which statement you think is more readable.
Don't be afraid of using long names and spaces to make your code more easy to read.

There, I think this will keep you occupied for a while.
 
Share this answer
 
v2
Comments
james7777 17-May-16 4:12am    
thank you so much for you're help :) i am gonna fix this later .
George Jonsson 17-May-16 4:37am    
You are welcome.

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