I have a text file points.txt

which is formatted by Point2D/Point3D , X ,Y

points.txt

Point2D, [-9, -9]
Point3D, [-9, -9, -9]
Point2D, [-99, -99]
Point2D, [-999, -999]
Point3D, [-999, -999, -999]

What I want to do is

1) retrieve the X and Y values that belong to Point2D from the textfile
2) place it and calculate the distance from origin between the cordinates(0,0) and Point2D cordinates.
3) place the X,Y and distance from origin into a vector and sort the X cord or distance from origin by either Ascending or Descending.

The problem that I faced was, my Dist Fr Origin is sorting in Descending order instead of
Ascending order and vice-versa. whereas my sorting for X seems be to sorting correctly.

this is my implementation for sorting

 bool sortOrderPointTwoDXAsc (const Point2D & rhs, const Point2D & lhs) {
        return lhs.x < rhs.x;
    }

    bool sortOrderPointTwoDXDsc (const Point2D & rhs, const Point2D & lhs) {
        return lhs.x > rhs.x;
    }


    bool sortOrderDisFrOriginAsc (const Point2D & rhs, const Point2D & lhs) {
        return lhs.distFrOrigin < rhs.distFrOrigin;
    }

    bool sortOrderDisFrOriginDsc (const Point2D & rhs, const Point2D & lhs) {
        return lhs.distFrOrigin > rhs.distFrOrigin;
    }

My output

 Sort X by Ascending
 ------------------------------------
    X     Y    Dist. Fr Origin 
 ------------------------------------
   -9    -9    12.728 
  -99   -99    140.007 
 -999  -999    1412.799 

 Sort X by Descending
 ------------------------------------
    X     Y    Dist. Fr Origin 
 ------------------------------------
 -999  -999    1412.799 
  -99   -99    140.007 
   -9    -9    12.728 

 //Sorting by Descending instead of ascending
 Sort Dist Fr Origin by Ascending
 ------------------------------------
    X     Y    Dist. Fr Origin 
 ------------------------------------
 -999  -999    1412.799 
  -99   -99    140.007 
   -9    -9    12.728 

 //Sorting by Ascending instead of Descending
 Sort Dist Fr Origin by Descending
 ------------------------------------
    X     Y    Dist. Fr Origin 
 ------------------------------------
   -9    -9    12.728 
  -99   -99    140.007 
 -999  -999    1412.799 

Below are my codes. I am seeking help as I do not know what's wrong with my codes that is
causing the sorting of my distance from origin to be incorrect.

MyTemplates.h

    #ifndef sample_MyTemplates_h
    #define sample_MyTemplates_h

    template<class T>
    T pointTwoDDifference(T  x , T  y)
    {
        T difference;
        difference =  ((x-0)*(x-0) + (y-0)*(y-0));
        return difference;
    }


    #endif

Point2D.h

    #ifndef __sample__Point2D__
    #define __sample__Point2D__

    #include <iostream>

    class Point2D {
        friend class Line2D;
        friend class main;

    protected:
        int x;
        int y;
        double distFrOrigin;
        void setDistFrOrigin();
    private:

    public:

        Point2D() {
            x=0;
            y=0;
        };//default Constructor
        Point2D (int x , int y);
        int getX();
        int getY();
        double getScalarValue();
        void setX(int x);
        void setY(int y);


        friend bool sortOrderPointTwoDXAsc (const Point2D & rhs, const Point2D & lhs);
        friend bool sortOrderPointTwoDXDsc (const Point2D & rhs, const Point2D & lhs);

        friend bool sortOrderDisFrOriginAsc (const Point2D & rhs, const Point2D & lhs);
        friend bool sortOrderDisFrOriginDsc (const Point2D & rhs, const Point2D & lhs);
    };

    #endif 

Point2D.cpp

#include "Point2D.h"
#include "MyTemplates.h"
#include <math.h>

Point2D::Point2D(int x , int y) {
    setX(x);
    setY(y);

}

int Point2D::getX() {
    return x;
}

int Point2D::getY() {
    return y;
}

void Point2D::setX(int x)   {
    this->x = x;
}

void Point2D::setY(int y)   {
    this->y = y;
}

void Point2D::setDistFrOrigin()     {
    int difference;
    difference = pointTwoDDifference<int>(x, y);
    distFrOrigin = sqrt(difference);

}

double Point2D::getScalarValue()    {
    setDistFrOrigin() ;
    return distFrOrigin;
}

main.cpp

#include <iostream>
#include "main.h"
#include "MyTemplates.h"
#include "Point2D.h"
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <stdio.h>
#include <iomanip>
#include <algorithm>

Point2D point2D;
class main printResults;

bool sortOrderPointTwoDXAsc (const Point2D & rhs, const Point2D & lhs) {
    return lhs.x < rhs.x;
}

bool sortOrderPointTwoDXDsc (const Point2D & rhs, const Point2D & lhs) {
    return lhs.x > rhs.x;
}


