PC33
PC33

Reputation: 1

How to check if a number is within an interval without using the boolean operators in C++

I'm just getting started with programming and this is my first post on this site, hopefully the start of a long and productive journey!

I'm studying C++ from the Deitel's book and one of the exercises of the control flow 1 (chapter 4 - if, if...else, while) is asking me, among other things, to input a number and check that it's no smaller than 1 and no greater than 20 (in this case an error message has to be displayed to the user until a valid number is entered). In order to make it work I had to use the || operator as follows:

while (number < 1 || number > 20)
{
   cout << "Wrong number, insert a valid number";
   cin >> number;
}

Problem is, the book has yet to introduce boolean operators (||, && ...)!

So my question is, is it possible to operate such a control on a value without using the "or" operator and only using the if, if...else and while, nested if necessary? Thank you in advance.

Upvotes: 0

Views: 1310

Answers (5)

PC33
PC33

Reputation: 1

Thank you all for your help. The solution to the issue that was bothering me is the little expression that Peter wrote in the comments, as it is the only thing suggested that's already been explained in the few chapters that i've read (the implicit boolean operators with 0 as "false" and 1 or greater as "true"). I'm sorry I couldn't be more specific with my question but english isn't my first language. That's also the reason why I started studying on the Deitel book: being programming a complex subject I thought it'd be better to begin with something in italian before moving to an english text.

Upvotes: 0

lastchance
lastchance

Reputation: 6480

Well, you could always use std::clamp()

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
   int low = 10, high = 20;
   int number;

   while ( true )
   {
      cout << "Enter a number between " << low << " and " << high << " inclusive : ";
      cin >> number;
      if ( clamp( number, low, high ) == number ) break;
      cout << "Invalid number\n\n";
   }
}

Upvotes: 2

Henrique Bucher
Henrique Bucher

Reputation: 4474

You could possibly use arithmetic for this. But then it is a trick

do
{
   cout << "Insert a valid number";
   cin >> number;
}
while ( (number-1)*(20-number)<0 );

Another approach is to test in separate

while ( true ) 
{
   cout << "Insert a valid number";
   cin >> number;
   if ( number < 1 ) continue;
   if ( number > 20 ) continue;
   break;
}

Upvotes: 1

John3136
John3136

Reputation: 29266

On approach would be nested ifs.

bool ok = false;
if (number >= 1) {
    if (number <= 20) {
        ok = true;
    }
}
if (!ok) {
    // print error
}

So you assume the number is bad. If it's < 1 then it skips the first if and the number stays bad. If the number is > 1 then it tries the second condition and only sets ok to true is that is true i.e. both conditions are true. You can wrap this in the necessary while look yourself:

bool ok = false;
while(!ok)
{
    // input number
    // check number
    // optional error
}

Upvotes: 1

Tomek
Tomek

Reputation: 4659

You can write a function already (see main function) so write one which checks if the number is within an interval and returns 1 if it is and 0 otherwise. This can be done with using only if and return statements in a function. And put a function call in while statement.

Upvotes: 0

Related Questions