9 Replies - 396 Views - Last Post: 04 November 2017 - 11:45 AM Rate Topic: ***-- 3 Votes

#1 Shadowfaxxy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-September 17

Problems with the Code::Blocks Debugger

Posted 03 November 2017 - 09:33 PM

Hey guys,

I've been making a little black jack game just for fun, and whenever I try and run the code it just immediately crashes.
But that's not why I'm here. I cant get the debugger in Code::Blocks to work!
I open a new project, then start the debugger. I whole bunch of stuff happens, and console
pops up for a sec, but then everything goes back to where i started, and I get this in the logs.

Active debugger config: GDB/CDB debugger:Default
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: C:\Users\nickm\Desktop\BLACKJACK\
Adding source dir: C:\Users\nickm\Desktop\BLACKJACK\
Adding file: C:\Users\nickm\Desktop\BLACKJACK\bin\Debug\BLACKJACK.exe
Changing directory to: C:/Users/nickm/Desktop/BLACKJACK/.
Set variable: PATH=.;C:\Program Files (x86)\CodeBlocks\MinGW\bin;C:\Program Files (x86)\CodeBlocks\MinGW;C:\ProgramData\Oracle\Java\javapath;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Libs\plplot-5.11.1\buildmingw\dll;C:\Users\nickm\AppData\Local\Microsoft\WindowsApps
Starting debugger: C:\Program Files (x86)\CodeBlocks\MinGW\bin\gdb.exe -nx -fullname -quiet -args C:/Users/nickm/Desktop/BLACKJACK/bin/Debug/BLACKJACK.exe
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.6.1
Child process PID: 2236
[Inferior 1 (process 2236) exited with code 01]
Debugger finished with status 0

If anyone can tell me what's wrong, id really appreciate it!

Side note, if you feel like trying to find whats making my code crash in the first place, go for it! :)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM_CARDS 11
#define NUM_CARDS_DEALT 2
#define MAX_CARDS 21
#define HIT 1
#define STAND 0
#define OTHER 3
#define BLACKJACK 21
#define BLACKJACKDEALER 47
#define BLACKJACKPLAYER 48
#define BUST 22
#define MUST_STOP 17
#define ASK 84
#define RANDOM 35
#define YES 77
#define NO 78
#define RANDO 56

typedef struct
{
    char cardname[8];
    int cardval;
}CARDS;
//FUNCTION PROTOTYPES
void chooseCards(CARDS *[], CARDS *[], CARDS*[]);
void playGame(CARDS *[], CARDS *[], CARDS*[]);

void main(void)
{
    CARDS playercards[MAX_CARDS];
    CARDS dealercards[MAX_CARDS];
    int flag;

    CARDS cards[NUM_CARDS] =
    {
        {"Ace", 11},
        {"Two", 2},
        {"Three", 3},
        {"Four", 4},
        {"Five", 5},
        {"Six", 6},
        {"Seven", 7},
        {"Eight", 8},
        {"Nine", 9},
        {"Ten", 10},
        {"Jack", 10},
        {"Queen", 10},
        {"King", 10},
    };

    chooseCards(&dealercards, &playercards, &cards);

    playGame(&dealercards, &playercards, &cards);

}

void chooseCards(CARDS *dealerPtr[], CARDS *playerPtr[], CARDS *cards[])
{
    int r = rand() % 11;
    int ix=0;

    do
    {
        *dealerPtr[ix]->cardname = *cards[r]->cardname;
        dealerPtr[ix]->cardval = cards[r]->cardval;
        r = rand() % 11;
        ix++;
    }while(ix < 1);

    ix = 0;
    r = rand() % 11;

    do
    {
        *playerPtr[ix]->cardname = *cards[r]->cardname;
        playerPtr[ix]->cardval = cards[r]->cardval;
        r = rand() % 11;
        ix++;
    }while(ix < 1);

}

