-
trouble assigning arrays
Inside the for loop of my SetPoints() method which is in the Polygon class Im trying to assign the values of the parameter arrays into a class array, but its not working, the class array just continues to hold garbage values. I have a few debugging output statements that show that. Can anyone tell me whats wrong?
Code:
#include <iostream>
#include <string>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point();
Point(int takeX, int takeY);
void Set(int pntX, int pntY);
int GetX();
int GetY();
void DisplayPoint();
};
class Polygon
{
private:
int num_points;
static const int MAX_POINTS = 100;
string points[MAX_POINTS];
public:
Polygon();
Polygon(int polySize, int xArray[], int yArray[]);
void SetPoints(int polySize, int xArray[], int yArray[]);
string ShowPolygon();
void AddPoint(int x, int y);
void AddPoint(Point p);
void ClearPoly();
};
int main()
{
int coord1 = 83, coord2 = 25, coord3 = 27, coord4 = 85;
int groupNum = 6;
int groupX[] = {68,17,54,98,16,34};
int groupY[] = {38,15,84,56,68,84};
int x_values[] = {34,97,73,95,82,35,79,46,28,15};
int y_values[] = {35,94,67,15,94,68,64,26,68,18};
int coord5 = 37, coord6 = 15, coord7 = 67;
int coord8 = 92, coord9 = 73, coord10 = 93;
Point pointObj1(4,5);
pointObj1.DisplayPoint();
pointObj1.Set(coord1, coord2);
pointObj1.DisplayPoint();
Polygon poly1(10, x_values, y_values);
Polygon poly2;
poly2.SetPoints(groupNum, groupX, groupY);
cout << poly1.ShowPolygon() << endl;
cout << poly2.ShowPolygon() << endl;
poly2.AddPoint(pointObj1);
pointObj1.Set(coord3, coord4);
poly1.AddPoint(pointObj1.GetX(), pointObj1.GetY());
cout << poly1.ShowPolygon() << endl;
cout << poly2.ShowPolygon() << endl;
poly1.ClearPoly();
poly2.ClearPoly();
Point pointObj2(coord5, coord6);
Point pointObj3(coord7, coord8);
Point pointObj4(coord9, coord10);
poly1.AddPoint(pointObj2);
poly1.AddPoint(pointObj3);
poly1.AddPoint(pointObj4);
pointObj2.Set(x_values[0], x_values[1]);
pointObj3.Set(x_values[2], x_values[3]);
pointObj4.Set(x_values[4], x_values[5]);
poly2.AddPoint(pointObj2.GetX(), pointObj2.GetY());
poly2.AddPoint(pointObj3.GetX(), pointObj3.GetY());
poly2.AddPoint(pointObj4.GetX(), pointObj4.GetY());
cout << poly1.ShowPolygon() << endl;
cout << poly2.ShowPolygon() << endl;
return 0;
}
//------------------point methods--------------
Point::Point()
{
x = 0;
y = 0;
}
Point::Point(int takeX, int takeY)
{
Set(takeX, takeY);
}
void Point::Set(int pntX, int pntY)
{
if (pntX > 0 || pntX < 400)
{
x = pntX;
}
else
cout << "x is not within resolution boundries" << endl;
if (pntY > 0 || pntY < 400)
{
y = pntY;
}
else
cout << "y is not within resolution boundries" << endl;
}
int Point::GetX()
{
return x;
}
int Point::GetY()
{
return y;
}
void Point::DisplayPoint()
{
cout << "(" << x << "," << y << ")" << endl;
}
//-----------polygon methods------------------
Polygon::Polygon()
{
num_points = 0;
}
Polygon::Polygon(int polySize, int xArray[], int yArray[])//how to pass arrays int methods
{
SetPoints(polySize, xArray, yArray);
}
void Polygon::SetPoints(int polySize, int xArray[], int yArray[])
{
cout << "points Xarray is now " << xArray[0] << " " << xArray[1] << " " << xArray[2] << " " << xArray[3] << " " << endl;
cout << "points Yarray is now " << yArray[0] << " " << yArray[1] << " " << yArray[2] << " " << yArray[3] << " " << endl;
int xValue = sizeof(xArray), yValue = sizeof(yArray); // get array size
if (polySize < 0 && polySize >= MAX_POINTS)
cout << "polygon size is not within bounds" << endl;
else if (xValue > 50 || yValue > 50)
cout << "x array or y array is too large" << endl;
//else if (xValue != yValue || polySize != xValue || polySize != yValue)
//cout << "array size or array values do not agree" << endl;
else if (polySize % 2 != 0 || xValue % 2 != 0 || yValue % 2 != 0)
cout << "array size or array values are not even" << endl;
else
{
num_points = polySize;
int j = 0;
for (int h = 0;h < num_points;h++)
{
cout << "NUMPOINTS is: " << num_points << endl;
cout << "Xarray in for is now " << xArray[h] << endl;
points[j] = xArray[h];
points[j + 1] = yArray[h];
cout << "points[] in for is now " << points[j] << endl;
j += 2;
}
cout << "points_array is now " << points[0] << " " << points[1] << " " << points[2] << " " << points << " " << endl;
}
}
string Polygon::ShowPolygon()
{
string displayPoly;
for (int h = 0;h < num_points;h += 2)
{
displayPoly += "(" + points[h] + "," + points[h + 1] + ")";
if (h < num_points)
displayPoly += ",";
}
return displayPoly;
}
void Polygon::AddPoint(int x, int y)
{
points[num_points] = x;
points[num_points + 1] = y;
num_points += 2;
}
void Polygon::AddPoint(Point p)
{
points[num_points] = p.GetX();
points[num_points + 1] = p.GetY();
num_points += 2;
}
void Polygon::ClearPoly()
{
num_points = 0;
}
-
sizeof doesn't work on arrays passed into functions. It will return the size of a pointer.
You need to pass the size of the array to the functions that need it. Or you can use vector, which is a dynamic array that knows its size.
-
Daved- I havent studied vectors yet in my C++ class, so that is out. I have to follow the spec for the assignment, and the assignment calls for one array called points[] to be declared in the Polygon class. I dont think I need to pass in the size of arrays to any functions, the only reason I was checking for size is to do comparisons between arrays for error checking. I think the problem is that I have to instantiate a new points[] array inside the Set() method.
-
I think I found the problem, I declared the points[] array as a string instead of an int. Cant figure why that didnt show up as a compile error. sry to bother
-
You still have a problem with sizeof. I'm pretty sure that just won't work. It might seem like it works now, but it will likely cause you problems later.
If you don't believe me, try this code:
Code:
#include <iostream>
void print_size(int arr[])
{
std::cout << sizeof(arr) << '\n';
}
int main()
{
int arr1[] = { 1, 2 };
int arr2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
print_size(arr1);
print_size(arr2);
std::cin.get();
}
Does it output 2 and 11, or does it output 4 and 4? Note also that even if it worked like that, it would still be wrong because sizeof returns the number of bytes, not the number of items in the array.
If you can't use vectors, then you have to either pass in the size of the two arrays, or use the same size everywhere.
-
Quote:
Does it output 2 and 11
You mean 8 and 44, I'm sure ;)
--
Mats
-
>> You mean 8 and 44
Well, I meant 2 and 11 since that is what the OP is expecting even though that's not what will happen. It fails for two reasons, so even if the call to sizeof was made from inside main (where it would work better) it would still output the unintended values of 8 and 44.
-
Technically you could do the following, but then you would be able to pass only things that really are arrays (and not pointers to arrays). (So, in the general case, there's no escaping from the fact that often all you get is a pointer to the start of the array without any size information.)
Code:
#include <iostream>
template <unsigned Size>
void print_size(const int (&)[Size])
{
std::cout << Size << '\n';
}
int main()
{
int arr1[] = { 1, 2 };
int arr2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
print_size(arr1);
print_size(arr2);
std::cin.get();
}
-
That is, if you are allowed to use templates. They're typically an advanced subject, so they come last...