我有一个循环直到输入正确的y,n答案的函数,但是在选择的最后我得到了错误:
Time Check Failure #2 - Stack around the variable 'YESNO' was corrupted.
香港专业教育学院看过谷歌,并不能真正找到任何错误的答案,此错误我的代码如下:
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.
}
}
}
谢谢
胡拉汉
最佳答案
发生这种情况是因为YESNO
是一个字符数组,并且cin >> YESNO;
正在向该数组写入NULL终止符。
将YESNO的声明更改为char YESNO;
,并删除数组运算符,一切顺利。