Need Help With a BlackJack Program in C

This is a discussion on Need Help With a BlackJack Program in C within the C Programming forums, part of the General Programming Boards category; Can anyone help me get started on this program, i have started it lightlybut seem to get a bit confused ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Post Need Help With a BlackJack Program in C

    Can anyone help me get started on this program, i have started it lightlybut seem to get a bit confused there after when i have to begin adding in new functions. The out put for this should look like this:

    Enter card (1-13): 1
    Enter card (1-13): 10
    Total(s): 11, 21
    Enter dealer's total: 21
    TIE

    Another game (1:Yes/0:No): 1
    Enter card (1-13): 11
    Enter card (1-13): 1
    Total(s): 11, 21
    Enter dealer's total: 17
    YOU WON!!

    Another game (1:Yes/0:No): 1
    Enter card (1-13): 10
    Enter card (1-13): 12
    Total(s): 20
    Another card (1:Yes/0:No): 0
    Enter dealer's total: 22
    YOU WON!!

    Another game (1:Yes/0:No): 1
    Enter card (1-13): 10
    Enter card (1-13): 8
    Total(s): 18

    Another card (1:Yes/0:No): 0
    Enter dealer's total: 20
    YOU LOST!!


    Another game (1:Yes/0:No): 1
    Enter card (1-13): 1
    Enter card (1-13): 1
    Total(s): 2, 12

    Another card (1:Yes/0:No): 1
    Enter card (1-13): 9
    Total(s): 11, 21
    Enter dealer's total: 17
    YOU WON!!

    Another game (1:Yes/0:No): 1
    Enter card (1-13): 2
    Enter card (1-13): 3
    Total(s): 5

    Another card (1:Yes/0:No): 1
    Enter card (1-13): 3
    Total(s): 8
    Another card (1:Yes/0:No): 1
    Enter card (1-13): 2
    Total(s): 10

    Another card (1:Yes/0:No): 1
    Enter card (1-13): 4
    Total(s): 14
    Another card (1:Yes/0:No): 1
    Enter card (1-13): 3
    Total(s): 17
    Another card (1:Yes/0:No): 0
    Enter dealer's total: 17
    TIE

    Another game (1:Yes/0:No): -1
    Error! Enter 1 for yes or 0 for no.
    Another game (1:Yes/0:No): 2
    Error! Enter 1 for yes or 0 for no.
    Another game (1:Yes/0:No): 1
    Enter card (1-13): 0
    Error card must be between 1 and 13!
    Enter card: 14
    Error card must be between 1 and 13!
    Enter card: 8
    Enter card (1-13): 2
    Total(s): 10
    Another card (1:Yes/0:No): -1
    Error! Enter 1 for yes or 0 for no.
    Another card (1:Yes/0:No): 2
    Error! Enter 1 for yes or 0 for no.
    Another card (1:Yes/0:No): 1
    Enter card (1-13): 1
    Total(s): 11, 21
    Enter dealer's total: 20
    YOU WON!!
    Another game (1:Yes/0:No): 0


    i dont have much as of yet but here is what i have so far:


    Code:
    #include <stdio.h>
    
    
    main()
    {
    int total;
    int card1, card2;
    int dealer;
    
    
    
            total = 0;
    
            printf("Enter Card (1-13): ");
            scanf("%d", &card1);
            printf("Enter Card (1-13): ");
            scanf("%d", &card2);
            total = card1 + card2;
            printf("Total(s): %d\n", total);
            printf("Enter Dealer's Total: ");
            scanf("%d", &dealer);
    
    }
    and required functions i need to be used are :
    int validateCard();
    int value(int card);
    void endResult(int Lost,highest total);
    int anotherCard();
    void totals(int total, int numAces, int *pLost, int *pHighestTotal);


    your help would be greatly appriciated!!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    So what's your question? plz email me teh codez?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    which codes? like what i need to do?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Jp2009 View Post
    like what i need to do?
    Well... Write code, I'd guess...

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    well my question is basically how do i get started using the functions i posted in the initial thread? i get screwed up also because when the card is 1 it is suppose to be ACE which can be 1 or 10 so i have to total it twice....and then ask the user if they would like another card

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Well, the names of the functions are clear indicators for what they should do, so what's keeping you from writing them?
    And the ace having 2 possible values is the entire reason of having the number of aces as a parameter in the total function.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why don't you try writing at least one function (in fact, a Very Good Idea is to only do one function at a time, and make sure it works before you move on)...if you have a problem with that, then post the code. I don't think anyone will just write a function from scratch for your blackjack game.

    If you are having problems just with the idea of "writing a function" (your Original Post does not include any), then the issue of what the functions are supposed to be called, etc. is kind of irrelevent; you need to ask more specific questions...
    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

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    ok thanks,

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    ok so this is what i have created so far but i cant get it to compile yet, i still have a couple functions to add.... any suggestions as to what i am doing wrong so far??

    Thanks,


    Code:
    #include <stdio.h>
    int validateCard();
    int value(int care);
    void endResult(int Lost,highest total);
    int anotherCard();
    void totals(int total, int numAces, int *pLost, int *pHighestTotal);
    
    
    main()
    {
    int card1, card2, total, dtotal;
    
    printf("Enter card: \n");
    scanf("%d" , &card1);
    printf("Enter card: \n");
    scanf("%d" , &card2);
    
    validateCard(card1, card2);
    
    total = card1 + card2;
    
    printf("%d" , total);
    
    printf("Enter dealer total: \n");
    scanf("%d" , &dtotal);
    
    endResult(total, dtotal);
    
    
    
    int validateCard(*c1, *c2){
    if (card1 > 10)
    card1 = 10;
    if (card2 > 10)
    card1 = 10;
    }
    
    
    void endResult{
    if (total > dtotal)
    printf("You Win");
    
    else if(dtotal > total)
    printf("You Lose");
    
    else
    printf("Tie");
    }
    
    }

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Jp2009 View Post
    ok so this is what i have created so far but i cant get it to compile yet,
    1. post compilation errors
    2. indent your code
    3. use int main(void) - read FAQ why
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Code:
    #include <stdio.h>
    int validateCard();
    int value(int care);
    void endResult(int Lost,highest total);
    int anotherCard();
    void totals(int total, int numAces, int *pLost, int *pHighestTotal);
    
    
    main(void)
    {
    int card1, card2, total, dtotal;
    
         printf("Enter card: \n");  
         scanf("%d" , &card1);
         printf("Enter card: \n");
         scanf("%d" , &card2); 
    
    validateCard(card1, card2);
    
    total = card1 + card2;
    
          printf("%d" , total);
    
          printf("Enter dealer total: \n");
          scanf("%d" , &dtotal);
    
    endResult(total, dtotal);
    
    
    
    int validateCard(*c1, *c2){
        
         if (card1 > 10)
         card1 = 10;
         if (card2 > 10) 
         card1 = 10;
    }
    
    
    void endResult{
    
        if (total > dtotal)
        printf("You Win");
    
    
        else if(dtotal > total)
        
        printf("You Lose");
    
    else
        printf("Tie");
    }
    
    }



    and the compiling errors are:

    bj1.c:4: error: expected declaration specifiers or '...' before 'highest'
    bj1.c: In function 'main':
    bj1.c:27: error: too many arguments to function 'endResult'
    bj1.c:31: error: expected declaration specifiers or '...' before '*' token
    bj1.c:31: error: expected declaration specifiers or '...' before '*' token
    bj1.c:39: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The compiler is terribly confused.
    Code:
    void endResult(int Lost,highest total);
    what is "highest"? The placement of it suggest to the compiler that you have a type called highest. But there is no declaration of such a type yet.

    Code:
    int validateCard();
    ...
    validateCard(card1, card2);
    ...
    int validateCard(*c1, *c2)
    So first you say that validateCard takes an arbitrary number of arguments (or no arguments in C++), then that you pass two int arguments, and then you say that it takes to pointers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Code:
    #include <stdio.h>
    int validateCard();
    int value(int card);
    void endResult(int Lost, highestTotal);
    int anotherCard();
    void totals(int total, int numAces, int *pLost, int *pHighestTotal);
    
    
    int main(void)
    {
    int card1, card2, total, dtotal;
    int newgame, int another;
    
    printf("Enter card: \n");
    scanf("%d" , &card1);
    printf("Enter card: \n");
    scanf("%d" , &card2);
    
    validateCard(card1, card2);
    
    total = card1 + card2;
    
    printf("%d" , total);
    
    printf("Enter dealer total: \n");
    scanf("%d" , &dtotal);
    
    if (total < 16 || total > 27)
    printf("Error Card Must Be Between 1 and 13!");
    
    endResult(total, dtotal);
    
    
    
    int validateCard(*c1, *c2){
    if (card1 > 10)
    card1 = 10;
    if (card2 > 10)
    card1 = 10;
    }
    
    
    int anotherCard(another);
    printf("Another Card? (1:Yes/0:No)");
    scanf("%d", &another);
    if(another==1)
    /* i need this to go back to main to prompt for a new card*/
    
    else if
    (another==0)
    /* i need this to go to the dealers total*/
    void endResult{
    
    if (total > dtotal)
    printf("You Win");
    
    else if(dtotal > total)
    printf("You Lose");
    
    else
    printf("Tie");
    }
    
    printf("Another Game? 1:Yes/0:No");
    scanf("%d", &newgame);
    if (newgame==1)
    /* i need this to loop the game all over again*/
    else if
    (newgame==0)
    /* i need this to end the game*/
    
    }
    if anyone can help me figure this out it would be great, also i need int numAces to have it when a the number equals 1 for ace to have it calculate ace as 1 and 11 - i have no idea how to do this so if anyone can help again please that would be awsome!
    thanks,

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why are you posting the same code again, when I have suggested some of the things that are wrong (or did you change something else, but not what I suggested?)

    No one is going to write the code for you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    i realize noone is going to write the code for me,
    but i dont quite understand what you suggested either, could you clarify it a bit?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. blackjack program
    By mackieinva in forum C Programming
    Replies: 8
    Last Post: 09-20-2007, 04:46 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21