Home > Enterprise >  C less than operator doesn't work properly after few executions?
C less than operator doesn't work properly after few executions?

Time:05-30

Hi im beginning to learn C , just today i tried to learn boolean operator and if-else statment this is the code:

int main(){
//if-else statement
int a, b;
bool result = (a < b);

std::cout << "input number 1 : ";
std::cin >> a;
std::cout << "input number 2 : ";
std::cin >> b;
std::cout << std::boolalpha << result <<std::endl;
if(result == true){
    std::cout << a << " is less than " << b << std::endl;
}
if(!(result == true)){
    std::cout << a << " is NOT less than " << b << std::endl;
}

return 0;}

these are the results after a few execution. Initially the results were fine, but then after a couple times it went wrong. Does anybody know what is the cause of that?

CodePudding user response:

Your mistake is that you compare the two variables and save the result before you get any proper values for these variables. In other words, you compare uninitialized variables a and b which have undefined values:

bool result = (a < b);

...And only then you get the values:

std::cin >> a;

You should do as the following:

// ...
int a, b;
std::cout << "input number 1 : ";
std::cin >> a;
std::cout << "input number 2 : ";
std::cin >> b;

bool result = a < b;
// ...

CodePudding user response:

You expect result to be evaluate a == b when you use it later. Instead bool result = (a < b); initializes result with (a < b) once and its value does not change afterwards. As neither a nor b are initialized when you declare result your code has undefined behavior.

You can make result a function object to make it work as you expected by using a lambda expression. However, to call it you'll have to add ():

int main(){
    //if-else statement
    int a = 0;
    int b = 0;
    auto result = [&](){ return a < b; };

    std::cout << "input number 1 : ";
    std::cin >> a;
    std::cout << "input number 2 : ";
    std::cin >> b;
    std::cout << std::boolalpha << result() <<std::endl;
    if(result()) {
        std::cout << a << " is less than " << b << std::endl;
    } else {
        std::cout << a << " is NOT less than " << b << std::endl;
    }
}

You should always initialize variables. Using the value of uninitialized variables is undefined behavior and can happen easily (as in your code) when you do not initialize variables. Instead of if (condition) {} if (!condition){} you can use if (condition) {} else {}. Instead of if (result() == true) you can write if (result()). And return 0; is implicit in main, you need not write it.

CodePudding user response:

You overall code as it should be.

Explanations in the comments:

int main() {
  //if-else statement
  int a, b;

  std::cout << "input number 1 : ";
  std::cin >> a;
  std::cout << "input number 2 : ";
  std::cin >> b;

  bool result = (a < b);   // put this here, because now a and b have
                           // determined values

  std::cout << std::boolalpha << result << std::endl;

  if (result) {            // or simple if (a < b) and drop result alltogether
    std::cout << a << " is less than " << b << std::endl;
  }
  else  {                  // no need for testing the opposite of result
    std::cout << a << " is NOT less than " << b << std::endl;
  }

  return 0;
}
  • Related