Help with pointers, pointer arithmetic

How do i access the data, and how do i declare it. I need to be using "pointer arithmetic"

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
47
  #include <iostream>
#include <math.h>
using namespace std;


struct data {
	int x1,y1,x2,y2;
	double m,d;
	
};

void slope(double ptr);

void distance (double ptr );

int main()
{

	data *ptr = new data;
	
		
		cout << " What are the two coordinates of the First points : ";
		cin >> ptr->x1 >> ptr->y1;
		
		cout << " What are the two coordinates of the Second points : ";
		cin >> ptr->x2 >> ptr->y2;
	void distance (double ptr);
	{
		 d= sqrt( pow( x2-x1,2)  + ( pow(y2-y1,2)));
		cout << "Distance : " << d;
	}
	void slope (double ptr);
	{
		if ( (y2 - y1) == 0)
			cout << "Slope does not exist";
		else
		{
		double m = (y2 - y1)/ (x2- x1);
		cout << "Slope Equals :" << m;
		}
	}
//d = sqrt( pow( x2-x1,2)  + ( pow(y2-y1,2)));
//	m = (y2 - y1)/ (x2-x1);
		
	return 0;
}
> How do i access the data
What data?
int x1,x2,y1,y2. double m,d. E.x. what would i have to write to display those integers and doubles

> How do i declare it.
Declare what?
the variables, in main, my compiler keeps telling me they are not declared in the scope.
Thank you for replying to my question.

In function 'int main()':
29:4: error: 'd' was not declared in this scope
29:18: error: 'x2' was not declared in this scope
29:21: error: 'x1' was not declared in this scope
29:36: error: 'y2' was not declared in this scope
34:9: error: 'y2' was not declared in this scope
38:26: error: 'x2' was not declared in this scope
38:30: error: 'x1' was not declared in this scope

@raynierl,

I will start at the top struct data works better as struct Data notice the capital D. Not sure why it made a difference, but it did.

Then in your forward declarations void distance (double ptr ); you need to say that you are sending a pointer of type Data void distance (Data *ptr );.

Lines 27 to 41 are actually 2 functions that need to be defined after main, then at line 27 or 28 call the functions when needed.

In line 29 you did not declare d with a data type as you did with m in line 38.

Lastly in what should be separate functions x1, y1,x2, y2 are not declared, but are part of the struct so you need refer to the struct with pointer notation ptr->x1 etc.

Fix those problems and it should work.

Hope that helps,

Andy
@Handy Andy
Thank you SO MUCH, big help.Thank you for your time.
Though I do not know why, my professor says it should be able to compute (1/3,-3) and (2, -1/4) but the second I put in the first two points the program goes crazy. any advice.
Topic archived. No new replies allowed.