What is wrong in this code to find the greatest of four numbers using function? This is a question from Hackerrank C++ practice. Please give solution.
This is the error i am getting:
Solution.cpp: In function ‘int max(int, int, int, int)’: Solution.cpp:21:5: error: expected ‘}’ before ‘else’ else { cout << "b is greatest" << endl; } ^~~~ Solution.cpp:9:16: note: to match this ‘{’ if (a > b) { ^ Solution.cpp:22:5: error: no return statement in function returning non-void [-Werror=return-type] } ^ Solution.cpp: At global scope: Solution.cpp:23:1: error: expected declaration before ‘}’ token } ^ cc1plus: some warnings being treated as errors
#include <iostream>
#include <stdio.h>
using namespace std;
/*
Add `int max_of_four(int a, int b, int c, int d)` here.
*/
int max(int a, int b, int c, int d)
{
if (a > b) {
if (a > c) {
if (a > d) {
cout << "a is greatest" << endl;
}
else {
cout << "d is greatest" << endl;
}
}
else {
cout << "c is greatest" << endl;
}
else { cout << "b is greatest" << endl; }
}
}
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
int ans = max(a, b, c, d);
cout << ans;
return 0;
}