Monday, May 24, 2010

C++ run-time failure. please help?

I got this message using visual studi c++:


Run-Time Check Failure #2 - Stack around the variable 'temp' was corrupted.





here is my function:


void highscore(int score)


{


int indexes=0,temp[5];


ifstream infile;


infile.open("filesout.txt");


if (infile.is_open())


{


while (!infile.eof())


{


infile%26gt;%26gt;temp[indexes++];


}


}


else


cerr %26lt;%26lt;"error occured when opening file";


infile. close();





int a=1,k=0,s;


temp[5]=score;


while (k%26lt;6)


{


for (int m=a;m%26lt;6;m++)


{


if (temp[m]%26gt;temp[k])


{


s=temp[k];


temp[k]=temp[m];


temp[m]=s;


}


}


k=k+1;


a=a+1;


}


for (int y=0;y%26lt;5;y++)


cout %26lt;%26lt;temp[y]%26lt;%26lt; endl;





ofstream outfile;


outfile.open("filesout.txt");


for (int x=0;x%26lt;6;x++)


outfile %26lt;%26lt;temp[x]%26lt;%26lt;endl;


outfile.close();


}





what the function do is put the input extracted from a file "filesout.txt" and reorder from highest to lowest


can anyone check the error please. thank you

C++ run-time failure. please help?
One obvious error is the dimension on temp and the loop variables. Note how your dimension on temp is 5; however, you specifically reference temp[5] in your "for(int m=a" loop. All references into your temp array should only be 0 to 4 with a dimension of 5.





i.e. any reference to "var%26lt;6" then "temp[var]" will throw an error.


No comments:

Post a Comment