I'm hoping to create a program that allows a user to enter several points, their coordinates and their masses and then calculates, over a period of time, each points' new coordinates due to gravity.

I've started a code to calculate position, and I'm trying to understand why DEV C++ keeps telling me that I'm missing a semi-colon on lines 9 and 10. I don't understand that...

#include <iostream>
#include <cmath>

using namespace std;
 
class Points
{
      public:
             float Two_Point_Distance(int x1, int x2, int y1, int y2) { return sqrt(pow(x2-x1,2) + pow(y2-y1, 2)) ; } // find the distance between 2 points.
             float Three_Point_Distance(x1, x2, y1, y2, int z1, int z2) { return sqrt(pow(x2 - x1, 2) +  pow(y2 - y1, 2) + pow (z2 - z1, 2)) ; } // finds the distance between 3 points.
             Points (int, int, int); // constructor
             ~Points ();
      private:
              int ItsX;
              int ItsY;
              int ItsZ;
};

Points::Points(int Initial_X, int Initial_Y, int Initial_Z) // constructor defines the points x, y and z values.
{
                  ItsX = Initial_X;
                  ItsY = Initial_Y;
                  ItsZ = Initial_Z;
}

Points::~Points()
{
}

main()
{
      
int Initial_X;
int Initial_Y;
int Initial_Z;

cin >> Initial_X, cin >> Initial_Y, cin >> Initial_Z; // Asks the user for points X, Y, and Z.

Points PointA(Initial_X, Initial_Y, Initial_Z);

return 0;
}

Recommended Answers

All 23 Replies

Extra ) probably.

I figured it out. The function pow does not accept integers, only doubles. I just created my own doubling function.

So on to problem #2.

Take a look at lines 37 and 39. I want to user to be able to specify the points of every individual object. Unfortunately, the way I've done it, I'll only be able to do specify one point. Every time I call up a different object, I need to user to be able to choose the point. In addition, I need a way for the user to choose how many objects (points) are called.

I figured it out. The function pow does not accept integers, only doubles. I just created my own doubling function.

the function definition of three_point_dist is wrong i think...is it working...
you have written float Three_Point_Distance[B](x1, x2, y1, y2,[/B] int z1, int z2) won't x1,x2,y1,y2 need type specifier???

Take a look at lines 37 and 39. I want to user to be able to specify the points of every individual object. Unfortunately, the way I've done it, I'll only be able to do specify one point. Every time I call up a different object, I need to user to be able to choose the point. In addition, I need a way for the user to choose how many objects (points) are called.

well you can make a public member of class points for getting input like public void getInput(); put the line number 37 in this function inside the class and everytime you make an object in main call this function with that object...

Points obj;
obj.getInput();

third problem
you want the user to decide the number of points , right?? you may use dynamic memory allocation...ask user for the number of points N and make an array of Objects of point class.

PS: You should use function overriding concept for the two functions two_point_dist and Three_point_dist...

that's the beauty of overriding::cool:

alright, we'll see how this all turns out. I haven't learned about dynamic memory yet, so I'll need to teach myself that.

EDIT: hmm... Now, before I had wanted to specify every objects' coordinates when that object was created. The constructor class asks for the coordinates to be specified when the object is created. Using the GetInput function makes would get me the coordinates after the object had already been created. Could I place the GetInput function inside the parameters of the constructor function?

#include <iostream>
#include <cmath>

using namespace std;

int Second(int x)
{ 
          return x * x;
}
 
class Points
{
      public:
             float Two_Point_Distance(float x1, float x2, float y1, float y2) 
             { 
                   return sqrt(Second(x2-x1) + Second(y2-y1)); 
             } // find the distance between 2 points.
             float Three_Point_Distance (float x1, float x2, float y1, float y2, float z1, float z2) 
             { 
                   return sqrt(Second(x2 - x1) +  Second(y2 - y1) + Second(z2 - z1)) ; 
             } // finds the distance between 3 points.
             int GetInputX(); // asks the user for the X coordinate.
             int GetInputY(); // asks the user for the Y coordinate.
             int GetInputZ(); // asks the user for the Z coordinate.
             
             Points (int, int, int); // constructor
             ~Points ();
      private:
              int ItsX;
              int ItsY;
              int ItsZ;
};

Points::Points(int Initial_X, int Initial_Y, int Initial_Z) // constructor defines the points x, y and z values.
{
                   ItsX = Initial_X;
                   ItsY = Initial_Y;
                   ItsZ = Initial_Z;
}

