[英]Infinite while loop with a std::ofstream C++
好吧,我有将该代码保存在txt文件中的代码:
std::ofstream output("mapa.txt");
for(int y=0;y<worldHeight;y++) {
for(int x=0;x<worldWidth;x++) {
output<<scene[x][y];
if(x<(worldWidth-1)){output<<",";}
}
if(y<(worldHeight-1)){output<<std::endl;}
}
我想将其集成到while循环中:
std::ofstream output("mapa.txt");
while (true) {
if (yPos == topOfTheWorld) {
scene[xPos][yPos] = 2;
} else if (yPos >= topOfTheWorld) {
scene[xPos][yPos] = 1;
} else if(yPos < topOfTheWorld) {
scene[xPos][yPos] = 0;
} else {
scene[xPos][yPos] = 0;
}
output<<scene[xPos][yPos] << ",";
//if(xPos<(worldWidth-1)){output<<",";}
//if(yPos<(worldHeight-1)){output<<std::endl;}
yPos++;
if(yPos>worldHeight) {
output<<std::endl;
slope = random(5)-2;
if(topOfTheWorld<(worldHeight/6)) {
slope = 1;
} else if(topOfTheWorld>(worldHeight-10)) {
slope = -1;
}
topOfTheWorld += slope;
yPos = 0;
xPos++;
}
if (xPos>=worldWidth) {
break;
}
}
但是由于某种原因,“ while”循环变为无限...:/
我能做什么?
代码看起来不错。 但是我不确定scene
是否分配正确。 您有yPos>worldHeight
并在xPos>=worldWidth
,这表明您不确定如何比较这些值。 我怀疑您至少其中之一错了。 这可以轻松地在场景变量之外编写并覆盖yPos或xPos。
尝试更改为ypos>=worldHeight
,看看是否ypos>=worldHeight
。 如果没有检查,请为scene
分配多少空间,如果看起来还可以,请尝试使其更大一些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.