0

Hey I have written a code using C++. The program is about calculating distance between two points of a plane. Without using class the program works fine but whenever I do it with class & wanna return a value it doesn't return any value.

`

#include<iostream>
#include<math.h>
using namespace std;
class plane{
public:
    void getdata(float X1,float X2,float Y1,float Y2)
    {
        cout<< "Enter point X1 ";
        cin>>X1;
        cout<< "Enter point Y1 ";
        cin>>Y1;
        cout<< "Enter point X2 ";
        cin>>X2;
        cout<< "Enter point Y2 ";
        cin>>Y2;
    }
    double distance2(float A1,float A2,float B1,float B2)
{
    double distance;
    float side1,side2;
    side1=(A1-A2)*(A1-A2);
    side2=(B1-B2)*(B1-B2);
    distance=sqrt(side1+side2);
    return distance;
}
};

int main()
{
    float X1,X2,Y1,Y2;
    plane plane1,plane2;
    plane1.getdata(X1,X2,Y1,Y2);
    plane1.distance2(X1,X2,Y1,Y2);
    cout<<endl;
    plane2.getdata(X1,X2,Y1,Y2);
    plane2.distance2(X1,X2,Y1,Y2);
}
  • How do you know it is not returning a value? I'm asking because you seem to be ignoring the return value from `distance2`. Maybe you meant to write `cout << plane1.distance2(X1,X2,Y1,Y2);` – Superman Oct 04 '17 at 05:42

1 Answers1

1

There are two errors in your code.

First one: you are not looking at the value returned from the method distance2. You should do:

 float distance=plane1.distance2(X1,X2,Y1,Y2);
 cout<<distance<<'\n';

Second one: you are not really initialising the variables float X1,X2,Y1,Y2; declared in your main. Read about passing arguments by value or by reference.

So, you should modify you method getdata to:

void getdata(float &X1,float &X2,float &Y1,float &Y2)
{
    cout<< "Enter point X1 ";
    cin>>X1;
    cout<< "Enter point Y1 ";
    cin>>Y1;
    cout<< "Enter point X2 ";
    cin>>X2;
    cout<< "Enter point Y2 ";
    cin>>Y2;
}
Federico
  • 743
  • 9
  • 22