Thread: Debugging help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    Debugging help

    here is my code:
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #define DECKSIZE 52
    #define VALUE 9
    #define FACE 4
    #define TRUE
    #define FALSE
    
    typedef struct {
            int value;
            char suit[10];
            char name[10];
    }Card;
    
    Card cards[DECKSIZE];
    char *faceName[13][10]={"two","three","four","five","six","seven","eight","nine",
                      "ten","jack","queen","king","ace"};
    char *suitName[4][10]={"spades","diamonds","hearts","clubs"};
    void printDeck(){
    int i;
    for(i=0;i<DECKSIZE;i++){
                    printf("%s of %s \n",cards[i].name,cards[i].suit);
                    if((i+1)%13==0  && i!=0) printf("--------------------\n\n");
    }
    }
    void shuffleDeck(){
    srand(time(NULL));
    int this;
    int that;
    Card temp;
    int i;
    for (i=0;i<100;i++){
    int j = i+ rand()%(100-i);
            this=rand()%DECKSIZE;
            that=rand()%DECKSIZE;
    while(this==that)that=rand()%(52+1);
    //printf("shuffle  card%d with card %d\n", this, that);
    temp=cards[this];
    cards[this]=cards[that];
    cards[that]=temp;
    }
    }
    
    int sumHand;(card hand[12], int numCards){
    int i;
    int rval=0;
    for(i=0;i<num;i++){
    rval=hand[i].value
    }
    return rval;
    
    
    
    int main(){
            int suitCount=0;
            int faceCount=0;
            int i;
            for(i=0;i<DECKSIZE;i++){
                    if(faceCount<9){
                            cards[i].value=faceCount+2;
                    }else{
                            cards[i].value=10;
                    }
                    cards[i].suit=suitName[suitCount];
                    cards[i].name=faceName[faceCount++];
                    if(faceCount==13){
                            cards[i].value=11;
                            suitCount++;
                            faceCount=0;
                    }
            }
    {
    
    //printDeck();
    //shuffleDeck();
    //printDeck();
            }
    
    int nextCard=0;
    int numCardsDealer=0;
    Card dealerhand[12];
    int stop=FALSE;
    while(!stop){
    int numCards=0;
    while(sumCards(dealerhand,numCards)<=17){
    dealerhand[numCards]=cards[nextCard++];
    printf("%s of %s value=%d\n", dealerhand[numCards].name
    numCards++;
    
    }
    int finalvalue= sumCards(dealerhand,numCards);
    if(finalvalue>21) printf("BUSTED!");
    printf("Press one to stop or zero to keep going:");
    scanf("%d", &stop);
    
            return 0;
    }
    can some one help me debug this please!!!!!!!!!!!!!!!!!!!1

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Have you tried to run it thro' gdb?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Code:
    Indent correctly and check your { and }.
    Hint : They don't match up.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    //card should be Card
    int sumHand;(card hand[12], int numCards){  
    int i;
    int rval=0;
    for(i=0;i<num;i++){  //num is undeclared
    rval=hand[i].value
    }
    return rval;   //belongs above the closing }
    You MUST work on your indentation, glo. It's very difficult to study code with poor indentation.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by Adak View Post
    Code:
    //card should be Card
    int sumHand;(card hand[12], int numCards){  
    int i;
    int rval=0;
    for(i=0;i<num;i++){  //num is undeclared
    rval=hand[i].value
    }
    return rval;   //belongs above the closing }
    You MUST work on your indentation, glo. It's very difficult to study code with poor indentation.
    thank you will do

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Why would you post something with so many warnings and errors?
    You should be testing as you write.
    You should be posting about "Why won't this work???":
    Code:
    #include<stdio.h>
    #define  DECKSIZE 52
    typedef struct {
            int  value;
            char suit[10];
            char name[10];
    }Card;
    
    Card cards[DECKSIZE];
    
    char *faceName[13][10] =  { "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace" };
    
    char *suitName[4][12] = { "spades", "diamonds", "hearts", "clubs" };
    
    
    int main(void)
    {
      int i = 0;
    
      cards[i].suit = suitName[0];
      cards[i].name = faceName[0];
    
      printf("cards[%d].suit= %s   cards[%d].name= %s \n",
              i , cards[i].suit , i , cards[i].name );
    
      return 0;
    }
    Set warnings on FULL STUN and blast away
    Code:
     >  gcc -Wall -W -pedantic 100415_bjack-3x1.c -o 100415_bjack-3x1
    100415_bjack-3x1.c:11: warning: missing braces around initializer
    100415_bjack-3x1.c:11: warning: (near initialization for ‘faceName[0]’)
    100415_bjack-3x1.c:13: warning: missing braces around initializer
    100415_bjack-3x1.c:13: warning: (near initialization for ‘suitName[0]’)
    100415_bjack-3x1.c: In function ‘main’:
    100415_bjack-3x1.c:20: error: incompatible types in assignment
    100415_bjack-3x1.c:21: error: incompatible types in assignment
    FIX that and fully understand what WAS WRONG before moving on. It's important.
    Last edited by HowardL; 04-16-2010 at 08:47 AM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by HowardL View Post
    Why would you post something with so many warnings and errors?
    You should be testing as you write.
    You should be posting about "why this will not work as expected":
    I'd dig that as a floating banner that stays across the top of your screen as a new user until you convince one of the mods to remove it

    Special switches for gloworm:
    gcc -Wall -pedantic
    Use the first one always.
    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 2010
    Posts
    41
    why am i getting this error with this snippet?:
    error: warning: passing argument 2 of âstrcpyâ from incompatible pointer type
    Code:
    int main(){
            int suitCount=0;
            int faceCount=0;
            int i;
            srand(time(NULL));
            for(i=0;i<DECKSIZE;i++){
                    if(faceCount<9){
                            cards[i].value=faceCount+2;
                    }else{
                            cards[i].value=10;
                    }
                            strcpy(cards[i].suit,suitName[suitCount]);
                            strcpy(cards[i].name,faceName[faceCount++]);
                    if(faceCount==13){
                            cards[i].value=11;
                            suitCount++;
                            faceCount=0;
                    }
            }
    }

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    and here is my code so far, the only problem is my syntax is all messed up and i cant figure out the orientation of them, please help!!!
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #define DECKSIZE 52
    #define VALUE 9
    #define FACE 4
    #define TRUE
    #define FALSE
    
    typedef struct {
            int value;
            char suit[10];
            char name[10];
    }Card;
    
    Card cards[DECKSIZE];
    char *faceName[13][10]={"two","three","four","five","six","seven","eight","nine",
                      "ten","jack","queen","king","ace"};
    char *suitName[4][10]={"spades","diamonds","hearts","clubs"};
    void printDeck(){
    int i;
    for(i=0;i<DECKSIZE;i++){
                    printf("%s of %s \n",cards[i].name,cards[i].suit);
                    if((i+1)%13==0  && i!=0) printf("--------------------\n\n");
    }
    }
    void shuffleDeck(){
    srand(time(NULL));
    int this;
    int that;
            Card temp;
    int i;
    for (i=0;i<100;i++){
    int j = i+ rand()%(100-i);
            this=rand()%DECKSIZE;
            that=rand()%DECKSIZE;
    while(this==that)that=rand()%(52+1);
    //printf("shuffle  card%d with card %d\n", this, that);
            temp=cards[this];
            cards[this]=cards[that];
            cards[that]=temp;
    }
    }
    {
    int numCards=0;
    int sumHand(Card hand[12], int numCards());
    int i;
    int rval=0;
    for(i=0;i<numCards;i++){
            rval=cards[i].value
    }
    return rval;
    }
    
    int main(){
            int suitCount=0;
            int faceCount=0;
            int i;
            srand(time(NULL));
            for(i=0;i<DECKSIZE;i++){
                    if(faceCount<9){
                            cards[i].value=faceCount+2;
                    }else{
                            cards[i].value=10;
                    }
                            strcpy(cards[i].suit,suitName[suitCount]);
                            strcpy(cards[i].name,faceName[faceCount++]);
                    if(faceCount==13){
                            cards[i].value=11;
                            suitCount++;
                            faceCount=0;
                    }
            }
    }
    
    //printDeck();
    //shuffleDeck();
    //printDeck();
    
    {
    int nextCard=0;
    int numCardsDealer=0;
                    Card dealerhand[12];
    int stop;
    int numCards=0;
            while(!stop){
            while(sumCards(dealerhand,numCards)<=17){
                    dealerhand[numCards]=cards[nextCard++];
            printf("%s of %s value=%d\n", dealerhand[numCards].name
                    numCards++);
    
    }           
    }
    
    int finalvalue= sumCards(dealerhand,numCards);
            if(finalvalue>21) printf("BUSTED!");{
                    printf("Press one to stop or zero to keep going:");
                    scanf("%d", &stop);}
    {
            return 0;
    }
    }
    and i am using a gcc compiler and i think thats why i dont get that many warnings. but i dont have a "debugger" nor have i ever used one so maybe someone can tell me where i can get a free one cause everyone keeps tell me to use a debugger.
    i am getting the error"expected identifier before line 81 and 45 can someone just tell me what i am missing
    Last edited by gloworm; 04-18-2010 at 03:58 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The compiler tells you what's up, if you read the message:
    Code:
    $ gcc -Wall -Wextra gloworm.c
    gloworm.c:18: warning: missing braces around initializer
    gloworm.c:18: warning: (near initialization for ‘faceName[0]’)
    gloworm.c:20: warning: missing braces around initializer
    gloworm.c:20: warning: (near initialization for ‘suitName[0]’)
    gloworm.c: In function ‘shuffleDeck’:
    gloworm.c:35: warning: unused variable ‘j’
    gloworm.c: At top level:
    gloworm.c:45: error: expected identifier or ‘(’ before ‘{’ token
    gloworm.c: In function ‘main’:
    gloworm.c:67: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type
    /usr/include/string.h:127: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’
    gloworm.c:68: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type
    /usr/include/string.h:127: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’
    gloworm.c: At top level:
    gloworm.c:81: error: expected identifier or ‘(’ before ‘{’ token
    The error on line 45 is pretty obvious: you have a bunch of code that's not inside a function! Same with line 81.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    i fixed most

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #define DECKSIZE 52
    #define VALUE 9
    #define FACE 4
    #define TRUE
    #define FALSE
    
    typedef struct {
            int value;
            char suit[10];
            char name[10];
    }Card;
    
    Card cards[DECKSIZE];
    char *faceName[13][10]={"two","three","four","five","six","seven","eight","nine",
                      "ten","jack","queen","king","ace"};
    char *suitName[4][10]={"spades","diamonds","hearts","clubs"};
    void printDeck(){
    int i;
    for(i=0;i<DECKSIZE;i++){
                    printf("%s of %s \n",cards[i].name,cards[i].suit);
                    if((i+1)%13==0  && i!=0) printf("--------------------\n\n");
    }
    }
    void shuffleDeck(){
    srand(time(NULL));
    int this;
    int that;
            Card temp;
    int i;
    for (i=0;i<100;i++){
    int j = i+ rand()%(100-i);
            this=rand()%DECKSIZE;
            that=rand()%DECKSIZE;
    while(this==that)that=rand()%(52+1);
    //printf("shuffle  card%d with card %d\n", this, that);
            temp=cards[this];
            cards[this]=cards[that];
            cards[that]=temp;
    }
    }
    int main(){
            int suitCount=0;
            int faceCount=0;
     int i;
            srand(time(NULL));
            for(i=0;i<DECKSIZE;i++){
                    if(faceCount<9){
                            cards[i].value=faceCount+2;
                    }else{
                            cards[i].value=10;
                    }
                            cards[i].suit,suitName[suitCount];
                            cards[i].name,faceName[faceCount++];
                    if(faceCount==13){
                            cards[i].value=11;
                            suitCount++;
                            faceCount=0;
                    }
            }
    
    int numCards=0;{
    int sumHand(Card hand[12], int numCards());
    int i;
    int rval=0;
    for(i=0;i<numCards;i++){
            rval=cards[i].value;
    return rval;
    }
    }
    //printDeck();
    //shuffleDeck();
    //printDeck();
    
    {
    
    int nextCard=0;
    int numCardsDealer=0;
                    Card dealerhand[12];
    int stop;
    int numCards=0;
            while(!stop){
            while(sumCards(dealerhand,numCards)<=17){
                    dealerhand[numCards]=cards[nextCard++];
            printf("%s of %s value=%d\n", dealerhand[numCards].name,numCards++);
    }
    }
    int finalvalue= sumCards(dealerhand,numCards);
            if(finalvalue>21) printf("BUSTED!");{
                    printf("Press one to stop or zero to keep going:");
       scanf("%d", &stop);}
    {
            return 0;
    }
    }}
    i fixed all that with the current changes and it compliles but i get a segmentation fault:
    a.out
    of (null) value=7448132
    Segmentation fault
    why??????
    Last edited by gloworm; 04-18-2010 at 06:30 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Other than the fact that it doesn't exist? If those lines underneath are supposed to be part of that function, then use braces to make it so.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    Other than the fact that it doesn't exist? If those lines underneath are supposed to be part of that function, then use braces to make it so.
    i fixed that but check out my new code and my segmentation fault. Where is my issue???

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Really? You got that code to compile?
    Code:
    gcc -Wall -Wextra gloworm.c
    gloworm.c:18: warning: missing braces around initializer
    gloworm.c:18: warning: (near initialization for ‘faceName[0]’)
    gloworm.c:20: warning: missing braces around initializer
    gloworm.c:20: warning: (near initialization for ‘suitName[0]’)
    gloworm.c: In function ‘shuffleDeck’:
    gloworm.c:35: warning: unused variable ‘j’
    gloworm.c: In function ‘main’:
    gloworm.c:56: warning: left-hand operand of comma expression has no effect
    gloworm.c:56: warning: statement with no effect
    gloworm.c:57: warning: left-hand operand of comma expression has no effect
    gloworm.c:57: warning: value computed is not used
    gloworm.c:86: warning: implicit declaration of function ‘sumCards’
    gloworm.c:88: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    gloworm.c:88: warning: too few arguments for format
    gloworm.c:88: warning: operation on ‘numCards’ may be undefined
    gloworm.c:81: warning: unused variable ‘numCardsDealer’
    /tmp/ccTM92Je.o: In function `main':
    gloworm.c:(.text+0x4af): undefined reference to `sumCards'
    gloworm.c:(.text+0x4da): undefined reference to `sumCards'
    collect2: ld returned 1 exit status

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    Really? You got that code to compile?
    Code:
    gcc -Wall -Wextra gloworm.c
    gloworm.c:18: warning: missing braces around initializer
    gloworm.c:18: warning: (near initialization for ‘faceName[0]’)
    gloworm.c:20: warning: missing braces around initializer
    gloworm.c:20: warning: (near initialization for ‘suitName[0]’)
    gloworm.c: In function ‘shuffleDeck’:
    gloworm.c:35: warning: unused variable ‘j’
    gloworm.c: In function ‘main’:
    gloworm.c:56: warning: left-hand operand of comma expression has no effect
    gloworm.c:56: warning: statement with no effect
    gloworm.c:57: warning: left-hand operand of comma expression has no effect
    gloworm.c:57: warning: value computed is not used
    gloworm.c:86: warning: implicit declaration of function ‘sumCards’
    gloworm.c:88: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    gloworm.c:88: warning: too few arguments for format
    gloworm.c:88: warning: operation on ‘numCards’ may be undefined
    gloworm.c:81: warning: unused variable ‘numCardsDealer’
    /tmp/ccTM92Je.o: In function `main':
    gloworm.c:(.text+0x4af): undefined reference to `sumCards'
    gloworm.c:(.text+0x4da): undefined reference to `sumCards'
    collect2: ld returned 1 exit status
    yeah for some reason my compilier lets it compile with warnings just not errors.
    any thoughts on my segmentation fault?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on debugging
    By Masterx in forum C++ Programming
    Replies: 56
    Last Post: 01-30-2009, 10:09 AM
  2. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  3. Problem in debugging in Eclipse
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 08-21-2007, 09:53 AM
  4. gdb no debugging symbols found
    By Laserve in forum Linux Programming
    Replies: 8
    Last Post: 09-17-2006, 08:56 AM
  5. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM