Using operators for a beginner...

Hi. I just started programming with c++ this morning and i m following the tutorial on this site. I'm trying to write a program that wants me to input an integer between 100 and 200. And then it will be shown on the screen. But i want that when i input an integer below 100, the program will show 100 and when i input upper than 200 the program will show 200. i can solve just one of these two condition by creating a variable and using operators like:

int output = (in<100 ? 100 : in);

or

int output = (in>200 ? 200 : in);

but i dont know how to provide both conditions. I am looking for a way to overcome this problem without using control structures and need your help.

Thanks.
Last edited on
PROTIP: You can use another ?: operator inside any of the subexpressions.

There's no difference between the versions, but I assume you meant <100 in one of them.
Thanks Helios. The problem has been solved with your tip.

output = (in>200 ? 200 : (in<100 ? 100 : in))

Your assumption is right too. At my first post i made a silly mistake but I corrected it. :)
Or assuming "in" is of type "int":

1
2
3
#include <algorithm>

int output = std::min( 200, std::max( 100, in ) );
Topic archived. No new replies allowed.