I can't seem to understand the bug in this code

Write a function that takes 3 integer parameters and returns the maximum value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include <conio.h>
#include <iomanip>


void max (int,int,int);
using namespace std;
int main() {
int x,y,z;
int q;

cout << "Enter Three Integer Values " << endl;
cin >> x >> y >> z;
  q = max (x,y,z);
  cout << q;
}


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

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

    }

}
 In function 'int main()':
14:5: error: void value not ignored as it ought to be

 At global scope:
19:26: error: ISO C++ forbids declaration of 'max' with no type [-fpermissive]

 In function 'int max(int&, int&, int&)':
33:1: warning: no return statement in function returning non-void [-Wreturn-type] 
and returns the maximum value


Line 6: Function max returns void -- no value.
Line 14: you cannot store void into int q;

Line 19: If max is a function, what is its return type? There is nothing before name "max".

Line 33: Due to line 19 error the compiler decided that return type is int. Alas, the function does not return any value.
keskiverto explains it above

but yeah you are missing void,your prototype is not the same as the declaration

a function must return something if it does not it should return void.

also your function should return an int

because you cannot set an int = to void max returns void so return an int

also in your prototype you have three ints but it should be three int references

here is the fixed up code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


#include <iostream>


using namespace std;
int max(int&,int&,int&); // error was here

int main() {
int x,y,z;
int q;

cout << "Enter Three Integer Values " << endl;
cin >> x >> y >> z;
 q = max(x,y,z); // error was here
  cout << q;
}


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

}




hope that helps
Now questions on logic:

* If the main() receives and "answer" to q and then prints the answer,
why does the max() print anything?

* Why does the function take references?
Does it need to modify caller's variables?
Could you ever call q = max( x, y, 0 );?
Last edited on
Topic archived. No new replies allowed.