help getting fixing loop problem with my code

This is a discussion on help getting fixing loop problem with my code within the C Programming forums, part of the General Programming Boards category; I want the 'counter' loop to go through twice, each time reading in 15 characters from the standard input, then ...

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

    help getting fixing loop problem with my code

    I want the 'counter' loop to go through twice, each time reading in 15 characters from the standard input, then going through the evalutor
    Code:
    #include <stdio.h>
    #include <ctype.h>
                         
    int main (void)
    {
    char inputtedhand[64];
    int NumC=0, NumD=0, NumS=0, NumH=0;
     
    int handcount=0;
    int tempvar;
    char cardvalue[4];
                         
    int i=0, n=0, m=0;
    int index=0, counter=0;
     
    int z=0; 
      {   
       while((scanf("%c", &inputtedhand[z])>0))
       {
           inputtedhand[z]=toupper(inputtedhand[z]);
           z++;
       }
      }
    while(counter++<=1)
    {
    scanf( "%15c", &inputtedhand );                   
    printf( "Found %s \n", inputtedhand );
    
    for(i==0; i<15; i+=3)
    {  m=(i+1);
       n=(m+1);
       scanf("%c", &inputtedhand[i]);
       scanf("%c", &inputtedhand[m]);
       scanf("%c", &inputtedhand[n]);         
             
          if (inputtedhand[i]==50){
                    printf( "The two of "); tempvar=2;
                    /* The the first value if a 2, "The two of" will be dsplayed at the output, and the number 2 will be stored as a temporary variable for later */
          }else if (inputtedhand[i]==51){
                    printf( "The three of "); tempvar=3;               
          }else if (inputtedhand[i]==52){             
                    printf( "The four of "); tempvar=4;               
          }else if (inputtedhand[i]==53){               
                    printf("The five of "); tempvar=5;               
          }else if ( inputtedhand[i]==54){               
                    printf("The six of "); tempvar=6;               
          }else if ( inputtedhand[i]==55){
                     printf("The seven of "); tempvar=7;               
          }else if ( inputtedhand[i]==56){
                    printf("The eight of "); tempvar=8;               
          }else if (inputtedhand[i]==57){
                    printf("The nine of "); tempvar=9;               
          }else if ( inputtedhand[i]==48){
                     printf("The ten of "); tempvar=10;               
          }else if (inputtedhand[i]==74){
                     printf("The Jack of "); tempvar=11;               
          }else if (inputtedhand[i]==81){
                    printf("The Queen of "); tempvar=12;
          }else if (inputtedhand[i]==75){
                     printf("The King of "); tempvar=13;               
          }else if (inputtedhand[i]==65){
                     printf("The Ace of "); tempvar=14;               
          }else{               
                     printf("%c",inputtedhand[i]); printf("is not recognised\n ");
          }             
                           
          if (inputtedhand[m]==67){
                     printf("Clubs\n");  NumC++;
                     /* This will complete the name for the first card, eg. "The two of Clubs" and counts the number of clubs there has been, this will be useful then looking for flushes */
          }else if (inputtedhand[m]==68){
                     printf("Diamonds\n"); NumD++;                   
          }else if (inputtedhand[m]==104){
                     printf("Hearts\n"); NumH++;
          }else if (inputtedhand[m]==83){   
                     printf("Spades\n"); NumS++;                   
          }else{                 
                     printf(" SUIT ");  printf("%c",inputtedhand[m]);  printf(" not recognised\n");
          }
    cardvalue[index]=tempvar;
    index++;
    }
    
    int flush=0;
             
       if((NumH==5) || (NumC==5) || (NumD==5) || (NumS==5)){
            flush=1;
          }else{
            flush=0;
          }
         
    int outside, inside, first, second;
    {
    for(outside=4; outside>0; outside--)
        /* A loop that counts from 4 down to 0 as the algorithm passes through each term in the array */
       {
       for(inside=0; inside<outside; inside++)
          {
             first= cardvalue[inside];
             second= cardvalue[inside+1];
                if (first>second)
                    {
                       cardvalue[inside]=second;
                       cardvalue[inside+1]=first;
                       /* This is what makes the swap between the terms, if it is needed, the largest term should end up in position 4 of the array */
                    }
           }
        }
    }
    printf("%d", cardvalue[0]);
    printf(" (0)\n");
    printf("%d", cardvalue[1]);
    printf(" (1)\n");
    printf("%d", cardvalue[2]);
    printf(" (2)\n");
    printf("%d", cardvalue[3]);
    printf(" (3)\n");
    printf("%d", cardvalue[4]);
    printf(" (4)\n");
    int pair=0, pair1=0, pair2=0, pair3=0, pair4=0;
         
       if(cardvalue[0]==cardvalue[1]){
          pair1++; pair++;
       }if(cardvalue[1]==cardvalue[2]){
          pair2++; pair++;
       }if(cardvalue[2]==cardvalue[3]){
          pair3++; pair++;
       }if(cardvalue[3]==cardvalue[4]){
          pair4++; pair++;   
       }
    int straight=0, straightflush=0, royalflush=0, fourkind=0, fullhouse=0, threekind=0, twopair=0, highcard=1;
     
    if ((cardvalue[2]==cardvalue[1]+1) && cardvalue[3]==(cardvalue[2]+1) && cardvalue[1]==(cardvalue[0]+1) && (cardvalue[4]==(cardvalue[3]+1) || (cardvalue[0]==2 && cardvalue[4]==14)))
    {
       if((NumH==5) || (NumD==5) || (NumS==5) || (NumC==5))
       {
         if(cardvalue[0]==10)
         {
           royalflush++;
         }else{
           straightflush++;
         }
       }else{
         straight++;
       }
    }else{
      if ((pair1==1 && pair2==1 && pair4==1) || (pair1==1 && pair3==1 && pair4==1)){
           /*This stage tests every posibility for getting a full house, if the criteria is met, the the number the counter for fullhouse is at, is incremented by one*/
               fullhouse++;
           }else if ((pair1 == 1 && pair2 == 1 && pair3 == 1) || (pair4 == 1 && pair3 == 1 && pair2 == 1)){
              fourkind++;
           }else if ((pair1 == 1 && pair2 == 1) || (pair2 == 1 && pair3 == 1) || (pair3 == 1 && pair4 == 1)) {
              threekind++;
           }else if ((pair1 == 1 && pair3 == 1) || (pair1 == 1 && pair3 == 1) || (pair1 == 1 && pair4 == 1) || (pair2 == 1 && pair4 == 1)){
              twopair++;
           }else{
              if((NumH == 5) || (NumC == 5) || (NumD == 5) || (NumS == 5)){
                 flush++;
              }else{
                 highcard++;
              }
           }
    }    
      
    int scorehand[2];
    while(1)
       {
       if ((royalflush)>=1){
          /* When we see a yroyal flush we increment the count of a royal flush by 1, so here, by comparing to its initial value, 0. We can see if that had was a royal flush */
                printf("\n The hand is a Royal Flush!!\n");
                scorehand[counter]=(135+cardvalue[4]);      break;
       }else if((straightflush)>=1){         
                printf("\n The hand is the Straight Flush!\n");
                scorehand[counter]=(120+cardvalue[4]);break;       
       }else if((fourkind)>=1){         
                printf("\n The hand is a Four of a Kind!\n");
                scorehand[counter]=(105+cardvalue[4]);break;         
       }else if((fullhouse)>=1){     
                printf("\n The hand is a Full House\n"); 
                scorehand[counter]=(90+cardvalue[4]); break;         
       }else if((flush)>=1){         
                printf("\n The hand is a Flush\n"); 
                scorehand[counter]=(75+cardvalue[4]); break;         
       }else if((straight)>=1){         
                printf("\n The hand is a Straight\n");
                scorehand[counter]=(60+cardvalue[4]); break;         
       }else if((threekind)>=1){       
                printf("\n The hand is  a Three of a Kind\n"); 
                scorehand[counter]=(45+cardvalue[4]);break;         
       }else if((twopair)>=1){         
                printf("\n The hand is a Two Pair\n"); 
                scorehand[counter]=(30+cardvalue[4]); break;         
       }else if((pair)>=1){         
                printf("\nThe hand is a pair\n");
                scorehand[counter]=(15+cardvalue[4]); break;         
       }else if((highcard)>=1){         
                printf("\n The hand is a High Card\n");
                scorehand[counter]=cardvalue[4];      break;         
       }else{       
                printf("\n The hand scores 0 (this should not happen)");
                /* This is to ensure the above has been executed as planned, if this comes uop then there is an error */
       }
      }
     printf("%d", scorehand[1]);
     printf("%d", scorehand[2]);
      
      {
      if (scorehand[1]>scorehand[2]){
        puts("Player 1 is the winner!");
      }
      else if (scorehand[2]>scorehand[1]){
        puts("Player 2 is the winner!");
      }
      else if(scorehand[1]==scorehand[2]){
        puts("It's a draw :(");
      }
      else puts("this shouldnt happen");
    }  
      }  
    }
    Thanks for any help i get

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    A development process
    The first thing I would suggest you do is break your 200+ line main into several functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
       while((scanf("%c", &inputtedhand[z])>0))
       {
           inputtedhand[z]=toupper(inputtedhand[z]);
           z++;
       }
    are you aware that this loop will run until the program stops with an error ?

    BTW what is your question ?

    Kurt

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    my question is: if i put ' 2s 3s 4s 5s 7d
    2D 3D 4D 5D 7S' at the standard input why dont i see the text description of the cards
    i thought the toupper while loop worked, as line 26-27 showed the expected output

    this is what i get at the output:
    Found 2S 3S 4S 5S 7D
    2D 3D 4D 5D 7S
    The two of Spades
    The three of Spades
    The four of Spades
    The five of Spades
    The seven of Diamonds
    2 (0)
    3 (1)
    4 (2)
    5 (3)
    7 (4)

    The hand is a High Card
    70Player 1 is the winner!

    Found 2S 3S 4S 5S 7D
    2D 3D 4D 5D 7S
    2 (0)
    3 (1)
    4 (2)
    5 (3)
    7 (4)

    The hand is a High Card
    77It's a draw

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Any ideas?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Like I said, start small, use functions, test often.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void inputHand ( char hand[] ) {
      printf("Enter a hand -> ");
      fflush(stdout);
      scanf("%[^\n]",hand);
    }
    
    void printHand ( char hand[] ) {
      int i;
      for ( i = 0; i < strlen(hand); i += 3 ) {
        printf("Rank=%c, Suit=%c\n", hand[i], hand[i+1] );
      }
    }
    
    int main ( void ) {
      char  hand[20];
      inputHand(hand);
      printHand(hand);
      return 0;
    }
    
    
    $ gcc foo.c
    $ ./a.out 
    Enter a hand -> 2s 3s 4s 5s 7d
    Rank=2, Suit=s
    Rank=3, Suit=s
    Rank=4, Suit=s
    Rank=5, Suit=s
    Rank=7, Suit=d
    From this, you expand printHand() to print a wordy description.

    Then you might have say a function to sort the hand into some kind of order (rank and suit) to make the various "winning hands" easier to find.

    Next I would suggest a whole set of functions
    - isRoyalFlush
    - isStraghtFlush
    - isFourOfAKind
    - isFullHouse
    - etc etc
    Where each named function tests for ONE specific kind of winning hand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    Okaayy, just your code doesnt do 2 hands either, if i remove line24 I can make the program function as a 1 hand evaluator..

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by BenBusby View Post
    I want the 'counter' loop to go through twice,
    Use a simple for-loop if you know in advance how many times you want to loop:

    Code:
    for(i==0; i<15; i+=3)
    You should get a warning here.

    And please indent your code properly.

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    I dont understand what you mean by indent your code properly?

    Why should i get a warning there?

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by BenBusby View Post
    I dont understand what you mean by indent your code properly?
    You use different levels of indentation for lines which belong to the same level and you are putting more than one statement on one line which renders your code rather hard to read IMHO.

    E.g. this
    Code:
    scanf("%c", &inputtedhand[n]);
          
       if (inputtedhand[i]==50){
                 printf( "The two of "); tempvar=2;
                 /* The the first value if a 2, "The two of" will be dsplayed at the output, and the number 2 will be stored as a temporary variable for later */
       }else if (inputtedhand[i]==51){
    should better look like
    Code:
    scanf("%c", &inputtedhand[n]);
    
    if (inputtedhand[i] == 50) {
        /*  The the first value if a 2, "The two of" will be dsplayed at the  output, and the number 2 will be stored as a temporary variable for  later */
        printf( "The two of ");
        tempvar=2;      
    } else if (inputtedhand[i] == 51) {

    Why should i get a warning there?
    What's the meaning of the == operator?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help fixing for loop
    By mgravier in forum C Programming
    Replies: 1
    Last Post: 03-21-2013, 04:03 PM
  2. Help fixing code
    By return0 in forum C Programming
    Replies: 10
    Last Post: 10-23-2012, 10:31 PM
  3. I need some help fixing this code.
    By Mremokid1 in forum C Programming
    Replies: 4
    Last Post: 11-06-2011, 10:00 PM
  4. help in fixing this code..
    By transgalactic2 in forum C Programming
    Replies: 20
    Last Post: 12-13-2008, 05:38 AM
  5. help!!! code needs fixing
    By hockey123 in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2008, 06:02 PM

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