我的简单if-else语句是如何无法访问的代码

How is my simple if-else statement unreachable code?

本文关键字:何无法 访问 代码 简单 if-else 语句 我的      更新时间:2023-10-16

老实说,我收到一个错误,我很震惊。我是CS专业的大三学生,我无法让这个简单的程序发挥作用。Clion说这两条线是无法到达的,但我的测试用例似乎可以工作。

代码:

#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "";
while(s != "|") {
int val1 = 0;
int val2 = 0;
cin >> val1;
cin >> val2;
if(val1 == val2) {
cout << "the numbers are equal.n";
} else {
cout << "the smaller value is: " << min(val1, val2) << 'n'; // Says these two 
cout << "the larger value is: " << max(val1, val2) << 'n'; // lines are unreachable
}
cin >> s;
}
return 0;
}

测试用例:

3 3
the numbers are equal.
f
4 5
the smaller value is: 4
the larger value is: 5
|
Process finished with exit code 0

如果这个代码是如此的不可访问,我的程序为什么会访问它?

CLion 可能有一些问题

这个引起了我的注意:

你检查字符串是否等于聊天数组,这可能会在运行时得到解决,但代码检查器不喜欢。尝试使用:

char s;
while(s!='|') {...}

除此之外,我不知道。。。

它可能没有预测到变量的变化,尝试使用volatile关键字?这可能会有所帮助。。。这仍然是一个bug。