# Function for betting within blackjack program

• 04-28-2009
wdewalt
Function for betting within blackjack program
So here is my problem. The betting function is the only part of my program not working right. I am sure it is something simple, but I am missing it.

1. keeps showing minimum bet message when betting over 10 which is wrong
2. doesn't loop correctly, if I put in an amount lower than 10 I get the right message, if I do it twice it just goes to playing which is wrong. I can enter the whole code if needed.

Code:

```int betting() /*Asks user amount to bet*/ {  printf("\n\nEnter Bet: \$");  scanf("%d", &bet);  for (bet >= 10002){ /*If player tries to bet more money than player has*/                   printf("\nYou cannot bet more money than you have.\n");         return 0;  }  else if(bet <= 10){ /*If a player tries to bet under the minimum*/                   printf("\nThe minimum bet at this table is 10 dollars.\n");  else printf("\nGood bet!!");                 return 0;   }  }  else return bet; } /*End Function*/```
• 04-28-2009
quzah
The for is making a loop. I think you want an if there instead. Actually you want a combination of both.

Quzah.
• 04-28-2009
MK27
Quote:

Originally Posted by wdewalt
for (bet >= 10002){ /*If player tries to bet more money than player has*/

In C programming, most people would want to use an "if" here.
• 04-28-2009
wdewalt
prior to adding "for" it was if and was still incorrect in its action
• 04-28-2009
quzah
Quote:

Originally Posted by wdewalt
prior to adding "for" it was if and was still incorrect in its action

Incorrect how? It would be a "different" incorrect with that change.
Code:

```bet = 0 while bet < 10 || bet > 10002     prompt for bet     read bet```
You might actually consider using a different varaible instead of just hard coding the 10002. If they lose money, they are no longer going to have \$10002, so you shouldn't let them bet 10002 any more.

Also, you're using global variables, which are sucky.

Quzah.
• 04-28-2009
MK27
Quote:

Originally Posted by wdewalt
prior to adding "for" it was if and was still incorrect in its action

Just so you understand: using a "for" then an "else if" is silly to the point of nonsense.

Also, the "good bet" else if is nested inside the <=10 else if.
• 04-28-2009
wdewalt
I am still learning so if there is a better way to do things I am all ears.
• 04-28-2009
whiteflags
It would be better if you learned how to use what you already know.
• 04-29-2009
mrcheesypants
Ok you're developing some bad habits here and I'm going to give you some advice to fix them as well as the code.
1)When debugging, try not to do random things to fix code This is especially hard for a beginner, but try to remember what everything does. Ask yourself why this would fix your bug. You treated the for statement like magic and it did not fix your bug. Use an if statement instead. For loops are for iterations, if statements are for comparisons.
2Learn an indent style and use it.Indent style - Wikipedia, the free encyclopedia Personally I prefer K&R with 1TBS which tends to be the most popular indent style .Let me put your code in a proper indent style and see if you can spot your bug.
Code:

```int betting() /*Asks user amount to bet*/ {     printf("\n\nEnter Bet: \$");     scanf("%d", &bet);     if(bet >= 10002){ /*If player tries to bet more money than player has*/         printf("\nYou cannot bet more money than you have.\n");         return 0;     }     else if(bet <= 10){ /*If a player tries to bet under the minimum*/         printf("\nThe minimum bet at this table is 10 dollars.\n");     else         printf("\nGood bet!!");         return 0;     } }  else return bet; } /*End Function*/```
I'll post code later in this post, but you really should try and figure out what's wrong with this code before scroll down this page.

3)Avoid magic numbers Magic numbers are random constants in your code (like the ones in your if statement). You should avoid them like the plague. There are two reasons for this
i) Allows for more abstraction. Wouldn't this function be more useful if you could define the bet minimum or bet maximum in a function arguement? Would allow for more tables yes? Use an int argument in your function for established bet minimum and maximum
ii)Makes your code more readable. One of your goals of programming should make the code readable and self commenting. Consider the following code compare to your previous code.

Code:

```    if(bet >= bet_max){         printf("\n You cannot bet more money than you have.\n");   }```
Now programmer can look at this code and immediately understand what it does without the comment
Code:

` /*If player tries to bet more money than player has*/`
Makes the code prettier and saves you something to type. While I'm talking about magic numbers, there are two reasons to use comments
Reason 1: To tell a programmer what a block of code does. These comments are almost always used before a function or section of really hairy code. Try to avoid the hairy code.
Reason 2: To tell a programmer why you did something. It's kinda hard to give an example of this but in programming there are multiple ways of accomplishing the same task (Perl philosophy is built on this idea). Sometimes one way is better than another and you want to warn another programmer (which for hobbyist is generally "future self") why you wrote the code the way you did.

Anyway, I'll give you some fixed code now. I do hope you looked at the indented code before hand and found out where your bug was.
Code:

```/*I put your code in it's own program to test it better */ #include<stdio.h> /*These defines are used to get rid of magic numbers.  * These days const ints are preferred but I still have a habit  *  of using #define */ #define BET_MAX 100002 #define BET_MIN 10 int main() {     int bet = 0;     printf("\n\nEnter Bet: \$");     scanf("%d", &bet);     if(bet >= BET_MAX){         printf("\n You cannot bet more money than you have.\n");     }     else if(bet <= BET_MIN){         printf("\nThe minimum bet at this table is %d dollars.\n", BET_MIN);     } /* You forgot this bracket*/     else{         printf("\nGood bet!!");     }     return bet;         }```
Now here are some goals I'm going to give you for this function/program
1) Do you have a Player structure? Look up structures in your C book (get one. K&R might be a bit too advance for you, but it's a good reference. I recommend that or O'Reily's C book) and use them. You can store how much money each player has and then use it as a bet max.
2) Write a function that adds money to a Player. Assuming this is black jack, your bet is
going to be added to a player. This is simple but requires you to read on/use pointers to structs and struct attributes.
3)Keep practicing/reading. You're going to get flamed on programming forums, but you're just starting out. The only way you're going to get better at programming is to program. Reading just saves you from getting flamed on forums ;-)