void playGame(CARDS *dealerPtr[], CARDS *playerPtr[], CARDS *cards[])
{
    int dealerTotal, playerTotal, flag, ix=2;
    char hitstand, yesno;
    int r = rand() % 11;

    dealerTotal = dealerPtr[0]->cardval + dealerPtr[1]->cardval;
    dealerTotal = dealerPtr[0]->cardval + dealerPtr[1]->cardval;

    printf("Welcome to Blackjack 1.0!\n");
    printf("By Nick Mucci\n");
    printf("-----------------------\n");

    printf("Player's Cards:\n");
    printf("%s\n", playerPtr[0]->cardname);
    printf("%s\n", playerPtr[1]->cardname);
    printf("Total: %d\n", playerTotal);
    printf("");
    printf("Dealer Shows: %s\n", dealerPtr[0]->cardname);

    if(dealerTotal == BLACKJACK)
        {
            printf("Dealer has BlackJack!");
            flag = BLACKJACK;
        }

    if(playerTotal == BLACKJACK)
        {
            printf("You have BlackJack!");
            flag = BLACKJACK;
        }

    do //Hitting the players hand
    {
      while(flag != BLACKJACK)
        {
            printf("Would you like to Hit or Stand? (h/s): \n");
            fflush(stdin);
            hitstand = getc(stdin);
            if(hitstand == 'h')
                {
                    flag = HIT;
                }else if(hitstand == 's')
                    {
                        flag = STAND;
                    }else
                        {
                            flag = OTHER;
                        }
        }

        if(flag == HIT)
            {
            *playerPtr[ix]->cardname = *cards[r]->cardname;
            playerPtr[ix]->cardval = cards[r]->cardval;
            r = rand() % 11;
            ix++;
            playerTotal = playerTotal + playerPtr[ix]->cardval;
            }

        if(playerTotal < BLACKJACK)
            {
            flag = ASK;
            }

        if(playerTotal > BLACKJACK)
            {
            printf("You busted.\n");
            flag = BUST;
            }

        if(playerTotal == BLACKJACK)
            {
            flag = BLACKJACKPLAYER;
            }

        if(flag == ASK)
            {
            printf("Your new card is: %s\n", playerPtr[ix]->cardname);
            printf("Your Total is: %d", playerTotal);
            flag = HIT;
            }
    }while(flag == HIT);

    flag = RANDOM;
    ix = 2;
    r = rand() % 11;

    printf("Dealer's Hand: \n");
    printf("%s", dealerPtr[0]->cardname);
    printf("%s", dealerPtr[1]->cardname);

    while(dealerTotal < MUST_STOP)
        {
        *dealerPtr[ix]->cardname = *cards[r]->cardname;
        dealerPtr[ix]->cardval = cards[r]->cardval;
        printf("%s\n", dealerPtr[ix]->cardname);
        dealerTotal = dealerTotal + dealerPtr[ix]->cardval;
        }

    if(dealerTotal > BLACKJACK)
        {
        printf("Dealer Busts.");
        flag = BUST;
        }

    if(playerTotal < dealerTotal)
        {
        printf("Dealer wins!");
        }

    if(playerTotal > dealerTotal)
        {
        printf("Congratulations, you win!");
        }

    do //Play again?
    {
            printf("Do you want to play again? (y/n): ");
            fflush(stdin);
            yesno = getc(stdin);
            if(yesno == 'y')
                {
                    flag = YES;
                }else if(yesno == 'n')
                    {
                        printf("Thanks for playing!");
                        flag = NO;
                    }else
                        {
                            flag = RANDO;
                        }
        }while(flag == RANDO);
}



Good Luck!

Is This A Good Question/Topic? 0
  • +

Replies To: Problems with the Code::Blocks Debugger

#2 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,477
  • Joined: 13-June 14

Re: Problems with the Code::Blocks Debugger

Posted 03 November 2017 - 09:50 PM

Well, it doesn't look like there are any errors from gdb. Have you compiled the code with debugging information (i.e. -g for gcc)?
Was This Post Helpful? 0
  • +
  • -

#3 Shadowfaxxy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-September 17

Re: Problems with the Code::Blocks Debugger

Posted 03 November 2017 - 10:11 PM

I'm not sure what you mean. In that case, I'm probably missing some setup.
Ill try to find that.

This post has been edited by ndc85430: 03 November 2017 - 11:43 PM
Reason for edit:: Removed quote of previous post. Just press "Reply".

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,312
  • Joined: 20-March 10

Re: Problems with the Code::Blocks Debugger

Posted 03 November 2017 - 11:41 PM

void main(void)


is always wrong change to int main() or int main(void) if you must persist with void... for pedantic reasons...

