2

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?

VLL
  • 9,634
  • 1
  • 29
  • 54
Coder117
  • 801
  • 2
  • 9
  • 22
  • 2
    Seems like a bug in CLion to me. – Zereges Nov 02 '18 at 06:58
  • CLion is wrong then. – Anmol Singh Jaggi Nov 02 '18 at 06:58
  • 1
    Ok thank you because this is one of those times where I question my intelligence lol. – Coder117 Nov 02 '18 at 06:59
  • 1
    It seems CLion's analysis doesn't take non-const references into account. You can replace the input statements with a reference and an assignment through the reference and it gives the same result. – chris Nov 02 '18 at 07:07
  • 1
    Actually, after some more searching, this looks most relevant and still not marked as fixed: https://youtrack.jetbrains.com/issue/CPP-13000 – chris Nov 02 '18 at 07:42
  • It looks like CLion is interpreting `>> val1` as a bitshift instead of an IOstream operation. If you had written `255>>val1; 255 >> val2` then Clion would have been correct that the 2 lines are unreachable. But C++ has operator overloading. – MSalters Nov 02 '18 at 13:28

1 Answers1

1

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.

user6f6e65
  • 146
  • 9