### possible problem with pointers

In this code I have to check whether the number is triangle, square, pentagonal etc. (see project euler 61 problem).
My function checks if a number is one of these and then sets a pointer to point to a certain number, which shows what kind the number is.
The problem is that function never returns 8. I checked if the formula is right, it IS right, I also checked how the function works, it appears to work well and should return 8. I am completely lost. Please help.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` `````` #include #include using namespace std; int check(int number, int * p) { if ((..formula..) and (p[0]==0)) {p[0] = 3; return 3;} if ((..formula..) and (p[1]==0)) {p[1] =4; return 4;} if ((..formula..) and (p[2]==0)) {p[2]=5; return 5;} if ((..formula..) and (p[3]==0)) {p[3]=6; return 6;} if ((..formula..) and (p[4]==0)) {p[4]=7; return 7;} if ((..formula..) and (p[5]==0)) {p[5]=8; return 8;} return 0; } int main() { static int *p; p=new int; for (int gh=0; gh<=5; gh++) {p[gh]=0;} for (int i=1000; i<=9999; i++) { int i1=check(i,p); if (i1!=0) { cout << i << " " << i1 << endl; // HOW COME THIS NEVER COUTS OCTAGONAL (8) NUMBERS for (int b=i%100 * 100; b<=i%100 * 100+99; b++) { int b1=check(b,p); if (b1!=0) { for (int c=b%100*100; c<=b%100 * 100+99;c++) { int c1=check(c,p); if (c1!=0) { for (int d=c%100*100; d<=c%100*100+99; d++) { int d1=check(d,p); if (d1!=0) { for (int e=d%100*100; e<=d%100*100+99; e++) { int e1=check(e,p); if (e1!=0) { for (int f=e%100*100; f<=e%100*100+99; f++) { int f1=check(f,p); if ((f1!=0) and (f%100==i/100)) { cout << i+b+c+d+e+f << endl; p[f1-3]=0;} } p[e1-3]=0;} } p[d1-3]=0;} } p[c1-3]=0;} } p[b1-3]=0;} } p[i1-3]=0;} } }``````
Last edited on