problem with a function in my class

hi people!
i am a beginner in c++ and i wrote this code
this code is designed to get cordinates of 2 points and caleclute the lenght
between those 2 points the problem is that the function named GETCORDINATES that i designed is not working i wanted this code let the user import his points cordinates but programm shows the constructor values only
for sample i run the code and i set x=1 & y=2 but programm well tell me x=0 and y=0 please help tnx

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
45
46
  // rectangle.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;

class point
{
public:
	int x, y;
	point()
	{
		x = y =0;
	}
	static void GetCordinates(point point1)			//i wrote this code so when i call it it should set X & Y of the given point
	{
		cout << "please enter your point cordinates"<<endl;
		cout << "x=>";
		cin >> point1.x;
		cout << "y=>";
		cin >> point1.y;
	}
private:
};

class line
{
public:
	static double line_lengh(point point1, point point2)		//this code will caleclute the lenght between 2 points its working right now
	{
		return sqrt(pow(point1.x - point2.x, 2)+pow(point1.y-point2.y,2));
	}
private:

};

int _tmain(int argc, _TCHAR* argv[])
{
	point p1,p2;
	point::GetCordinates(p1);	
	cout << "x is "<<p1.x<<endl;	// but its not setting cordinates of the point its only show values of constractor that x&y is set 0
	cout << "y is "<<p1.y;
	getchar(); getchar();
	return 0;
}

Last edited on
Well you have 2 options:

1/ Make the point1 argument a reference to a point, rather than a copy of a point
1
2
3
4
5
6
7
8
	static void GetCordinates(point &point1)	  // "&" means reference. No "&" means copy
	{
		cout << "please enter your point cordinates"<<endl;
		cout << "x=>";
		cin >> point1.x;
		cout << "y=>";
		cin >> point1.y;
	}


2/ Make the GetCordinates function a non-static member function & call it on the point1 object directly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class point
{
public:
	int x, y;
	point()
	{
		x = y =0;
	}
	/*static */void GetCordinates(/*point point1*/)
	{
		cout << "please enter your point cordinates"<<endl;
		cout << "x=>";
		cin >> /*point1.*/x;
		cout << "y=>";
		cin >> /*point1.*/y;
	}
private:
};

and in main:
1
2
3
4
5
6
7
8
9
10
int _tmain(int argc, _TCHAR* argv[])
{
	point p1,p2;
	//point::GetCordinates(p1);	
	p1.GetCordinates();	
	cout << "x is "<<p1.x<<endl;
	cout << "y is "<<p1.y;
	getchar(); getchar();
	return 0;
}
Last edited on
thank u sir that worked fine!!
Topic archived. No new replies allowed.