Points::~Points()
{
}

int GetInputX() // Asks the user for coordiante X;
{
       int Initial_X;
       cout << "Please give the X coordiante" << endl;                 
       cin >> Initial_X;
       cin.ignore();
       return Initial_X;
}
int GetInputY() // Asks the user for coordiante Y;
{
       int Initial_Y;
       cout << "Please give the Y coordiante" << endl;                 
       cin >> Initial_Y;
       cin.ignore();
       return Initial_Y;
}
int GetInputZ() // Asks the user for coordiante Z;
{
       int Initial_Z;
       cout << "Please give the Z coordiante" << endl;                 
       cin >> Initial_Z;
       cin.ignore();
       return Initial_Z;
}

main()
{
Points PointA(GetInputX(), GetInputY(), GetInputZ());

return 0;
}

So things are working... Except that for some reason the program is asking for the coordinates backwards. It first asks for Z, then for Y, then for X. Any ideas why?

that is because u have one constructor only....which requires the parameter...you can have another constructor with no parameters ....

when u create an object the constructor in the class in invoked...if you have not defined any constructor then the default constructor gets invoked....also you may override the constructor..

what is happening is when you create an object the constructor defined by you gets invoked and it requires the parameter ....make another constructor with no parameter..it will solve the problem...
hope you are getting me...

one more thing ...
you have made three getInput functions for each co-ordinate ...that is not required have only one and initialize all the three cordinates in that one...
this will save time..and program will be much easier to use and implement..

Dear friend,

The only thing I've found is:

there is no typing for x1, x2, y1, y2 in the parameter list for the function

float Three_Point_Distance(x1, x2, y1, y2, int z1, int z2).

I don't remember if int is default in such case (parameter list), like for return function type.

I'm using a machine without Dev-C++ and can't try to change the code. Please try it:

float Three_Point_Distance(int x1, int x2, int y1, int y2, int z1, int z2).

I hope it could help.
Franco

I'm hoping to create a program that allows a user to enter several points, their coordinates and their masses and then calculates, over a period of time, each points' new coordinates due to gravity.

I've started a code to calculate position, and I'm trying to understand why DEV C++ keeps telling me that I'm missing a semi-colon on lines 9 and 10. I don't understand that...

#include <iostream>
#include <cmath>

using namespace std;
 
class Points
{
      public:
             float Two_Point_Distance(int x1, int x2, int y1, int y2) { return sqrt(pow(x2-x1,2) + pow(y2-y1, 2)) ; } // find the distance between 2 points.
             float Three_Point_Distance(x1, x2, y1, y2, int z1, int z2) { return sqrt(pow(x2 - x1, 2) +  pow(y2 - y1, 2) + pow (z2 - z1, 2)) ; } // finds the distance between 3 points.
             Points (int, int, int); // constructor
             ~Points ();
      private:
              int ItsX;
              int ItsY;
              int ItsZ;
};

Points::Points(int Initial_X, int Initial_Y, int Initial_Z) // constructor defines the points x, y and z values.
{
                  ItsX = Initial_X;
                  ItsY = Initial_Y;
                  ItsZ = Initial_Z;
}

Points::~Points()
{
}

main()
{
      
int Initial_X;
int Initial_Y;
int Initial_Z;

cin >> Initial_X, cin >> Initial_Y, cin >> Initial_Z; // Asks the user for points X, Y, and Z.

Points PointA(Initial_X, Initial_Y, Initial_Z);

return 0;
}

Thank you for the help so far, but I'm still not exactly sure what I need to do next. How can I create an another constructor?

how can i make the other constructor

just like u made the first one....see do u know what function overriding is....???
take a tutorial on that...and u will be able to make another constructor....

function overriding is nothing but two functions with the same name but different number or types of parameters passed to it....

Well, I made an effort at fixing this code. I got rid of the warning errors by making using function overriding. Thank you for that suggestion. But I'm still not able to get constructor to work properly. The program compiles just fine, but it never asks the user for coordinates. It just does nothing.

Does anyone have any ideas what I should do next?

#include <iostream>
#include <cmath>

using namespace std;

float Second(float x)
{ 
          return x * x;
}
float Second(int x)  // Overloaded Function.
{ 
          return x * x;
}

 
class Points
{
      public:
             float Two_Point_Distance(float x1, float x2, float y1, float y2) // Finds the distance between 2 points.
             { 
                   return sqrt(Second(x2-x1) + Second(y2-y1)); 
             }
             float Two_Point_Distance(int x1, int x2, int y1, int y2) // Overloaded Function.
             { 
                   return sqrt(Second(x2-x1) + Second(y2-y1)); 
             }
             
