This is from code chef. ( easy level, poker)
The program runs correctly for all the sample input I have tried. However the chef told me it's wrong. Can someone please help me?
Can it be wrong because I have used getchar() in the main() ??
I have used it so as to remove '\n' from input stream. How to accomplish it more efficiently??
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
void find (char cards[])
{
int suit_count, flag, i, j;
int rank_count=1;
char rank_cards[5], suit[5];
// finding suit_count
suit_count=1;
i=4;
suit[0]=cards[1];
while(i<14)
{
flag=0;
for(j=0;j<suit_count;++j)
if(suit[j]==cards[i])
{ flag=1;
break;
}
if(flag==0)
{ suit_count++;
suit[suit_count]=cards[i];
}
i=i+3;
}
//finding rank_count
rank_cards[0]=cards[0];
i=3;
while(i<13)
{ flag=1;
for(j=0;j<rank_count;++j)
if(cards[i]==rank_cards[j])
{ flag=0;
break;
}
if(flag==1)
{ ++rank_count;
rank_cards[rank_count-1]=cards[i];
}
i=i+3;
}
int rank_occur[rank_count], occur;
int rank_value[rank_count];
//occurances of different ranks
for (i=0;i<rank_count;++i)
{ occur=0;
for (j=0;j<14;j=j+3)
{ if (cards[j]==rank_cards[i])
++occur;
}
rank_occur[i]=occur;
}
//value of different ranks
char rank[13]={'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
rank[0]='A';
for (i=0;i<rank_count;++i)
{ for(j=0;j<13;++j)
{
if(rank_cards[i]==rank[j])
{ rank_value[i]=j+1;
break;
}
}
}
//sorting in ascending order
if(rank_count>1)
{ for(i=0;i<rank_count-1;++i)
{ flag=rank_value[i];
for(j=i+1;j<rank_count;++j)
if(rank_value[j]<flag)
{ flag=rank_value[j];
rank_value[j]=rank_value[i];
rank_value[i]=flag;
}
}
}
//for(i=0;i<rank_count;++i)
//cout<<rank_value[i]<<" ";
//cout<<"\n";
//assigning name to conditions
if (suit_count==1)
{ if (rank_count==5)
if (rank_value[0]==1)
{ if (rank_value[1]==10)
cout<<"royal flush\n";
if( rank_value[4]==5)
cout<<"straight flush\n";
}
else
{ if (rank_value[4]-rank_value[0]==4)
cout<<"straight flush\n";
else
cout<<"flush\n";
}
}
else
switch(rank_count)
{
case 2:
if (rank_occur[0]== 1||rank_occur[0]==4)
cout<<"four of a kind\n";
else
cout<<"full house\n";
break;
case 3:
if (rank_occur[0]==2||rank_occur[1]==2)
cout<<"two pairs\n";
else
cout<<"three of a kind\n";
break;
case 4:
cout<<"pair\n";
break;
case 5:
if (rank_value[0]==1)
{ if (rank_value[4]==5||rank_value[1]==10)
cout<<"straight\n";
else
cout<<"high card\n";
}
else if (rank_value[4]-rank_value[0]==5)
cout<<"straight\n";
else
cout<<"high card\n";
}
}
int main()
{
char cards[15];
int turns;
char ch;
// printf ("enter turns\n");
//freopen ("ab","r",stdin);
// freopen ("as","w",stdout);
scanf("%d", &turns);
// fflush(stdin);
ch=getchar();
//cout<<"The character is "<<ch<<" this\n";
//cout<<turns<<"\n";
while (turns>0)
{
scanf("%[^\n]", cards);
//fflush(stdin);
ch=getchar();
//cout<<" the ch is "<<ch<<" this\n";
// puts (cards);
//cout<<"\n";
find (cards);
--turns;
}
return 0;
}