gpt4 book ai didi

c++ - 变量周围的堆栈已损坏

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:31:05 25 4
gpt4 key购买 nike

我有一个循环直到输入正确的 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;,并删除数组运算符,就可以了。

关于c++ - 变量周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9590825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com