Variable wrap was damaged

I have a function that loops until y, n is the correct answer, but at the end of the selection I get an error:

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

ive looked at google and could not find any relivent answers to this error, my code looks like this:

  void Mesh_equations(float a,float b,float c,float d,float e,float f){ char YESNO[1]; //define variables. int loop=0; //loop set to zero. while(loop==0){ //while loop initiated whilst loop is equal to zero. cout <<"\nDo you want to display your coefficients for the mesh equations...(y/n)?"; cin>>YESNO; //prompt and cin. if ( YESNO[0] == 'Y' || YESNO[0] == 'y'){ //if cin is 'Y' or 'y' system("CLS"); cout<<"Loop One:\n(" <<a <<")" <<"Ix + (" <<b <<")" <<"Iy = (" <<e <<")" <<endl <<"Loop Two:\n(" <<c <<")" <<"Ix + (" <<d <<")" <<"Iy = (" <<f <<")" <<endl<<endl <<setw(5)<<" Where ;\n" <<setw(5)<<"A ="<<a<<endl <<setw(5)<<"B ="<<b<<endl <<setw(5)<<"C ="<<c<<endl <<setw(5)<<"D ="<<d<<endl <<setw(5)<<"E ="<<e<<endl ////set the field width to 5 characters. <<setw(5)<<"F ="<<f <<endl<<endl; //display. loop=1; //loop is 1, while loop passed. system("pause"); } else if( YESNO[0] == 'N' || YESNO[0] == 'n'){ //if 'N' or 'n', while loop passed. loop=1; } else{ //if neither y or n is enterred input must be incorrect. cout <<"bad answer, try again\n"; Beep (600,100); loop=0; //loop is zero, while loop continues. } } } 

Thanks Houlahan.

+4
source share
3 answers

This is because YESNO is an array of characters, and cin >> YESNO; writes a NULL terminator to this array.

Change the declaration of YESNO to char YESNO; and delete the array operators and you will go well.

+4
source

If an array element is assigned outside the array boundary, then at runtime it displays the message "Stack around the variable has been corrupted." Therefore, to solve this problem, make sure that the size of the allocated array and the value assigned to it.

+2
source

Make YESNO larger, that is, 10 characters instead of 1. The zero character is placed after the last valid position in the array, and this is the cause of the error.

+1
source

Source: https://habr.com/ru/post/1400087/


All Articles