0

I've only been using c for a couple of weeks, so there will very likely be obvious errors I've overlooked. I've looked at other threads, but I don't understand a lot of what I'm reading. The program assumes an infinitely large deck.

KNOWN ISSUES:

  • clearBuffer isn't currently in use, I am trying different things.
  • dealerhand doesn't add values properly.
  • compare function doesn't exist yet.
  • Multiple non numeric inputs result in repeating of error message.
  • Attempting to "hit" crashes program.

Current code:

#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int deal()
{
    unsigned long timenow;
    timenow = time(NULL);
    srand(timenow);
    int deck[] ={2,3,4,5,6,7,8,9,10,10,10,10,11};
    return deck[rand() % 13];
        printf("%d\n", deal());
}

void clearBuffer(){
    char ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
}

int dealerhand()
{
    int dealerhand[10];
    int dealertotal = 0;
    while (dealertotal < 17)
    {
        dealertotal = deal();
        printf("%d\n dx", deal());
        dealertotal = dealertotal + deal();
        printf("%d\n D", dealertotal);
    }
}

int playerhand()
{
    int playerhand [10];
    int i;
    for(i=0; i<1; i++)
    {
        playerhand[i] = deal();
        printf(" %d\n", playerhand[i]);
    }
}

int hit()
{
    int i;
    int playerhand[10];
    playerhand[i] = deal();
    printf(" %d", playerhand[i]);
}

int main() 
{
    int hold = 1;
    int num;
    int num2;
    int money = 500;
    printf("Welcome to Blackjack! You have $500. Play a hand? 1=y 2=n. ");
    while (hold = 1)
        {
            scanf ("%d",&num);
            if (num == 1)
                {
                    printf("Great! Ante is $20, here's your hand!");
                    playerhand();
                    playerhand();
                    dealerhand();
                    money = money - 20;
                    printf("You have %d dollars. Hit? 1=y 2=n", money);
                    //clearBuffer();
                    scanf("%d", num2);
                    if (num2 = 1)
                    {
                        playerhand();
                        break;
                    }
                    if (num2 = 2)
                    {
                     //   compare();
                    }
                    }
            if (num == 2)
                {
                    printf("Too bad, come back soon!");
                    break;
                }
            if (num != 1 || num != 2)
                {
                printf("Sorry what was that? Play a hand? 1=y 2=n.");
                while((num = getchar()) =! EOF && num != '\n');
                }
        }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
NoldorianKing
  • 11
  • 1
  • 3
  • in your `deal()` function after return statement, your `printf()` never run and use stdlib.h instead of cstdlib – Parham Alvani Feb 07 '15 at 19:58
  • 1
    Small point, use `srand((unsigned)time(NULL));` **once**, at the start of `main()`. If you need to debug using an repeatable sequence, comment it out. – Weather Vane Feb 07 '15 at 20:26
  • See [`srand()` — why call it only once?](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/) for an explanation of @WeatherVane's comment. – Jonathan Leffler Feb 07 '15 at 21:43

1 Answers1

1

I tried to correct this but it have many issue see following source and try to correct your logical errors, this is best I can do for you:

#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int deal(void)
{
    int deck[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
    return deck[rand() % 13];
}

void clearBuffer(void)
{
    char ch;

    while ((ch = getchar()) != '\n' && ch != EOF);
}

int dealerhand(void)
{
    int dealerhand[10];
    int dealertotal = 0;

    while (dealertotal < 17) {
        dealertotal = deal();
        printf("%d\n dx", deal());
        dealertotal = dealertotal + deal();
        printf("%d\n D", dealertotal);
    }
    /* RETURN INT ... */
}

int playerhand(void)
{
    int playerhand[10];
    int i;

    for (i = 0; i < 10; i++) {
        playerhand[i] = deal();
        printf(" %d\n", playerhand[i]);
    }
    /* RETURN INT ... */
}

int hit(void)
{
    int i;
    int playerhand[10];

    /* I think you need for here ... */
    for (i = 0; i < 10; i++) {
        playerhand[i] = deal();
        printf(" %d", playerhand[i]);
    }

    /* RETURN INT ... */
}

int main(int argc, char *argv[])
{
    srand((unsigned)time(NULL));

    int hold = 1;
    int num;
    int num2;
    int money = 500;

    printf("Welcome to Blackjack! You have $500. Play a hand? 1=y 2=n. ");

    while (hold == 1) {
        scanf("%d", &num);
            if (num == 1) {
                printf("Great! Ante is $20, here's your hand!");
                playerhand();
                playerhand();
                dealerhand();
                money = money - 20;

                printf("You have %d dollars. Hit? 1=y 2=n", money);
                scanf("%d", &num2);
                if (num2 == 1) {
                    playerhand();
                    break;
                }
                if (num2 == 2);
                    /* compare(); */
            }
            if (num == 2) {
                printf("Too bad, come back soon!");
                break;
            }
            if (num != 1 || num != 2) {
                printf("Sorry what was that? Play a hand? 1=y 2=n.");
                while (((num = getchar()) != EOF) && num != '\n');
            }
    }
}
Parham Alvani
  • 2,305
  • 2
  • 14
  • 25