"Code will never be executed"

Feb 27, 2016 at 12:12am
Hi everyone, I've made a library for input validation, and overloaded the function, input_prot for different scenarios. The problem is that whenever I compile it, it says "code will never be executed" for cout<<prompt (line 5 in my sample); this shows up at the same line for ALL the functions.

Here's one function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double input_prot (string prompt, double upper, string err,  double lower){
    bool good = false;
    double input;
    while (good= false) { //keep asking till the input is valid
        cout<<prompt;
        cin>>input;
        if (cin.fail()){ //if it isn't even a double
            cout<<"invalid input\n";
            cin.clear() ;
            cin.ignore();
        }
        else if (lower >input ||input>upper){ //double but out of bounds 
            cout<<err<<endl ;
            cin.clear();
            cin.ignore();
	
        }
        else {
            good=true;
        }
    }
        return input;
}//closes function 
Last edited on Feb 27, 2016 at 12:18am
Feb 27, 2016 at 12:18am
while (good= false) = is assignment == is equality
Last edited on Feb 27, 2016 at 12:19am
Feb 27, 2016 at 12:18am
What if you swap operands on line 4? while (false = good) {
Feb 27, 2016 at 2:51am
A new problem came up after I followed Yanson's advice. It worked, and eventually compiled error free, but now the functions don't work. Once I set the arguments, and run the driver, even when the input is within bounds, it displays the error message and takes me back to the prompt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double input_prot (string prompt, double upper, string err,  double lower){
    bool good = false;
    double input;
    while (good== false) {
        cout<<prompt;
        cin>>input;
        if (cin.fail()){ //if it isn't even a double
            cout<<"invalid input\n";
            cin.clear() ;
            cin.ignore();
        }
        else if (lower >input ||input>upper){ //double but out of bounds 
            cout<<err<<endl ;
            cin.clear();
            cin.ignore();
	
        }
        else {
            good=true;
        }
    }
        return input;
}//closes function 
Last edited on Feb 27, 2016 at 3:04am
Feb 27, 2016 at 10:20am
Perhaps a different order in the logic? After all, we have faith in the user to type the right thing on first try (have we?):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double
input_prot( string prompt, double upper, string err,  double lower )
{
  double input;
  while (true) {
    cout << prompt;
    if ( cin >> input ) {
      if ( /*input is within limits*/ ) {
        return input;
      }
      else {
        // input not in range. Stream is ok.
      }
    }
    else {
      // invalid input, the stream is in error state
    }
  }
}



PS. My previous post was a hint about style.
Equality should be symmetric, i.e. if a==b, then b==a too.
Therefore, when you test foo==true, you can as well write true==foo.
In the latter form the left side is const and typing an assignment by mistake becomes a hard error.

A good compiler does warn when it sees an assignment within conditional expression. Does yours?

PS2. Another style detail. On custom types that have relational operators, canonical approach is to write < and == for them, and let the standard library templates to form the other operators for them. Therefore, > could be a bit more expensive than <. In case of double there is no difference, but being systematic is a good thing.
Last edited on Feb 27, 2016 at 10:24am
Feb 29, 2016 at 11:59pm
I reordered the logic, and it works fine now. I even got it to reprompt the user if their input was invalid. Funny enough I didn't change the order of logic for the other functions, but they worked just fine.

Thank you for the style critique. I'm still figuring out the specifics on what makes good code vs bad code, and this helped.
Mar 1, 2016 at 12:05am
Don't compare bools to 'true' or 'false'. For example, just say while(!good) instead of while (good == false) or while ( false == good )
Topic archived. No new replies allowed.