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 ;-)