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;
}

Recommended Answers

All 2 Replies

Without seeing your sample input files and, more importantly, the original problem/program specification, and not knowing what the program is supposed to do, it's hard to offer any advice. What does Code Chef say is wrong (or does he/it say elaborate? I'm unfamiliar with Code Chef)? A link to the problem would be useful, or cut and paste it here if it's not too long.

i am sorry. i should have done that.
here's the link :http://www.codechef.com/problems/POKER/
The chef doesn't elaborate. It just shows wrong answer which means maybe i am getting wrong answer for some input.
sample input is in the question. I have tried some more sample inputs which i am getting correct:
10
KH JH QH TH AH
KH JH QH TH 9H
9H 9C 9S 9D AH
9H 9C 9S 5D 5H
KH 2H QH JH AH
KH JH QH TH AS
5C 6D 5S 3D 5D
5C 6D 5S 3D 6C
5C 4D 5S 3D 6C
KH JH QH 9S AH

Results:
royal flush
straight flush
four of a kind
full house
flush
straight
three of a kind
two pairs
pair
high card
Please help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.