i have a function that loops untill the correct y,n answer is typed but at the end of the choice i get the error:
Time Check Failure #2 - Stack around the variable 'YESNO' was corrupted.
ive had a look on google and cant realy find any relivent answers to this error my code is as below:
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.
This is happening because
YESNOis an array of characters, andcin >> YESNO;is writing a NULL terminator to this array.Change the declaration of YESNO to
char YESNO;, and remove the array operators, and you’re good to go.