Return variable help

need to somehow get the center point of an object using this code,

1
2
3
4
 
double Center_x, Center_y;
    return Center_x = (p1x+p2x+p3x)/3;
    return Center_y = (p1y+p2y+p3y)/3; 


However, I Dont know how to get the y variable to return as the cout looks like this
(x,)
(x being arbitrary)
Last edited on
You can return only one value from a function. Luckily that one value can be a custom type that holds more than one number.

Do you have a type for your "object"?
Do you have a type for a point?
Ive tried creating a double called "Center" that I set equal to Center_x and Center_y. is that what you mean?

or do you mean I need to do something like

*cntxpntr and *cntypntr? if so I am not really sure how I would go about setting that up.
Do you have a type for the object you're working with, as in a class or a struct, any user-defined datatype.
@Uk Marine
I have a class and a object for the class. I can get my other functions to return the correct number but this function has to have two, ( x,y) I cant figure it out
Do you have a type for a point? For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Point {
  double x;
  double y;

  Point( double x, double y ) : x(x), y(y) {}

  Point & operator/= ( int s ) {
    x /= s;
    y /= s;
  }

  Point & operator+= ( Point & rhs ) {
    x += rhs.x;
    y += rhs.y;
    return *this;
  }
};

Point operator+ ( Point lhs, const Point & rhs ) {
  return lhs += rhs;
}

// and much more for a compleat interface 


You could of course use std::pair<double,double> or std::array<double,2>, etc, or ...

Choose an up to date and maintained third-party library that provides the required functionality (like a type for point) and has an acceptable license.
Topic archived. No new replies allowed.