1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

String Issue, reads incorrect if cut out printf statement

Discussion in 'C' started by manjotpahwa, Jun 6, 2010.

  1. manjotpahwa

    manjotpahwa New Member

    Joined:
    Apr 14, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I have a very weird problem. I was trying to solve this poker problem in which I took a string as input and then worked on it to classify it into rank (array of integers) and suit (array of characters). The input was like this:
    AH KH QH JH TH
    5S 3C QD 7D 2H
    etc.
    To separate into rank and suit I have done:
    Code:
    getchar(); fflush(stdin); 
    	for(i=0;i<20;i++) str[i]='\0';
    	for(i=0;i<6;i++) suit[i]='\0';
    	scanf("%[^\n]s", str);
    	i=0; hand=0; a=k=q=j=t=n9=n8=n7=n6=n5=n4=n3=n2=s=d=c=h=0; flag1=1;
    	for(i=0;i<5;i++) rank[i]=0; i=0;
    	while(i<strlen(str))
    	{
    	if(str[i]=='A')
    	rank[i/3]=14;
    	else if(str[i]=='K')
    	rank[i/3]=13;
    	else if(str[i]=='Q')
    	rank[i/3]=12;
    	else if(str[i]=='J')
    	rank[i/3]=11;
    	else if(str[i]=='T')
    	rank[i/3]=10;
    	else
    	rank[i/3]=str[i]-'0';
    	suit[i/3]=str[i+1];
    	i+=3;
    	} 
    	i=0; hand=0; a=k=q=j=t=n9=n8=n7=n6=n5=n4=n3=n2=s=d=c=h=0; flag1=1;
    	suit[5]='\0'; printf("%s\n", suit);
    If I remove this last printf statement, the answer comes out to be wrong otherwise it is always correct. How is this statement influencing my answer?

    Can you tell me an alternative to this and the reason for this? Thanks, everyone.
     
    Last edited by a moderator: Jun 6, 2010
  2. CrazyGeek

    CrazyGeek New Member

    Joined:
    Jun 6, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    can you explain more clearly the phrase 'answer comes out to be wrong' . What's the answer here? I think if you remove the last printf statement, there will be nothing displayed on the screen. That statement simply prints out a string. No use nothing displayed
     
  3. manjotpahwa

    manjotpahwa New Member

    Joined:
    Apr 14, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    The answer should be telling me which Poker hand it is. My full code is as follows:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void bubbles(int rank[]);
    int arr[5];
    int main()
    {
    int test, i, hand=0,rank[5], a,k,q,j,t,n9,n8,n7,n6,n5,n4,n3,n2,s,d,c,h, flag1=0;
    /*
    HAND INDEX:
    1 royal flush
    2 straight flush
    3 four of a kind
    4 full house
    5 flush
    6 straight
    7 three of a kind
    8 two pairs
    9 pair
    10 highest card
    */
    char suit[6], str[20];
    scanf("%d", &test);
    while(test--)
    	{getchar(); fflush(stdin); 
    	for(i=0;i<20;i++) str[i]='\0';
    	for(i=0;i<6;i++) suit[i]='\0';
    	scanf("%[^\n]s", str);
    	i=0; hand=0; a=k=q=j=t=n9=n8=n7=n6=n5=n4=n3=n2=s=d=c=h=0; flag1=1;
    	for(i=0;i<5;i++) rank[i]=0; i=0;
    	while(i<strlen(str))
    	{
    	if(str[i]=='A')
    	rank[i/3]=14;
    	else if(str[i]=='K')
    	rank[i/3]=13;
    	else if(str[i]=='Q')
    	rank[i/3]=12;
    	else if(str[i]=='J')
    	rank[i/3]=11;
    	else if(str[i]=='T')
    	rank[i/3]=10;
    	else
    	rank[i/3]=str[i]-'0';
    	suit[i/3]=str[i+1];
    	i+=3;
    	} 
    	i=0; hand=0; a=k=q=j=t=n9=n8=n7=n6=n5=n4=n3=n2=s=d=c=h=0; flag1=1;
    	suit[5]='\0'; printf("%s\n", suit);
    	bubbles(rank);
    
    	for(i=0;i<5;i++)
    	{
    	switch(suit[i])
    		{
    		case 'S': s++; break;
    		case 'D': d++; break;
    		case 'C': c++; break;
    		case 'H': h++; break;
    		}
    	switch(rank[i])
    		{
    		case 2: n2++; break;
    		case 3: n3++; break;
    		case 4: n4++; break;
    		case 5: n5++; break;
    		case 6: n6++; break;
    		case 7: n7++; break;
    		case 8: n8++; break;
    		case 9: n9++; break;
    		case 10: t++; break;
    		case 11: j++; break;
    		case 12: q++; break;
    		case 13: k++; break;
    		case 14: a++; break;
    		}
    	} 
    	if(a==4||k==4||q==4||j==4||t==4||n9==4||n8==4||n7==4||n6==4||n5==4||n4==4||n3==4||n2==4) hand=3;
    	if(arr[0]==10 && arr[1]==11 && arr[2]==12 && arr[3]==13 && arr[4]==14) {if(s==5 || d==5 || c==5 || h==5) hand=1; else hand=6; flag1=1;}
    	for(i=0;i<4;i++)
    	{if(arr[i+1]!=arr[i]+1) {flag1=0; break;} else flag1=1;}//in order
    	if(arr[4]==14 && arr[0]==2 && arr[1]==3 && arr[2]==4 && arr[4]==5) flag1=1; //printf("%d\n", flag1);
    	if(flag1)
    	{
    	if(s==5 || d==5 || c==5 || h==5) {if(hand!=1) hand=2;}
    	else if(hand!=1 && hand!=2) hand=6;
    	}
    	if(s==5 || d==5 || c==5 || h==5)//same suit-can be 1, 2
    	{if((hand!=1) && (hand!=2) && hand==0) hand=5;}
    	if(a==3||k==3||q==3||j==3||t==3||n9==3||n8==3||n7==3||n6==3||n5==3||n4==3||n3==3||n2==3)
    		{if(a==2||k==2||q==2||j==2||t==2||n9==2||n8==2||n7==2||n6==2||n5==2||n4==2||n3==2||n2==2) hand=4;
    		else hand=7; }
    	if(a==2||k==2||q==2||j==2||t==2||n9==2||n8==2||n7==2||n6==2||n5==2||n4==2||n3==2||n2==2)//either 1 or 2 pairs
    	{
    	if(arr[0]==arr[1]) {if((arr[2]==arr[3]) || (arr[3]==arr[4])) if(hand!=4) hand=8;}
    	else if(arr[1]==arr[2]) {if(arr[3]==arr[4]) if(hand!=4) hand=8;}
    	else if(hand!=4) hand=9;
    	}
    	if(hand!=1 && hand!=2 && hand!=3 && hand!=4 && hand!=5 && hand!=6 && hand!=7 && hand!=8 && hand!=9) hand=10;
    //	printf("%d\n", hand);
    	switch(hand)
    		{
    		case 1: printf("royal flush\n"); break;
    		case 2: printf("straight flush\n"); break;
    		case 3: printf("four of a kind\n"); break;
    		case 4: printf("full house\n"); break;
    		case 5: printf("flush\n"); break;
    		case 6: printf("straight\n"); break;
    		case 7: printf("three of a kind\n"); break;
    		case 8: printf("two pairs\n"); break;
    		case 9: printf("pair\n"); break;
    		case 10: printf("highest card\n"); break;
    		}
    }
    }//main ends
    void bubbles(int rank[])
    {
    int temp, i, j, k=0;
    for(i==0;i<5;i++)
    arr[k++] = rank[i];
    for(i=0;i<k;i++)
    	{
    	for(j=0;j<i;j++)
    		{
    		if(arr[j]>arr[i])
    			{
    			temp = arr[j];
    			arr[j] = arr[i];
    			arr[i] = temp;
    			}//if ends
    		}//for end
    	} //for ends
    }
    
    If I input
    AH KH QH JH TH
    The answer should be royal flush. But if I remove the printf("%s\n", suit) statement, the answer becomes flush. I know myself that it should not affect the answer but it is. You can copy paste the whole code and try out yourself.
     
    Last edited by a moderator: Jun 6, 2010
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    arr[4]==14 && arr[0]==2 && arr[1]==3 && arr[2]==4 && arr[4]==5 will never evaluate TRUE.
    This code is very difficult to read, I'm not surprised you have trouble debugging it yourself. Use labels instead of constants (ideally const int, but #define will do).

    for(i==0;i<5;i++) Remember == compares, = assigns. i==0 is *NOT* the same as i=0.

    I'd say this last one is probably where your problem comes from. Odd behaviour like that is typical of a memory corruption, and since i contains garbage and isn't initialised to zero, arr = temp; could be writing to absolutely anywhere in memory.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Oh and format it properly as well. This will also make it a lot easier to debug. You hardly ever see experts writing code that looks as awful as yours, not just because we aren't as 1337 as you, but because we understand the point of good formatting. Your code would make a reasonable contender for the IOCCC.
     

Share This Page