and return 0; for success at the end of int main()

re-run the debugger..

you will now get errors...

As for your program your design principles are terrible, you don't seem to understand the language therefore getting a modern book on C is now your priority...

This project ie a game is currently beyond your skill set, you don't seem to understand what arrays are or what pointers are so using pointer to pointers is at a too advanced level for you currently....
C strings cannot be assigned to other c strings using = you must use a function like strcpy
C strings cannot be compared to other c strings using == you must use a function like strcmp

single chars can be assigned using =
single chars can be compared using ==

You don't seem to understand the rules of blackjack Aces can be either one or eleven also there is an order of precedence in blackjack Ace + Jack beats Ace + Ten for example...

There are other blackjack rules unimplemented...

Get a good book....
https://www.amazon.com/dp/0131103628/
learn the rules...
http://www.bicycleca...play/blackjack/
Was This Post Helpful? 1
  • +
  • -

#5 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,477
  • Joined: 13-June 14

Re: Problems with the Code::Blocks Debugger

Posted 03 November 2017 - 11:46 PM

View PostShadowfaxxy, on 04 November 2017 - 05:11 AM, said:

I'm not sure what you mean.


The compiler, gcc, needs to be told to produce an executable with debugging information so that the program can be debugged. You use the -g option to do that. I don't know where you set compiler options in Code::Blocks, however, so I'd suggest reading the docs.
Was This Post Helpful? 0
  • +
  • -

#6 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 276
  • Joined: 09-September 16

Re: Problems with the Code::Blocks Debugger

Posted 04 November 2017 - 04:14 AM

In Code::Blocks there is a little box with Debug and Release options in the toolbar. Make sure you choose Debug and then hit the little red arrow right to it. That will start the debugger. However, in your case at the bottom right corner there pops up a little box saying sth. about segmentation fault.
Was This Post Helpful? 0
  • +
  • -

#7 Shadowfaxxy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-September 17

Re: Problems with the Code::Blocks Debugger

Posted 04 November 2017 - 08:57 AM

View Postsnoopy11, on 03 November 2017 - 11:41 PM, said:

void main(void)


is always wrong change to int main() or int main(void) if you must persist with void... for pedantic reasons...

and return 0; for success at the end of int main()

re-run the debugger..

you will now get errors...

As for your program your design principles are terrible, you don't seem to understand the language therefore getting a modern book on C is now your priority...

This project ie a game is currently beyond your skill set, you don't seem to understand what arrays are or what pointers are so using pointer to pointers is at a too advanced level for you currently....
C strings cannot be assigned to other c strings using = you must use a function like strcpy
C strings cannot be compared to other c strings using == you must use a function like strcmp

single chars can be assigned using =
single chars can be compared using ==

You don't seem to understand the rules of blackjack Aces can be either one or eleven also there is an order of precedence in blackjack Ace + Jack beats Ace + Ten for example...

There are other blackjack rules unimplemented...

Get a good book....
https://www.amazon.com/dp/0131103628/
learn the rules...
http://www.bicycleca...play/blackjack/


Ummm, do you really think I don't know the rules of blackjack? I just started buddy, its not finished.
And ik its beyond my skillset, that's why I'm doing it for fun and figuring stuff out.
No need to be a dick.
Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,312
  • Joined: 20-March 10

Re: Problems with the Code::Blocks Debugger

Posted 04 November 2017 - 09:15 AM

Im not trying to offend you..

but you have a hell of a lot of errors... and you do stuff like this....

fflush(stdin);


and fflush() is for output streams only not as input streams like stdin....

Its undefined behaviour for all input streams..


ref:

https://www.tutorial...tion_fflush.htm

But I will stand aside and let others guide you from now on...

Thank you for your time..
Was This Post Helpful? 0
  • +
  • -

#9 Shadowfaxxy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-September 17

Re: Problems with the Code::Blocks Debugger

Posted 04 November 2017 - 09:27 AM

I cant take responsibility for that one, your teacher told us to do that before getting a character.
But you're right, I do have a lot to learn, that's why I'm trying this out to see what happens.
Thanks for the tips man

This post has been edited by Skydiver: 04 November 2017 - 11:46 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,102
  • Joined: 05-May 12

Re: Problems with the Code::Blocks Debugger

Posted 04 November 2017 - 11:45 AM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1