Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im a beginner and I am trying to write a function that checks if the number is in a given interval. If it is, the program is supposed to return true, else return false. For some reason the output is always "Process returned 1", no matter which values I assign to the variables. Does return do something else than I think or what is the problem?

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int IsInRange(int number, int high, int low){
    if (low <= number && number <= high){
        return true;
    }
    else{
        return false;
    }
}

int main()
{
    int x, low = 15, high = 25, number = 2;
    return IsInRange(low, high, number);

    return 0;
}
Posted
Updated 4-Nov-20 20:31pm
v4

The condition you use does not produce the results you expect:
low <= number <= high

Actually evaluates as either
(low <= number) <= high
Or
low <= (number <= high)
Since the result of a <= b will always be zero or nonzero only (and most likely 0 or 1) the result isn't what you expected.
Try this instead:
if (low <= number && number <= high)
 
Share this answer
 
Comments
Member 14955513 4-Nov-20 16:36pm    
if ((low <= number) && (number <= high))

Modified it like this but now it only return true when number == low.

"Process returned 1 (0*1)"
OriginalGriff 4-Nov-20 17:09pm    
That's because you are passing the values in the wrong order ... check the names in the call list against the parameters in the function.
When you call a function, it's parameters are passed by order into the variables named in the function declaration regardless of the names in the "outside world".

int IsInRange(int number, int high, int low){
...
return IsInRange(low, high, number);

You need:

int IsInRange(int number, int high, int low){
...
return IsInRange(number, high, low);
C++
int IsInRange(int number, int high, int low){
    if (low <= number <= high){ // this is not c
        return true;
    }
    else{
        return false;
    }
}

In most programming languages, a double condition is written as 2 simple conditions with logical operation in between.
C++
int IsInRange(int number, int high, int low){
    if (low <= number && number <= high){
        return true;
    }
    else{
        return false;
    }
}

which means: (low <= number) and (number <= high)
[Update]
By the way, position of arguments matters !
C++
int IsInRange(int number, int high, int low)
...
return IsInRange(low, high, number);
 
Share this answer
 
v2
Comments
Member 14955513 4-Nov-20 16:43pm    
Changed it this way but now the output is only true when number == low
Patrice T 4-Nov-20 16:55pm    
Show your new code.
Member 14955513 4-Nov-20 16:59pm    
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int IsInRange(int number, int high, int low){
if ((low <= number) && (number <= high)){
return true;
}
else{
return false;
}


}

int main()
{
int x, low = 20, high = 30, number = 21;
return IsInRange(low, high, number);



return 0;
}
Patrice T 4-Nov-20 17:00pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
I prefer to write code in very simple terms. I would do it like this :
C++
int IsInRange( int number, int high, int low )
{
    if( number < low )
       return false;
    else if( number > high )
       return false;
    else
       return true;
}
and you can test it :
C++
int main()
{
    const char * str;
    int result;
    int low = 15;
    int high = 25;
    int n;
    for( int n = 10; n <= 30; ++n )
    {
        result = IsInRange( n, low, high );
        if( result )
           str = "true";
        else
           str = "false";
        printf( "when n is %d  result is %s\n", n, str );
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900