mardi 3 avril 2018

Storing Cartesian points in a vector and outputting distance

Im having trouble writing my main for a class I created. I created a class called CartesianPoints which I want to use to construct my main with. I have the general structure of my main created but am struggling with the some technical stuff..

Heres what im looking for:

  • creating an empty vector of CartesianPoint Objects which will be the starting point for the vector
  • Limit range for x and y values between 10 & -10.
  • loop to show user the points they just entered and ask what they would like to enter next
  • the loop above should continue until the break is triggered

here is my header for CartesianPoints

    #ifndef MY_CARTESIAN_POINT_H

    #define MY_CARTESIAN_POINT_H

    #include <iostream>         // cin, cout
    #include <sstream>          // stringstream
    #include <cmath>            // sqrt()
    #include <limits>           // INT_MAX
    #include <stdexcept>        // out_of_range

    using namespace std;


    class CartesianPoint
    {
      public:

        CartesianPoint(int x = 1, int y = 1) { SetPoint(x, y); }


        int GetX() const { return myX; }
        int GetY() const { return myY; } 


        double GetDistanceTo(CartesianPoint pointTo) const; 
        string ToString() const;    


        void SetX(int x) { myX = validateCoordinateValue(x); } 
        void SetY(int y) { myY = validateCoordinateValue(y); } 
        void SetPoint(int x, int y) { SetX(x); SetY(y); }   

        static int GetLimit() { return sharedLimit; }

        static void SetLimit(int limit) { sharedLimit = abs(limit); }

      private: 

        int myX; 
        int myY;  


        static int sharedLimit; 


        int validateCoordinateValue(int value) const;

    };  

    int CartesianPoint::sharedLimit = INT_MAX;    

    double CartesianPoint::GetDistanceTo(CartesianPoint pointTo) const 
    {
        int xDelta = pointTo.myX - myX; 
        int yDelta = pointTo.myY - myY;

        return sqrt((xDelta * xDelta) + (yDelta * yDelta));
    } 

    string CartesianPoint::ToString() const
    {

        stringstream strOut; 

        strOut << "(" << myX << ", " << myY << ")";

        return strOut.str();
    }

    int CartesianPoint::validateCoordinateValue(int value) const
    {

        if((value < -sharedLimit || value > sharedLimit))
        {

            throw out_of_range( "Parameter (" + to_string(value) + ") must be between " 
                + to_string(-sharedLimit) + " and " + to_string(sharedLimit) + ".");
        }
        return value;
    }

    #endif

here is my main so far

int main()
{

GreetingScreen(); // just a formatting function ive already created 

// while loop that makes will give the option to end the program.
while(/* if myX! =10 and myY!= 10 keep doing this loop */ )
{
    // try catch for errors....
    try
    {
        cout << "Move from point"  /* (0,0)*/ "to where?" << endl;  
        cout << "X: " << endl;
        cin >> x; //point x 
        cout << "Y: " << endl; 
        cin >> y; //point y 


        catch 
        {
            cerr << "could not do this task"; 
        }

    } 
} // ending of while loop 
} // ending of main 

Aucun commentaire:

Enregistrer un commentaire