我在编译和执行下面的代码时遇到如下问题: 在 while 循环中,x1 保持它的值。一旦输入“ok”并且 while 循环结束,x1 就会失去它的值并重置为 0。
你能告诉我这是什么原因吗?
附言。我知道 vector 和表格,但不使用它们 atm。 使用dev c++编译
int main(){
int x1 = 10; int x2=10,x3=10;
int y1=60,y2=20,y3=20;
string res="";
cout << "config x pos ";
cin >> res;
while(res != "ok"){
cin >> res;
x1= atoi(res.c_str());
moveTo(x1, y1);
cout << endl;
}
cout << x1;
cout << "config y pos ";
cin >> res;
while(res != "ok"){
cin >> res;
y1= atoi(res.c_str());
moveTo(x1, y1);
cout << endl;
cout << "x " << x1 << endl;
}
}
最佳答案
一旦你输入“ok”并打破循环, 函数 atoi(res.c_str()) 将零返回到变量 x1 中;
关于c++值在循环后丢失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24922019/