Click here to Skip to main content
15,921,295 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically I have an assignment where I can't use if() or if else statements in C++.

The problem is saying :
If the entered number is greater than 2.375, make it = 2.375
If the number is less than 0, make it =0.

How do I do this without if functions? I'm just newbie learning C++ :3

What I have tried:

I so far cannot think of anything I've learned yet that can do this. Any help would be appreciated!
Posted
Updated 5-Oct-18 19:38pm

Your friend is the ternary operator[^]!

/ravi
 
Share this answer
 
Couple of ways:
1) The ternary operator:
C++
value = value > 0 ? value : 0;
But ... technically, that;s a short form of if ... else ...
2) Cheat, and use a while:
C++
while (value < 0) value = 0;
It's not an efficient solution, but ... it works.
3) Cheat even less efficiently and use a for:
C++
for ( ; value < 0; ) value = 0;
 
Share this answer
 

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