Thread: Book: Absolute Beginners problem

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Book: Absolute Beginners problem

    I'm a complete beginner working through the "C Programming for Absolute beginners" book.
    Every once in a while I run into an example that won't compile correctly. I'm trying to run an end of chapter program the book has written in it. And it compiles, and tries to run, then crashes.

    Being so new to c programming I'm not entirely sure what to look for since the Dev-c++ compiler I use didn't pick up an error before running the program.

    Below is the code, would much appreciate any thoughts or tips, anything I missed.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <stdlib.h>
    
    //define new data type
    typedef struct deck{
            
            char type[10];
            char used;
            int value;
            }aDeck; //end type
            
      //func proto
      void shuffle( aDeck *);
      
    main()
    {//start main
    
       int x,y;
       
       aDeck myDeck[52];
       
       srand ( time(NULL) );
       
       //initialize struct array
       for (x=0; x<3; x++){//start for
         for(y=0; y<13; y++){//start for
         
         switch (x){//start switch
         
             case 0:
                  strcpy(myDeck[y].type, "diamonds");
                  myDeck[y].value=y;
                  myDeck[y].used='n';
                  break;
             
             case 1:
                  strcpy(myDeck[y + 13].type, "clubs");
                  myDeck[y+13].value=y;
                  myDeck[y+13].used='n';
                  break;
             
             case 2:
                  strcpy(myDeck[y + 26].type, "hearts");
                  myDeck[y+26].value=y;
                  myDeck[y+26].used='n';
                  break;
             
             case 3:
                  strcpy(myDeck[y +39].type, "spades");
                  myDeck[y+39].value=y;
                  myDeck[y+39].used='n';
                  break;
                  
                  }//end switch
                  }//end inner for
                  }//end outer for
             
             shuffle(myDeck);
            
            
       }//end main
       
       void shuffle(aDeck * thisDeck)
    {//start func
    
       int x;
       int iRnd;
       int found=0;
       
       system("cls");
       printf("\nYour five cards are: \n\n");
       
       while (found <5){//start while
          
        iRand=rand() % 51;
          
          if (thisDeck[iRnd].used=='n'){//start if
              
              switch(thisDeck[iRnd].value){//start switch
                 
                 case 12:
                      printf("Ace of %s\n", thisDeck[iRnd].type);
                      break;
                 
                 case 11:
                      printf("King of %s\n", thisDeck[iRnd].type);
                      break;
                 
                 case 10:
                      printf("Queen of %s\n", thisDeck[iRnd].type);
                      break;
                 
                 case 9:
                      printf("Jack of %s\n", thisDeck[iRnd].type);
                      break;
                 
                 default:
                         printf("%d of ", thisDeck[iRnd].value+2);
                         printf("%s\n", thisDeck[iRnd].type);
                         break;
                         }//end switch
                         
                 thisDeck[iRnd].used='y';
                 found = found+1;
                 }//end if
                 }//end while
          }//end shuffle

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    for (x=0; x<3; x++){//start for
    1) That should probably be x < 4.


    Code:
    void shuffle(aDeck * thisDeck)
    {//start func
    
       int x;
       int iRnd;
       int found=0;
       
       system("cls");
       printf("\nYour five cards are: \n\n");
       
       while (found <5){//start while
          
        iRand=rand() % 51;
          
          if (thisDeck[iRnd].used=='n'){//start if
              
              switch(thisDeck[iRnd].value){//start switch
                 
                 case 12:
                      printf("Ace of %s\n", thisDeck[iRnd].type);
                      break;
                 
                 case 11:
                      printf("King of %s\n", thisDeck[iRnd].type);
                      break;
                 
                 case 10:
                      printf("Queen of %s\n", thisDeck[iRnd].type);
                      break;
                 
                 case 9:
                      printf("Jack of %s\n", thisDeck[iRnd].type);
                      break;
                 
                 default:
                         printf("%d of ", thisDeck[iRnd].value+2);
                         printf("%s\n", thisDeck[iRnd].type);
                         break;
                         }//end switch
                         
                 thisDeck[iRnd].used='y';
                 found = found+1;
                 }//end if
                 }//end while
          }//end shuffle
    2) Is the iRnd/iRand thing a typo? If not then this value (iRnd) isn't being set and has a completely random value (not the random range that you expect) and is likely causing the error when you try to access thisDeck[234576] (as an example) because you are attempting to access memory that you don't own.
    3) rand() % 51 produces a range from 0 through 50 meaning index 51 never gets picked, you probably want rand() % 52 instead.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok ... a few things...
    DEVC++ is terribly outdated (and thus not a very good learning tool) it's been abandoned for nearly a decade. You would do well to update. If you are doing plain C coding I suggest Pelles C or if you're also doing C++ you might try Code::Blocks with MinGw (google is your friend).

    Tutorials and books often have mistakes in them... It's kind of a challenge to find them and fix them, shows you're learning...

    I just ran your code through Pelles C and here are the errors I got...
    Code:
    Building main.obj.
    E:\c_Code\Experiments\testconsole\main.c(17): warning #2099: Missing type specifier; assuming 'int'.
    E:\c_Code\Experiments\testconsole\main.c(77): error #2048: Undeclared identifier 'iRand'.
    E:\c_Code\Experiments\testconsole\main.c(68): warning #2114: Local 'x' is not referenced.
    *** Error code: 1 ***
    Done.
    Note that the line numbers are in brackets, so you shouldn't have too much trouble finding them...
    the first one is because it's not ... main() ... it's .... int main (void) and main always returns a value (usually 0).

    When I fixed the errors it ran nicely.
    Last edited by CommonTater; 05-04-2011 at 02:02 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2

    Thank you

    Thank you both for analyzing my newbie code, very helpful!


    I'm switching over to Pelles C, latest version, see what I can do with it.

    Thanks again

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Slingshottheory View Post
    Thank you both for analyzing my newbie code, very helpful!
    I'm switching over to Pelles C, latest version, see what I can do with it.
    Thanks again
    Before you get too far into it... it's quite different than DEV... so spend some time in the help file. Pelles C has the best help system I've ever seen.
    Especially be sure to read up on how to create projects... it's slightly non-obvious until you get the hang of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats the best beginners book?
    By bluenose90 in forum C# Programming
    Replies: 3
    Last Post: 06-03-2010, 10:40 PM
  2. Absolute Doozy of a problem I don't understand
    By Hexadakota in forum C++ Programming
    Replies: 6
    Last Post: 03-11-2008, 11:08 PM
  3. Petzold's Book Good For Beginners?
    By drdroid in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2002, 01:35 PM
  4. My book recommendations for rank beginners ...
    By snakum in forum C++ Programming
    Replies: 4
    Last Post: 08-21-2002, 10:38 AM
  5. What is a good beginners' C++ book?
    By GrNxxDaY in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2002, 09:50 AM