### Function with IF statements - Error?

I am new to c++ and been self learning, still a newbie. I have been doing exercises to practice. I have got a error with return value for my function.

Can someone please help? - I am trying to write a function which will return the highest number out of 4 numbers which is going to be inputted.

 ``12345678910111213141516171819202122232425262728`` `````` int highest(int a, int b, int c, int d) { if(a>b) { if(a>c) { if(a>d) { return a; } else return d; } } else if(b>c) { if(b>d) { return b; } else return d; } } ``````
Last edited on
Is that ALL of your code? You really need to indent:

 ``12345678910111213141516171819202122`` ``````int highest(int a, int b, int c, int d) { if (a > b) { if (a > c) { if (a > d) { return a; } else return d; } } else if (b > c) { if (b > d) { return b; } else return d; } }``````

I see no reason for errors in this function, you should put the rest of your code.
The else (and hence a return) for `if(b>c)` is missing. What if (a > b) and (b <= c)?
With the help of a helper function (say std::max) or just write your own version, you can reduce this to
 ``123`` ``````int highest(int a, int b, int c, int d) { return max(a,(max(b,max(c,d)))); }``````

 ``1234567891011121314151617181920`` ``````#include #include using namespace std; template T largest( T a, T b) { return a > b ? a : b; } template T largest( T a, Tail... b ) { return largest( a, largest( b... ) ); } int main() { int a = 1, b = 3, c = 2, d = -2; cout << largest( a, b, c, d ) << '\n'; cout << max( { a, b, c, d } ) << '\n'; }``````
what if the largest number is c?
This is my code to find the largest number, correct me if I'm wrong.
 ``12345678910111213141516171819202122232425262728`` ``````int getLargest(int a, int b, int c, int d) { if (a > b) { if (a > c) { if (a > d) { return a; } else return d; } if (c > d) return d; else return c; } if (b > c) { if (b > d) return b; else return d; } if (c > d) return c; else return d; }``````
Jc126 wrote:
correct me if I'm wrong.

You're wrong.

Try a=2, b=1, c=4, d=3

This bit's definitely wrong:
 ``1234`` `````` if (c > d) return d; else return c;``````

I do suggest code with a little less ... branching.
Last edited on
 ``12345678910`` ``````int highest( int a, int b, int c, int d ) { int highest_so_far = a ; if( b > highest_so_far ) highest_so_far = b ; // higher of a and b if( c > highest_so_far ) highest_so_far = c ; // highest of a,b and c if( d > highest_so_far ) highest_so_far = d ; // highest of a,b,c and d return highest_so_far ; }``````

Or
 ``12345`` ``````// using the library: #include (you may ignore this for now) constexpr int highest( int a, int b, int c, int d ) { return std::max( { a, b, c, d } ) ; }``````
Would this be correct now?

 ``123456789101112131415161718192021222324252627282930313233`` `````` int highest(int a, int b, int c, int d) { if (a > b) { if (a > c) { if (a > d) { return a; } else return d; } } else if (b > c) { if (b > d) { return b; } else return d; } else if (c > d) { return c; } else return d; } ``````
No. There are circumstances under which it would return nothing.

BTW, how many separate accounts do you have?

 ``12345678910111213`` ``````int highest(int a, int b, int c, int d) { if (a > b) { // the only thing we know so far is that b is not the largest // we have to compute highest(a, c, d) here } else { // the only thing we know so far is that a is not the largest // we have to compute highest(b, c, d) here } }``````

Writing all the nested conditions makes code long and complex.

Look at the solution by JLBorges. There are only 3 tests.

@salem c: In this specific case there are power-of-two operands. One could:
`return std::max( std::max(a, b), std::max(c, d) );`