Thread: Function for betting within blackjack program

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    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*/
    Last edited by wdewalt; 04-28-2009 at 07:17 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The for is making a loop. I think you want an if there instead. Actually you want a combination of both.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by wdewalt View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    prior to adding "for" it was if and was still incorrect in its action

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by wdewalt View Post
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by wdewalt View Post
    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.
    Last edited by MK27; 04-28-2009 at 07:27 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    I am still learning so if there is a better way to do things I am all ears.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It would be better if you learned how to use what you already know.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    34
    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 ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. one function in my program won't work....
    By talz13 in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2003, 12:19 PM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM