Home > front end >  Why is my C function skipping my IF statements?
Why is my C function skipping my IF statements?

Time:12-08

So, let me preface that I am still learning C and would appreciate some guidance on what I am doing wrong.

My prompt is to write a function that continuously prompts a user for a valid age (between 0 and 100) and the function must only return the age to the caller of the function after a valid age is retrieved. AND For each function, you must declare the function using a function prototype before main and then define the function after main.

Here is my code,

#include <iostream>
using namespace std;

  int num;
  bool valid;
  int validateInput()
{
    cout << "Pick a number between 0 and 100" << endl;
    cin >> num;
    while(bool valid = false)
    {
        if(num <= 0) 
        {
          cout << "Error: Number is invalid because it's too low" << endl;
          bool valid = false;
          return 0;
          }
        else if (num >= 100)
        {
          cout << "Error: Number is invalid because it's too high" << endl;
          bool valid = false;
          return 0;
          }
        else
        {
          cout << "You are " << num << " years old." << endl;
          bool valid = true;
          return 0;
        }
      }
}
int main() 
{ 
validateInput();
return 0;
}

So I am trying to get my program to work but the IF statements keep getting skipped. Am I misunderstanding something? Any and all help is very much appreciated.

EDIT: Thank you to Arvin and iammilind for your help. I was able to fix the code so my while loop condition would actually trigger, moved my cout statements into the loop and so I wouldn't get infinite output.

My final working code looked like this.

#include <iostream>
using namespace std;

  int num;
  bool valid = false;
  int validateInput()
{
    while(!valid)
    {
        cout << "Pick a number between 0 and 100" << endl;
        cin >> num;
      
        if(num <= 0) 
        {
          cout << "Error: Number is invalid because it's too low" << endl << endl;
          bool valid = false;
          }
        else if (num >= 100)
        {
          cout << "Error: Number is invalid because it's too high" << endl << endl;
          bool valid = false;
          }
        else
        {
          cout << "You are " << num << " years old." << endl;
          bool valid = true;
          return 0;
        }
      }
}
int main() 
{ 
validateInput();
return 0;
}

CodePudding user response:

You give a false value to the while loop that makes the while loop doesn't start the loop.

do this instead:

bool valid = false;
while (!valid){ // while valid is still false, do the loop
 // your code here
}

Further explanation: You're currently using sentinel-controller loop reference: https://simplecplusplus.wordpress.com/tag/sentinel-controlled-loop/

In order for while loop to start running, you've to provide the "true" condition to the while, and it will start looping until the condition turn out to false.

Programming tips for you: next time, you've to see the larger picture of your code every time you're trying to debugging. If you're sure the if-else code is running and have no problem, you have to enlarge your investigation for the bug, maybe it's the while loop that didn't work, not if-else. And if the while loop seems having no problem, maybe its the function or the caller of the function

CodePudding user response:

while(bool valid = false) never allows the execution to enter the loop. Hence the if conditions are never called.

Use it as below:

while(valid == false) { // see '==' sign. `while(not valid)` is also fine
//   ... your 'if' conditions
}

Actually you are creating a locally scope variable within while() by having a bool before it. So bool valid hides bool ::valid (declared outside).

Also, once the loop ends, you may want to reset it to false again. Otherwise this function will never be able to used again!
Using globals (bool valid) for such functionality is a bad design.

  • Related