My code doesn't work, help please

This is a discussion on My code doesn't work, help please within the C Programming forums, part of the General Programming Boards category; I want it to read 5 cards then display a text value, any idea why is does do that? Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    35

    My code doesn't work, help please

    I want it to read 5 cards then display a text value, any idea why is does do that?

    Code:
    #include <stdio.h>
    
    
    int main( void )
    {
       char inputtedhand[25];
       int index, i, m;                  
       int text;
       int tempvar;
       char tempsuit;     
       int cardvalue[5];
       int cardtext[5];   
       char cardsuit[5];
       
       scanf( "%d", inputtedhand[25]);   
       
        for( index==1; index<=5; index++)   
        /* This is going to count up from 1 through to 5, fo we can read all the 4 card in the hand */       
           {
           for(i==0; i<=12; i+=3)
           /* This loop will be counting in multiples of 3, starting at 0, it is used so we can locate specific places within the array*/
              {
                  scanf("%d",  inputtedhand[i]); 
                  
                  if ("%d",inputtedhand[i]==2)
                    { 
                       printf( "The two of"); 
                       tempvar=2; 
                    }        
                  else if (inputtedhand[i]==3)
                    {
                       printf( "the three of");
                       tempvar=3;
                     }
                  else if (inputtedhand[i]==4)
                    { 
                       printf("%c", "the four of");
                       tempvar=4;
                    } 
                 else if (inputtedhand[i]==5)
                    {
                       printf("%c", "the five of");
                       tempvar=5;
                    }
                 else if (inputtedhand[i]==6)
                    {
                       printf("%c", "The six of");
                       tempvar=6; 
                    }
                 else if (inputtedhand[i]==7)
                    {
                       printf("%c", "The seven of");
                       tempvar=7;
                    }    
                 else if (inputtedhand[i]==8)
                    {  
                       printf("%c", "The eight of");
                       tempvar=8;
                    }  
                 else if (inputtedhand[i]==9)
                    {
                       printf("%c", "The nine of");  
                       tempvar=9;
                    }
                 else if (inputtedhand[i]==0)
                    {
                       printf("%c", "The ten of");
                       tempvar=10;
                    }
                else if (inputtedhand[i]=="J")
                   {
                      printf("%c", "the jack of");
                      tempvar=11;
                   }
                else if (inputtedhand[i]=="Q")
                   {
                      printf("%c", "the queen of");
                      tempvar=12;
                   }
                else if (inputtedhand[i]=="K")
                   {
                      printf("%c", "the king of");
                      tempvar=13;
                   }
                else if (inputtedhand[i]=="A")
                   {
                      printf("%c", "the ace of");
                      tempvar=14;
                   }
                else 
                   {   
                   printf("the value of the card is not recognised");
                   }
                   
                   m=(i+1);
                   /* The card suit is always the nect character after the value, so 'm' will locate the suit for us */
               
                  scanf("&d", &inputtedhand[m]);
                    {
                     if (inputtedhand[m]=="C")
                        {
                           printf("%c", "Clubs\n");
                          
                        }
                     else if (inputtedhand[m]="D")
                        {
                           printf("%c", "Diamonds\n");
                          
                        }
                     else if (inputtedhand[m]="H")
                        {
                           printf("%c", " Hearts\n");
                           
                        }
                     else if (inputtedhand[m]="S")
                        {
                           printf("%c", "Spades\n");                       
                        }   
                   }
                      
                      
                       {
                         if (index=1) 
                         {
                            cardvalue[index]=tempvar;
                            cardsuit[index]=tempsuit;
                                                  
                         }  
                         else if (index=2)
                         {
                            cardvalue[index]=tempvar;
                            cardsuit[index]=tempsuit;
                            
                         }  
                         else if (index=3)
                         {
                            cardvalue[index]=tempvar;
                            cardsuit[index]=tempsuit;
                            
                         }  
                         else if (index=4)
                         {
                            cardvalue[index]=tempvar;
                            cardsuit[index]=tempsuit;
                           
                         }   
                         else if (index=5)
                         {
                            cardvalue[index]=tempvar;
                            cardsuit[index]=tempsuit;
                            
                         }
                         /* Here we stored the values we found for each card into an array, this is so we can perform the buble sort algorithm on it */
                      }    
             }                                          
          }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,120
    You have some problems with notation, many (or most?) of which could be found if compiling with warnings turned on. A simple strategy is to compile with warnings on and then read the very FIRST diagnostic message, FIX IT, then try to recompile again. If you don't understand the message you see, try to get help on just that one issue. Repeat this process until all warnings/errors are fixed.

    For example the very first problem is on this line:

    scanf( "%d", inputtedhand[25]);

    1. Because you made inputtedhand as having 25 elements, the indices go from 0..24, so inputtedhand[25] is out of bounds.
    2. Because you are using scanf, the variables after the format string must be of pointer type. I.e. You must prepend & to a char variable for this to work properly.

    There are other problems, but it's better if you go from top to bottom and fix as much as you can by reading the messages from your compiler and comparing it to examples in your reference material.

    Also for a program with input like this it helps to think about what the input should be and to say this if you ask for help here. For example, you need to say what input you gave the program or what input you want to give the program.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Where can I download a compiler that will do that for me?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,120
    Most compilers do have this option, and there are many that are free or have free versions for download. GCC and Visual C++ are popular.

    The top pages of this site have an article on this topic, Getting Started:

    How to Get Started with C or C++ - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C+P code use to work but now it doesn't!
    By CodeMonkey03 in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2012, 07:33 PM
  2. Example Code doesn't work!
    By Kayl669 in forum C Programming
    Replies: 8
    Last Post: 02-18-2010, 09:19 AM
  3. Simple C++ code doesn't work
    By alex_dude_122 in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2006, 12:53 PM
  4. this code doesn't work please help me debug
    By newcstudent in forum C Programming
    Replies: 2
    Last Post: 10-09-2006, 05:01 AM
  5. this code compiles, but doesn't work how it should
    By Leeman_s in forum C++ Programming
    Replies: 10
    Last Post: 09-10-2002, 05:31 PM

Tags for this Thread


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