+ 1

Why doesn't this work?

I've been trying to make a program where you input 3 numbers and it outputs the biggest number or in the case of several biggest numbers, one of them. But I can't figure out why it's not working properly. I narrowed it down to this part of it not working properly: int main() { int a, b, c; cin >> a >> b >> c; if (b > a && c) { cout << b; } return 0; } If I type in 1 2 3, it outputs 2. I just can't wrap my head around such a simple problem.

6th Oct 2017, 9:21 AM
Mr.E
Mr.E - avatar
8 Antworten
+ 16
Current program flow. a = 1 b = 2 c = 3 if (2 > 1 && 3) if (true && true) if (true) cout << 2;
6th Oct 2017, 9:30 AM
Babak
Babak - avatar
+ 15
@Mr.e If you had any question about my solution feel free and ask.
6th Oct 2017, 9:51 AM
Babak
Babak - avatar
+ 14
Here is your solution. One-liner guy! cout << (a > b && a > c ? a : (b > a && b > c ? b : c));
6th Oct 2017, 9:46 AM
Babak
Babak - avatar
+ 5
Try this int main() { int a,b,c; if(a>b&&b>c){ cout<<a; } else if(b>a&&a>c){ cout<<b; } else { cout<<c; } return 0; }
6th Oct 2017, 9:54 AM
RZK 022
RZK 022 - avatar
+ 4
According to your code it will always print b bcos c is true. instead say, if( b>a & b>c)
6th Oct 2017, 9:50 AM
Ore
Ore - avatar
+ 2
@jhk123 Thank you, this was extremely confusing with the ton of different answers, but you saved me.
6th Oct 2017, 9:55 AM
Mr.E
Mr.E - avatar
0
@Dev I literally copy and pasted your code and it STILL shows 2.
6th Oct 2017, 9:44 AM
Mr.E
Mr.E - avatar