15,746,759 members
1.00/5 (1 vote)
See more:
i am trying to solve euler projects poker hands. Problem 54 - Project Euler[^]

my code:

```#include<iostream>
#include<fstream>
std::ifstream infile("poker.txt");
char a[10000][2];
int b=0;
using namespace std;
{
int i;
for(i=0;i<=10000;i++)
{

infile>>a[i][0]>>a[i][1];
}
}

class play
{

public:
int high1,high2;
char p;
void display()
{
for(int i=b;i<b+5;i++)
cout<<a[i][0]<<a[i][1];
}
void check( int b)
{

int c[10];
int n=0,temp[2];
int str=0,fl=0,four=0,three=0,two=0;
for(int i=b;i<(5+b);i++)
{

if(isdigit(a[i][0]))
c[i]=(int)a[i][0]-'0';
else if(a[i][0]=='T')
c[i]=10;
else if(a[i][0]=='J')
c[i]=11;
else if(a[i][0]=='Q')
c[i]=12;
else if(a[i][0]=='K')
c[i]=13;
else if(a[i][0]=='A')
c[i]=14;
else
cout<<"invalid";

}
for(int k=b;k<5+b;k++)
{
for(int j=k+1+b;j<5+b;j++)
{
if (c[k]<c[j])
{
int temp=c[k];
c[k]=c[j];
c[j]=temp;
}
}
}
if((c[0+b]==c[1+b]-1) &&(c[1+b]==c[2+b]-1) && (c[2+b]==c[3+b]-1) && (c[3+b]==c[4+b]-1) )
{
p='E';
str=1;
high1=c[0+b];

}
if((a[0+b][0+b]==a[0+b][1+b]) && (a[0+b][0+b]==a[0+b][2+b]) && (a[0+b][0+b]==a[0+b][3+b]) && (a[0+b][0+b]==a[0+b][4+b]))
{
p='F';
fl=1;
high1=c[0+b];
high2=c[1+b];

}
for(int l=0+b;l<5+b;l++)
{
if(c[l]==c[l+1])
{
two=two+1;
temp[n]=c[l];
n++;
}

}
for(int m=b;m<3+b;m++)
{
if (c[m]==c[m+2])
{
two=two-2;
three=three+1;
high1=c[m];
if(two==1)
{
if(temp[0]==c[m])
high2=temp[1];
else
high2=temp[0];
}
else
{
high2=c[m+3];
}

}

}
for(int q=b;q<2+b;q++)
{
if(c[q]==c[q+3])
{
two=two-3;
three=three-2;
four=four+1;
high1=c[q];
if(q==0)
high2=c[4];
else
high2=c[0];
}
}
if(two==1)
p='B';
if (two==2)
p='C';
if(three==1)
{
if (two==1)
p='G';
else
p='D';
}
if(four==1)
p='H';
if(str==1 && fl==1)
p='I';

if(str==1 && fl==1 && c[4]>9)
p='J';
if(str!=1 && fl!=1 && two!=1 && three!=1 && four!=1)
{
p='A';
high1=c[0];
high2=c[1];
}

}

};
int main()
{

int counter=0,anti=0;
play player1,player2;
b=0;
while(b<=10000)
{
player1.check(b);
b=b+5;
player2.check(b);
b=b+5;
if((int)player1.p>(int)player2.p)
{
counter++;
}

else if((int)player1.p==(int)player2.p)
{
if(player1.high1>player2.high1)
counter++;

if(player1.high1==player2.high1)
{
if(player1.high2>player2.high2)
counter++;

}

}

}
anti++;
cout<<b;
cout<<a[100][1];
cout<<counter;
return 0;
}```

What I have tried:

i used another variable to do looping instead of b and other loops(for & do).

i think i have problem with indentation but i can't figure it out.
Posted
Updated 23-Jun-18 16:30pm
jeron1 26-Jun-18 15:44pm
```char a[10000][2];
int b=0;
using namespace std;
{
int i;
for(i=0;i<=10000;i++)
{

infile>>a[i][0]>>a[i][1];
}
}
```

The largest index into the array a is a[9999][1], you for loop in the read() method tries to access a[10000][1], this will probably result in an access violation.

## Solution 1

Quote:
My while loop inside main() does not work more than once?

On first look, I see no reason for looping only once.
add this line inside the loop to have a trace of the looping.
C++
`cout<<b;`

-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 75 Richard MacCutchan 60 Dave Kreskowiak 40 Graeme_Grant 25 Member 16085996 9
 OriginalGriff 2,083 Richard MacCutchan 1,418 merano99 609 Rick York 455 Graeme_Grant 415

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900