bool sortOrderDisFrOriginAsc (const Point2D & rhs, const Point2D & lhs) {
    return lhs.distFrOrigin < rhs.distFrOrigin;
}

bool sortOrderDisFrOriginDsc (const Point2D & rhs, const Point2D & lhs) {
    return lhs.distFrOrigin > rhs.distFrOrigin;
}

void main::getRecordsFromFile() {

    std::ifstream readFile;
    //put your text file name inside the ""
    readFile.open("points.txt");
    if (!readFile.is_open()) {
        std::cout <<" "<< std::endl;
        std::cout << "File Not Found!" << std::endl;
        std::cout <<" "<< std::endl;
    }
    else    {
        while (readFile.good()) {
            while(getline(readFile,line))   {
                std::stringstream iss(line);
                getline(iss, type,',');
                if(type == "Point2D")  {

                    getline(iss, pointTwoDX,'[');
                    getline(iss, pointTwoDX,',');

                    getline(iss, pointTwoDY ,' ');
                    getline(iss, pointTwoDY,']');

                    Point2D point2DConsturctor(std::stoi(pointTwoDX),std::stoi(pointTwoDY));
                    scalarValue = point2DConsturctor.getScalarValue();
                    point2DVector.push_back(point2DConsturctor);
                }
            }
        }

    }
    readFile.close();
}


void main::viewData()   {

        getRecordsFromFile();
        sort(point2DVector.begin(), point2DVector.end(),sortOrderPointTwoDXAsc);
        std::cout <<" " << std::endl;
        printf("%20s\n "," Sort X by Ascending");
        std::cout <<"------------------------------------" << std::endl;
        printf("%5s %5s %18s \n ","X" ,"Y", "Dist. Fr Origin");
        std::cout <<"------------------------------------" << std::endl;
        for (int i = 0; i<point2DVector.size(); i++) {
            printf("%5d %5d %2s %.3f \n",point2DVector[i].getX(),point2DVector[i].getY()," ",point2DVector[i].getScalarValue());
        }
        point2DVector.clear();

        getRecordsFromFile();
        sort(point2DVector.begin(), point2DVector.end(),sortOrderPointTwoDXDsc);
        std::cout <<" " << std::endl;
        printf("%20s\n "," Sort X by Descending");
        std::cout <<"------------------------------------" << std::endl;
        printf("%5s %5s %18s \n ","X" ,"Y", "Dist. Fr Origin");
        std::cout <<"------------------------------------" << std::endl;
        for (int i = 0; i<point2DVector.size(); i++) {
            printf("%5d %5d %2s %.3f \n",point2DVector[i].getX(),point2DVector[i].getY()," ",point2DVector[i].getScalarValue());
        }
        point2DVector.clear();


        getRecordsFromFile();
        sort(point2DVector.begin(), point2DVector.end(),sortOrderDisFrOriginAsc);
        std::cout <<" " << std::endl;
        printf("%20s\n "," Sort Dist Fr Origin by Ascending");
        std::cout <<"------------------------------------" << std::endl;
        printf("%5s %5s %18s \n ","X" ,"Y", "Dist. Fr Origin");
        std::cout <<"------------------------------------" << std::endl;
        for (int i = 0; i<point2DVector.size(); i++) {
            printf("%5d %5d %2s %.3f \n",point2DVector[i].getX(),point2DVector[i].getY()," ",point2DVector[i].getScalarValue());
        }
        point2DVector.clear();


        getRecordsFromFile();
        sort(point2DVector.begin(), point2DVector.end(),sortOrderDisFrOriginDsc);
        std::cout <<" " << std::endl;
        printf("%20s\n "," Sort Dist Fr Origin by Descending");
        std::cout <<"------------------------------------" << std::endl;
        printf("%5s %5s %18s \n ","X" ,"Y", "Dist. Fr Origin");
        std::cout <<"------------------------------------" << std::endl;
        for (int i = 0; i<point2DVector.size(); i++) {
            printf("%5d %5d %2s %.3f \n",point2DVector[i].getX(),point2DVector[i].getY()," ",point2DVector[i].getScalarValue());
        }
        point2DVector.clear();


    std::cout << " " << std::endl;
}

int main()
{
    printResults.viewData();
    return 0;
}

main.h

#ifndef sample_main_h
#define sample_main_h
#include <iostream>
#include "Point2D.h"
#include <vector>


class main : public Point2D  {

private:

    double scalarValue;
    std::string type,line;
    std::string pointTwoDX, pointTwoDY;
    std::vector<Point2D> point2DVector;

public:

    void viewData();
    void getRecordsFromFile();

};

#endif

whereas my sorting for X seems be to sorting correctly.

I don't think it is. Your program suggests that -999 is greater than -9, which isn't true; -999 is nine hundred and ninety less than -9. Flip round the < and the > on all your sort functions.

Hi,

Thanks alot. I forgotten that the value it's negative.

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.