簡體   English   中英

我的簡單 if-else 語句無法訪問代碼如何?

[英]How is my simple if-else statement unreachable code?

老實說我很震驚我收到了一個錯誤。 我是一名初級 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 關鍵字? 這可能會有所幫助......這仍然是一個錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM