max integer of 3 numbers using an outside funcition

I need to write a program using a function to output the max of 3 integers. This is what i have so far but i cant figure out whats wrong with the program. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
#include <iostream>
using namespace std;

int main () {
        int a, b, c;
        int maximum;
        cout << "Input 3 numbers to find the max number: " << endl;
        cin >> a, b, c;
        maximum=max(a, b, c);
        cout << "The largest integer is " << maximum << endl;
        return 0;
        }

int max (int a, int b, int c) {
        if ((c<b) && (c<a)) {
                if ((a>b) cout << a;
                else cout << b;}
        else cout << c;}

cin >> a, b, c; is equivalent to:
1
2
3
cin >> a;
b;
c;
As you can see it is probably not what you want.
#include <iostream>
using namespace std;

int main () {
cout << max(5,9,8) << endl;
cout << max(12,5,7) << endl;
cout << max(10,15,18) << endl;
return 0;
}

int max (int a, int b, int c) {
if ((c<b) && (c<a)) {
if (a>b) cout << a;
else cout << b;}
else cout << c;}

i chaged it to this. Why does this still not work?
1
2
3
if ((c<b) && (c<a))
 /*...*/
else cout << c;
If c is not smallest, it will be output even if it is not largest. For example in your first call (c<b) is true, but (c < a) is false.
How have you determined that there is "wrong with the program"?
* Compile error? If yes, what does the error say?
* Unexpected results?

Line 9. Operator precedence. You write: (cin >> a), (b), (c);
The last two statements do nothing, so this is equivalent: cin >> a;

One can chain >> operators: cin >> a >> b ...


Line 3. It is better not to use that statement. It can bring unexpected names into global scope.

Line 4. The main() does not know about function max(int,int,int) because there is no declaration before main().

Line 20. Your max returns int, but code does not return anything.

Line 16. If true, then c is smallest. Else (line 19) you think that c is largest. Can you see why that does not hold?
Topic archived. No new replies allowed.