Please help with POINT!

Pages: 12
Well, I've been working on a project that can store coordinate points in an array. Unfortunately, I'm not a very good coder, so this is the best I could come up with with my limited knowledge. I have gotten an error when I try to print this, saying that "no operator "<<" matches these operands on the line cout << pnt[30] << " ";. If anybody could help, that would be great :).

Coordinate Program:
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
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <windows.h>

using namespace std;

POINT pnt[30];
POINT randompoint;

int _tmain(int argc, _TCHAR* argv[])
{
	for (int n=0; n<30; n++)
	{
		srand ( time(NULL) );
		int x = rand() % 100;
		int y = rand() % 100;
		randompoint.x = x;
		randompoint.y = y;
		pnt[30] = randompoint;
	}
	for (int n=0; n<30; n++)
	{
		cout << pnt[30] << " ";
		cout << "\n";
	}
	system("pause");
	return 0;
}

Also, I would like to know if this would work. I'm trying to generate 30 random coordinate points just to test it.
closed account (zb0S216C)
On line 24, you need to specify which member to print. For example:

 
cout << pnt[ 30 ].x << endl;

Wazzak
Last edited on
Oh thank you! I can't believe I didn't think of that! But I'm having one more error- it displays the same random number for all the point. How can I make it so that it displays random numbers for all the points?
closed account (zb0S216C)
You can generate random numbers like this:

1
2
3
4
time_t Time;
time( &Time );
srand( Time );
int x( rand( ) % 100 );   

Wazzak
Last edited on
Edit:nvm
Last edited on
closed account (zb0S216C)
Here's your code but modified to work:

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
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <windows.h>

using namespace std;

POINT pnt[30];
POINT randompoint;
time_t Time;
int x, y;

int _tmain(int argc, _TCHAR* argv[])
{
        time( &Time );
	for (int n=0; n<30; n++)
	{
		srand ( Time );
		x = (rand() % 100);
		y = (rand() % 100);
		randompoint.x = x;
		randompoint.y = y;
		pnt[30].x = randompoint.x;
		pnt[30].y = randompoint.y;
	}
	for (int n=0; n<30; n++)
	{
		cout << "X coordinates: " << pnt[30].x << " ";
		cout << "Y coordinates: " << pnt[30].y << " ";
		cout << "\n";
	}
	system("pause");
	return 0;
}

Try that and see if it works for you.

Wazzak
Edit:nvm
Last edited on
The lines that contain pnt[30] are incorrect. The array is of 30 elements, so this means the elements go from index 0 to index 29. Besides, you probably want pnt[n].
Edit:nvm
Last edited on
You did not correct lines 23 and 24.
closed account (D80DSL3A)
Also, you should seed rand() only once. Replace line 15 with srand ( time(NULL) ); and remove line 18.

Another point. You don't have to assign through 3 different sets of variables like you are in lines 19-24, just assign directly:
1
2
pnt[n].x = rand()%100;
pnt[n].y = rand()%100;

There is no need for the variables x, y and randompoint.
Last edited on
Edit:nvm
Last edited on
Yes. I suppose that you would define "closest" as the point closest according to Pythagoras. So just run through the array of points calculating pythagoras between the point of interest and the point in turn in the array. Then just settle for the point that yields the lowest distance.
Edit:nvm
Last edited on
Edit:nvm
Last edited on
hehe, you are making a big mess. To determine the minimum (or maximum) from a group of items is rather simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double minDistance; //You need a double for the distance because the rounding to integer can make you err the choice.
int minIndex = -1;
//The fancy way of initializing minDistance is to set it to the highest possible number (#include <limits>):
minDistance = std::numeric_limits<double>::max;
//Now just loop through the array of points.
double distance = 0;
for (int n = 0; n < 30; n++)
{
    distance = DistanceFunction(pnt[n], MyCharacter);
    if (distance < minDistance)
    {
        minDistance = distance;
        minIndex = n;
    }
}
//At this point you know that minIndex is the index of the point nearest to MyCharacter. 
Edit:nvm
Last edited on
Sorry, it is max(), not max. The parenthesis are missing. You should also #include <limits>. See my comments in the code.
Last edited on
Edit:nvm
Last edited on
Pages: 12