How is my simple if-else statement unreachable code?

1.5k views Asked by At

Honestly am shocked I am getting an error. I'm a junior CS major and I can't get this simple program to work. Clion says these two lines are unreachable, yet my test cases seem to work.

Code:

#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;
}

Test Cases:

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

If this code is so unreachable than how come my program reached it?

1

There are 1 answers

0
user6f6e65 On BEST ANSWER

There may be a few problems wrong with CLion

This is the one which caught my attention:

You check whether the string is equal to a chat array this may get resolved at runtime but the code checker doesn’t like it. Try using :

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

Other than that I have no idea...

It may not have predicted the change to the variables, try using the volatile keyword? This may help... That is still a bug.