Tuesday, 15 February 2011

c++ - STD::COUT affecting functionality of program -



c++ - STD::COUT affecting functionality of program -

so in middle of wring function much larger project. messy @ moment, experiencing unusual behaviour.

the portion of code behaviour originates shown below:

while(rw < (starty + 3)) { while(cl < (startx + 3)) { if(board[rw][cl] == '*'){ char poss[9] = {'1','2','3','4','5','6','7','8','9'}; unsigned int totalcount = 0; unsigned int possibilities = 0; unsigned int possiblerow = 0; unsigned int possiblecol = 0; unsigned int lastcheck = 0; for(unsigned int alpha = 0; alpha < 9; alpha++){ if (testgrid('r', poss[alpha], rw) == true) { totalcount++; } if (testgrid('c', poss[alpha], cl) == true) { totalcount++; } if (testgrid('s', poss[alpha], 0) == true) { totalcount++; } if(totalcount == 0) { possibilities++; } totalcount = 0; } std::cout << possibilities << " possibilities" << std::endl; if(possibilities == 1) { possiblerow = rw; possiblecol = cl; for(unsigned int alpha = 0; alpha < 9; alpha++){ if (testgrid('r', poss[alpha], possiblerow) == true) { lastcheck++; } if (testgrid('c', poss[alpha], possiblecol) == true) { lastcheck++; } if (testgrid('s', poss[alpha], 0) == true) { lastcheck++; } if(lastcheck == 0) { board[rw][cl] = poss[alpha]; } lastcheck = 0; } } possibilities = 0; } cl++; } rw++; cl = startx; }

the output of entire programme solves 1 little square of sudoku grid (infant stages). if comment out line: std::cout << possibilities << " possibilities" << std::endl; output different (other obvious lack of output. shown below:

obvious undesired behaviour. can explain it?

pastebin: the code pastebin: the input file

i believe (can't test, unfortunately, have no c++ compiler on machine) issue coming line 133, never give value count. if stack variable left alone, go on 1, , test pass every subsequent time on line 149. cout creates several stackframes on top of current stack, overwriting value in memory , changing results. alter line 133 like

unsigned int count = 0;

note have count variable in scope when declared; legal, want point out in case intent using one, , not making new one. if want utilize 1 instead, remove line 133.

declaring primitive , using when may not have given value recipe odd behavior. have no thought what's in memory given variable, value theoretically arbitrary. it's possible 1 begin with, what's happening here, since 1 left on in memory previous calls function.

for posterity, in case pastebin (god forbid) dies, troubling section:

unsigned int starty = 0; unsigned int startx = 0; unsigned int count; //line 133 starty = (num / 3) * 3; startx = (num % 3) * 3; unsigned int rw = starty; unsigned int cl = startx; while(rw < (starty + 3)) { while(cl < (startx + 3)) { if(board[rw][cl] == ident){ count = 1; } cl++; } rw++; cl = startx; } if(count == 1){ //line 149 homecoming true; } else { homecoming false; }

c++ cout

No comments:

Post a Comment