my code is near correct except at the endan error occurs saying: Debugging: Run-
ID: 441690 • Letter: M
Question
my code is near correct except at the endan error occurs saying: Debugging: Run-Time Check Failure #2 - Stack around the variable 'used' was corrupted. I can't figure the error? appreciate if someone takes a look around and see what am doing wrong..and corrects me..am using visual c plus plus 2010 /* Anbessa Mesquitta Program 8.6 This program simulates a lottery */ #include#include using namespace std; int main() { int lottery[5]; // ARRAY 1 int num[5]; // ARRAY 2 int lottoNum=0; int i,j; srand(time(0)); // the randomization func code!!!! bool used[5]; for(i=0; i < 10; i++ ) used[i]=false; for(i=0; i<5; i++) { lottery[i] = rand()%9; if(used[lottery[i]] == true) i--; else used[lottery[i]]=true; } for(i=0; i < 10; i++ ) used[i] = false; cout<<"Enter your numbers: "; for(i=0; i<5; i++) { cout<< "Enter number" << i+1 <<": "; cin >> num[i]; if(num[i]<0 ||num[i]>9 || used[num[i]]==true) { cout<<"Invalid entry " ; i--; } else used[num[i]]=true; } cout<<" The Lottery Numbers: "; for(i=0;i<5;i++) cout<<lottery[i]<<" "; cout<<" Your Numbers are: "; for(i=0; i<5; i++) cout << num[i] <<" "; for(i=0; i<5; i++) for(j=0; j<5; j++) if(num[i] == lottery[j]) lottoNum++; cout<<" You match "<< lottoNum <<"numbers: "; if(lottoNum==5) cout<<"You are the Grand Prizewinner "; else // else a message for try once again cout<<"Sorry - you didn't win "; //system("pause"); return 0; }Explanation / Answer
FOLLOW THIS THIS WILL HELP YOU his problem is caused when you try to write too much data to a particular memory address. Typical causes are writing more to a string buffer than you have room for. ie void myfun() { char mybuf[10]; strcpy(mybuf, "This is definitely more than 10 characters long, it will also cause a Run-Time Check"); } Another cause of this is when you are using memset/ZeroMemory to initialise a structure or array with the wrong size. struct MyStruct { int var; }; void myfun2() { MyStruct ms; ZeroMemory(&ms;, 20); //since MyStruct is only one variable in the struct this will cause problems } A third possible problem is if you are accidentaly moving a pointer. void myfun3() { int a; int*b = &a; a++; *a = 20; } Of course, these problems are not so easy as above to find out, but I hope this will get you on the right track. But this happens and will break at the end of the function that the stack is corrupted in while it is returning. So the best place to look would be in the function that your LoggerThread variable is in.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.