[英]Why doesn't this order of code work properly?
So I've been working on part of this program for a few hours straight. 因此,我已经连续几个小时从事该程序的一部分。 I have a few lines of code that have stumped me for over an hour now.
我有几行代码现在困扰了我一个多小时。 It just doesn't make sense.
只是没有意义。 I read the lines over and over again and think in my head what it should do, but it's not doing it!
我一遍又一遍地阅读台词,并在脑海中思考该怎么办,但它没有这样做! It's probably some stupid mistake that I keep overlooking (usually happens to me..) or it could be from lack of sleep/constantly working on it for the last few hours.
这可能是我一直忽略的一个愚蠢错误(通常发生在我身上..),或者可能是由于过去几个小时缺乏睡眠/一直在努力工作。
The program is quite long, but the chunk of code I'm having issues with is only a few lines - so I'll only post the whole thing if needed. 该程序很长,但是我遇到的代码块只有几行-因此,我只会在需要时发布整个内容。
Ok, so here is a quick run down of what this should be doing: 好的,这是此操作的简要概述:
I have a single vector, filled with with manually set strings (19, each being 79 characters long). 我有一个向量,里面充满了手动设置的字符串(19个字符串,每个字符串长79个字符)。 This 1 dimensional vector will be treated as a 2d vector - as i'm trying to keep it basic.
这个一维向量将被视为2d向量-因为我正在尝试使其保持基本状态。 So this vector of strings, if displayed 1 string per line, essentially creates a grid with rectangle sections of '.'
因此,如果此字符串向量每行显示1个字符串,则实际上会创建一个矩形截面为'。'的网格。 and paths of '#' connecting the rectangle sections.
和连接矩形部分的“#”路径。 It should get an x and y coordinate, passed to the function.
它应该获得传递给函数的x和y坐标。 It loops through like a 2d vector and finds the location.
它像2D向量一样循环遍历并找到位置。 It checks to see if the char at the location is NOT '.'
它检查该位置的字符是否不是'。'。 or '#' else, it changes the char to an X to mark its position.
或“#”,它将char更改为X以标记其位置。 I set the testing up with a valid x,y coordinate that is the location of a '.'
我使用有效的x,y坐标(即“'”的位置)设置了测试。 - The problem is right after the line where i cout "match found::: " When the program runs, it displays the value of vLevel[i][j] = '.'
-问题就在我提示“找到匹配项:::”的行之后。程序运行时,它显示vLevel [i] [j] ='。 But for some reason, it doesn't go to the else statement where the function is called 4 more times and I put the cout statement.
但是由于某种原因,它不会转到else语句中,在该语句中该函数又被调用了4次,而我放置了cout语句。 Heres what the grid looks like.
这是网格的外观。 The white spaces are spaces.. each line is a string in the vector:
白色空格是空格..每行都是向量中的字符串:
.......
....... .......
.......##....... ..........................
....... ....... ..........................
....... .......................... ..........
....... ..........................#####..........
.......................... ..........
.......................... #
.......................... #
# #
# #
........ # ............ .......
........ # ............ .......
........ # ............ .... .......
........###############............ .... .......
........ ............####....
........ ............ ....
Heres the section of code I'm dealing with: 这是我要处理的代码部分:
void markPosition(vector<string> & vLevel, int x, int y){
cout << "x: " << x <<endl;
cout << "y: " << y <<endl;
bool doubleCheck = true; //extra bool I added for testing purposes
for( int i = 0; i < vLevel.size(); i++ ){
for( int j = 0; j < vLevel[i].size(); j++ ){
//displays if x and/or y are out of range
if (doubleCheck){
if ((x < 0) || (x > vLevel[i].size())){
cout << "X is out of range!" << endl;
doubleCheck = false;
}
if ((y < 0) || (y > vLevel.size())){
cout << "Y is out of range!" << endl;
doubleCheck = false;
}
}
else {
return;
}
if ((j == x) && (i == y)){
//////THIS IS THE PROBLEM LOL
cout << "match found::: " << vLevel[i][j] << endl;
if ((vLevel[i][j] != '.') || (vLevel[i][j] != '#')){
cout << "Located in the blankness" << endl;
return;
}
else {
cout << "I don't get reached!!??" << endl;
vLevel[i][j] = 'X';
markPosition(vLevel,i-1,j);
markPosition(vLevel,i+1,j);
markPosition(vLevel,i,j+1);
markPosition(vLevel,i,j-1);
}
}
}
}
}
The spacing got messed up when pasting it, so i tried to clean it up a bit. 粘贴时间距弄乱了,所以我尝试将其清理一下。 I don't get why it skips the else.
我不明白为什么它会跳过其他情况。 Like I said, I'm sure I'll feel dumb once this is resolved.
就像我说的那样,我敢肯定一旦解决,我就会感到愚蠢。 I need sleep lol.
我需要睡觉哈哈。
Let me know if you need more info. 让我知道您是否需要更多信息。 Thanks in advanced!
提前致谢!
Look at: 看着:
if ((vLevel[i][j] != '.') || (vLevel[i][j] != '#'))
This will always evaluate to true
. 这将始终评估为
true
。 Think about it. 想一想。
(I think you meant if((!=)&&(!=))
or if(!((==)||(==)))
.) (我认为您的意思是
if((!=)&&(!=))
或if(!((==)||(==)))
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.