             float Three_Point_Distance (float x1, float x2, float y1, float y2, float z1, float z2) // finds the distance between 3 points.
             { 
                   return sqrt(Second(x2 - x1) +  Second(y2 - y1) + Second(z2 - z1)) ; 
             }
            
             float Three_Point_Distance (int x1, int x2, int y1, int y2, int z1, int z2) // Overloaded Function.
             { 
                   return sqrt(Second(x2 - x1) +  Second(y2 - y1) + Second(z2 - z1)) ; 
             } 
             
             void SetInput();
             
             Points ();  // Constructor
             ~Points (); // Destructor
      private:
              float ItsX;
              float ItsY;
              float ItsZ;
};

Points::Points() // Construcor defines the points x, y and z values.
{
     void SetInput();
}

Points::~Points() // Destructor
{
}

void Points::SetInput() // Asks the user for coordiantes;
{
float Initial_X, Initial_Y, Initial_Z;
cout << "Please give the X coordiante" << endl;
cin >> Initial_X;
cout << "Please give the Y coordiante" << endl;
cin >> Initial_Y;
cout << "Please give the Z coordiante" << endl;
cin >> Initial_Z;
cin.ignore();

ItsX = Initial_X;
ItsY = Initial_Y;
ItsZ = Initial_Z;
}

main()
{
      
Points PointA();
cin.ignore();

return 0;
}

>Points PointA();
That's not an object declaration, it's parsed as a function declaration (a function called PointA that takes no arguments and returns a Point object by value). Remove the parens:

Points PointA;

>Points PointA();
That's not an object declaration, it's parsed as a function declaration (a function called PointA that takes no arguments and returns a Point object by value). Remove the parens:

Points PointA;

Thank you! That's one bug squashed, but the program still doesn't seem to be working. Am I not allowed to use a function inside the constructor the way that I did? That's the only thing that I can think of... Some sort of syntax error like that.

>void SetInput();
That's a function declaration too. To call a function you omit the return type and supply arguments for the parameters. Since this function takes no parameters, you can just remove void and call it good.

>void SetInput();
That's a function declaration too. To call a function you omit the return type and supply arguments for the parameters. Since this function takes no parameters, you can just remove void and call it good.

Thank you! My program finally works. Time to implement some new features.

So now I'm trying to create a function that will ask the player how many points the problem will include. The function will then instantiate that many objects (points) and call each object's constructor. I really went out on a limb with this one... I'm positive that it isn't in the least bit correct. Here's what I've got.

void Points::SetNumberOfPoints()
{
     int NumberOfPoints, i;
     cout << "How many points do you want?" << endl;
     cin >> NumberOfPoints;
     for (i = 1; i <= NumberOfPoints; i++)
     {
          Points ("Point" + ("A + i"));
     }
}

Thank you! My program finally works. Time to implement some new features.

well that's good that your program is working but i would suggest u to call the setInput function from main
you can also leave the body of the constructor empty....the purpose of such kind of empty bodied constructor is just to be there while declaring an object in main...

and calling setInput in the main through the object of the class is a good programming practice and it gives better readability to the program

So now I'm trying to create a function that will ask the player how many points the problem will include. The function will then instantiate that many objects (points) and call each object's constructor.

well that's not the work of the function of that class....means this work has to be done in main and objects will be created there only ....may be an array of objects of class Point
you must ask me why can't we do that in class...that is because one object of the point class represents only one point...as you have declared three private data variables (x,y,z)...

if you want to have an entity of a number of points you have to do it in main....or you make a child class of this point class which will have number of points(N) and N points object as the data member..

for (i = 1; i <= NumberOfPoints; i++)
     {
          Points ("Point" + ("A + i"));
     }
}

there is nothing like this.... "Point" & "A+i" denotes strings.. and this is not the way to make multiple object of a class....

if you want to have an entity of a number of points you have to do it in main....or you make a child class of this point class which will have number of points(N) and N points object as the data member...

So what is a child class? I'll look it up right now. Do you have any example of one by any chance?

i think you havn't come across Inheritance yet...

take a tutorial on Inheritance...the class can be a subclass of class points....then class points is class the root class,or parent class
and the class which inherits the points class is called subclass or child class...

click here
for inheritance tutorial

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.