Like Tree2Likes
  • 1 Post By Tclausex
  • 1 Post By std10093

&& in statements

This is a discussion on && in statements within the C Programming forums, part of the General Programming Boards category; I am trying to write a poker hand score system. I have worked everything out except how to do full ...

  1. #1
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67

    && in statements

    I am trying to write a poker hand score system. I have worked everything out except how to do full house and 2 two pair. Currently I scan through all 127 ASCII values, then if the code detects that in the input from the user it will increment a counter. But when I try to write an if statement, if the counter is equal to 3 and 2 it will not work. Any ideas why this is not working, or is there another way to do this?
    here is my code to help you out, this is not my whole code as it is quite large, just the bits that i feel will help you help me. Thank you very much for your time in reading this.
    Code:
    #include<stdio.h>int main(void)
    {   int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int count3 = {0};
        int i;
        int temp;
        int j;
        int difference; 
        char array[5];
        char array1[5];
        int picturedifference;
        int difference1;
        int suitdifference;
        int suitcount = {0};
        int count4 = {0};
        n = 5;
         
         
        for (c = 0; c < n; c++) 
            scanf("%c%c ", &array1[c], &array[c]);
            getchar();
    
           
         for (i = 0; i < n; i++) {
            count1[array[i]]++;                    //if array[i] is N, then count1[N]++//
        }                                          //these two for statement adds to the counts//
        for (i = 0; i < n; i++) {
            count2[array1[i]]++;}   
    
    
    
    
    
    
        {      for (i = 0; i < 127; i++)             
                  { 
                  if ( count2[i] == 4 && suitcount!= 4 )
                  printf("Four of a kind. Your score is 50.\n");
                  
                  if( count2[i] == 3 && count2[i] == 2 && suitcount != 4)
                  printf("Full House. Your score is 25. \n");
                  
                  if ( count2[i] == 3 && suitcount != 4  )
                  printf("Three of a kind. Your score is 5.\n");
                  
                  if ( count2[i] == 2 && suitcount != 4  )
                  printf("Pair. Your score is 2.\n");
                 
                  }
                  
                  
                   
                 
        }
       
        return 0;
        }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Your code is confusing. You use 'count1' and 'count2' to increment the respective values in the hand, and then later you test only 'count2' and 'suitcount' which is zeroed out.

    Of course count2[i] cannot be equal to 3 AND 2 at the same time which is what
    Code:
    if( count2[i] ==3 && count2[i] ==2... )
    says.

    You have unnecessary braces blocking your last for loop.

    And you're working with the strangest deck of cards I've ever heard of if they include all 127 ASCII values, even the unprintable ones.
    stahta01 likes this.

  3. #3
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    sorry i tried to cut out some of code so it wasnt as long, but I will just post the whole thing so its easier for you to read.
    Code:
    #include<stdio.h>int main(void)
    {   int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int count3 = {0};
        int i;
        int temp;
        int j;
        int difference; 
        char array[5];
        char array1[5];
        int picturedifference;
        int difference1;
        int suitdifference;
        int suitcount = {0};
        int count4 = {0};
        n = 5;
         
         
        for (c = 0; c < n; c++) 
            scanf("%c%c ", &array1[c], &array[c]);
            getchar();
       for(i=0;i<n;i++)
      
         {        if ( array1[i] == '2'){
                  printf("Two of ");
                  }else if( array1[i] == '3'){
                  printf("Three of ");     
                  }else if( array1[i] == '4'){
                  printf("Four "); 
                  }else if( array1[i] == '5'){
                  printf("Five of "); 
                  }else if( array1[i] == '6'){
                  printf("Six of "); 
                  }else if( array1[i] == '7'){
                  printf("Seven of "); 
                  }else if( array1[i] == '8'){
                  printf("Eight of "); 
                  }else if( array1[i] == '9'){
                  printf("Nine of "); 
                  }else if( array1[i] == '0'){
                  printf("Ten of "); 
                  }else if( array1[i] == 'J'){
                  printf("Jack of "); 
                  }else if( array1[i] == 'Q'){
                  printf("Queen of "); 
                  }else if( array1[i] == 'K'){
                  printf("King of "); 
                  }else if( array1[i] == 'A'){
                  printf("Ace of ");}
                  
                  if ( array[i] == 'C'){
                  printf("Clubs\n");
                  }else if( array[i] == 'D'){
                  printf("Diamonds\n");
                  }else if( array[i] == 'H'){
                  printf("Hearts\n");
                  }else if( array[i] == 'S'){
                  printf("Spades\n");}
                  
         }
                
         for (i = 0; i < n; i++) {
            count1[array[i]]++;                    //if array[i] is N, then count1[N]++//
        }                                          //these two for statement adds to the counts//
        for (i = 0; i < n; i++) {
            count2[array1[i]]++;}   
    ////////////////////////////////////////////////////////////////////////////////
    /////////////////////bubble sort///////////////////////////////////////////////    
       
       for (i=0;i<n; i++)
     {
        for (i=0; i<4; i++)
       {
          for (j=i+1; j<5; j++)
          {
             if(array1[i] > array1[j]) 
             {
                temp = array1[i];
                array1[i] = array1[j];
                array1[j] = temp;
             }
           }   
        }
      }
     ///////////////////////////////////////////////////////////////////////////////////////
     ///////////////////////suit counter/////////////////////////////////////////////////// 
      for(i=0;i<n-1;i++) 
        {
        suitdifference=(array[i+1]-array[i]);
           {if ( suitdifference == 0)   
            suitcount++;
            
           }
        }
    ///////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////Picture code straight code//////////////////////////////////////
       for(i=0;i<n-1;i++) 
        {
        picturedifference=(array1[i+4]-array1[i]);
           
           {if ( picturedifference == 33)   
            count4++;
            
           }
        }
                     {if (suitcount == 4 && count4 == 1) 
                      printf("Royal Flush. Your score is 100.\n"); 
                     } 
    ////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////// striaght code///////////////////////////////////
         for(i=0;i<n-1;i++) 
        {
        difference=(array1[i+1]-array1[i]);
           {if ( difference == 1)   
            count3++;
            
           }
        } 
                  
                      {if ( suitcount == 4 && count3 == 4) 
                      printf("Straight flush. Your score is 75.\n");
                      }
                      {if ( suitcount != 4 && count3 == 4)
                      printf("Straight. Your score is 15.\n"); 
                      }
                      {if ( suitcount != 4 && count4 == 1)
                      printf("Straight. Your scoreeeee is 15.\n");
                      }
                      {if ( suitcount == 4 && count4 != 1 && count3 != 4 )
                      printf("Flush. Your Score is 20.\n");
                      }
        ////////////////////////////////////////////////////////////////////////////////////
        /////////////////////hand identiy codes/////////////////////////////////////////////
       
        {      for (i = 0; i < 127; i++)             
                  { 
                  if ( count2[i] == 4 && suitcount!= 4 )
                  printf("Four of a kind. Your score is 50.\n");
                  
                  if( count2[i] == 3 && count2[i] == 2 && suitcount != 4)
                  printf("Full House. Your score is 25. \n");
                  
                  if ( count2[i] == 3 && suitcount != 4  )
                  printf("Three of a kind. Your score is 5.\n");
                  
                  if ( count2[i] == 2 && suitcount != 4  )
                  printf("Pair. Your score is 2.\n");
                 
                  }
                  
                  
                   
                 
        }
       
        return 0;
        }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    He didn't only say that your code is confusing. He said also about line 142 in your latest post
    stahta01 likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,552
    I suggest learning to use functions. Functions in C - Cprogramming.com
    I also suggest re-reading post #2.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with if and else statements
    By kburyanek in forum C Programming
    Replies: 7
    Last Post: 09-25-2009, 01:28 AM
  2. If statements
    By DJ_Steve in forum C Programming
    Replies: 12
    Last Post: 08-13-2009, 12:43 PM
  3. If statements
    By hebali in forum C Programming
    Replies: 5
    Last Post: 03-03-2008, 01:50 PM
  4. If else statements
    By Timbo8 in forum C++ Programming
    Replies: 15
    Last Post: 01-03-2007, 06:08 PM
  5. Help with IF statements
    By boontune in forum C++ Programming
    Replies: 12
    Last Post: 01-22-2003, 07:26 AM

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