int* to int error?

Hey guys I keep getting an invalid int* to int conversion on lines 44, 48, and 51. Can someone tell me what exactly that is and how to fix it? Thanks.
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
39
40
41
42
43
44
 #include <iostream>
#include <iomanip>
using namespace std;

void readInput(int &a, int &b, int &c);
void findMedian(int d, int e, int f, int &g);

int main()
{
  int num1;
  int num2;
  int num3;
  int d;
  int e;
  int f;
  int g;
  int median;

  readInput(num1, num2, num3);
  findMedian(d, e, f, g);

  cout<<"The median value is " << median << endl;
}
void readInput(int &a, int &b, int &c)
{
  cout<<"Enter three integers" << endl;
  cin>> a >> b >> c;
}
void findMedian(int d, int e, int f, int &g)
{
  int median;
  if ((d > e && d < f) || (d > f && d < e))
    {
    median = &g;
    }
  else if ((e > d && e < f) || (e < d && e > f))
    {
    median =&g;
    }
  else ((f > d && f < e) || ( f < d && f > e));
    median = &g;
  return;
}
 
It would help if you actually showed lines 44, 48, and 51..Or gave us proper lines.

I'm guessing you mean lines 34, 38, and 41 though. That is because it should be "g" and not "&g" the reason you use &g in the parameter is because you passed it as reference. When you use & as a unary operator it is the address of. Also why are you even passing it by reference?

Ps there is no need to have a return statement in void. That is kind of the point of void, having no return.

By the way your find median function is ugh...like completely wrong. You are probably supposed to return the median with g, or use int instead of void as return type.

Also if you use g as output it would be g = median, but you would assign an actual value for median on lines 34, 38, 41 and not some arbitrary value.

Btw the median is the middle number when they are sorted.
Last edited on
@giblit sorry man i forgot to say that the first 10 lines are just comments. I'm passing it by reference because that's what my professor wants me to do. As for the return thing I don't really know what I was thinking. :/

When I put median = g; on lines 33, 38, and 41 it gives me some crazy negative number. What would cause that? Should I change g to a float?

The assignment states that we cant use returns.

Also sorry for stupid questions. I am trying to learn how to program and be a better programmer. I really am. I'm just stuck.
Last edited on
When I put median = g; on lines 33, 38, and 41 it gives me some crazy negative number. What would cause that?
The fact that g contain some crazy negative number.
Look at lines related to g:
1
2
3
4
5
int g;
//...
findMedian(d, e, f, g);
/*...*/
median = &g;
What do you think it would be equal to?

The same could be said for other parameters.

Point of your assigment is to read three numbers, pass same three numbers to your median function and get result as fourth parameter, then output said fourth parameter.
1
2
findMedian(num1, num2, num3, median);
std::cout<<"The median value is " << median << '\n';

@MiiNiPaa Hey thanks. I got it to compile but for some reason it keeps telling me the median is 3. I'll keep working at it. :)
Topic archived. No new replies allowed.