-3

I am new to generic programming and I am trying to get it to read in a text file, store it in a vector, sort it and then output them. Below shows the code and my output:

Point2D.cpp:

Point2D::Point2D() 
{
    x = 0;
    y = 0;
    distFrOrigin = 0.0;
}

Point2D::Point2D(int x, int y) 
{
    x = x;
    y = y;
    distFrOrigin = 0.0;
}

Point2D::~Point2D() {
//destructor
}

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

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

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

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

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

void Point2D::setDistFrOrigin() {
 distFrOrigin = sqrt(abs((pow(x, 2)) + pow(y, 2)));
}

ostream& operator<<(ostream &out, const Point2D &p2d)
{
    out << "[" << setw(4) << p2d.x << "," << setw(4) << p2d.y << "]" << setw(3) << "" << endl;
    return out;
}

I have these sort functions which are also listed in the driver file:

bool sortP2DAscX(const Point2D & rhs, const Point2D & lhs) {
return lhs.x > rhs.x;
}
bool sortP2DDscX(const Point2D & rhs, const Point2D & lhs) {
return lhs.x < rhs.x;
}
bool sortP2DAscY(const Point2D & rhs, const Point2D & lhs) {
return lhs.y > rhs.y;
}
bool sortP2DDscY(const Point2D & rhs, const Point2D & lhs) {
return lhs.y < rhs.y;
}
bool sortP2DOAsc)(const Point2D & rhs, const Point2D & lhs) {
return lhs.distFrOrigin < rhs.distFrOrigin;
}
bool sortP2DODsc)(const Point2D & rhs, const Point2D & lhs) {
return lhs.distFrOrigin > rhs.distFrOrigin;
}

My code shows me a negative value and the text file is as follows: messy.txt:

Point2D, [324, -374]Point2D, [882, -588]Point2D, [-993, 958]Point2D, [6, -922]Point2D, [376, 219]Point2D, [600, 203]Point2D, [425, -676]

Output

This is part of the main.cpp driver file which I read in the text file:

main.cpp:

void inputTextFile() {
string fileName;
cout << endl;
cout << "Please enter filename : "; cin >> fileName;
ifstream file(fileName, ios::in);

while (getline(file, fileName)) {
    if (!fileName.empty()) {

        stringstream linestream(fileName);
        string readDataType = "";
        string omnom;
        string stringX1, stringY1, stringZ1, stringX2, stringY2, stringZ2;
        int bX1, bY1, bZ1, bX2, bY2, bZ2;
        // Save PID into StockArray
        getline(linestream, readDataType, ',');
        if (readDataType == "Point2D") {
            // [bX, bY]

            //Trim
            getline(linestream, omnom, '[');

            //Get X coordinate
            getline(linestream, stringX1, ',');
            bX1 = stoi(stringX1);

            //Get Y coordinate
            getline(linestream, stringY1, ']');
            bY1 = stoi(stringY1);

            Point2D bP2D = Point2D(bX1, bY1);
            GP2D.push_back(bP2D);
            records++;
        }

    }
 }

}

void viewData() {
cout << endl;
cout << "[ View data ... ]" << endl;
cout << "Filtering Criteria: " << curFilterOpt << endl;
cout << "Sorting Criteria : " << curSortCri << endl;
cout << "Sorting Order: " << curSortOrder << endl;
cout << endl;

if (curFilterOpt == "Point2D") 
{
    if (curSortCri == "x-ordinate") {
        if (curSortOrder == "ASC") {
            sort(GP2D.begin(), GP2D.end(), sortP2DAscX);
        }
        else if (curSortOrder == "DSC") {
            sort(GP2D.begin(), GP2D.end(), sortP2DDscX);
            reverse(GP2D.begin(), GP2D.end());
        }
    } else if (curSortCri == "y-ordinate") {
        if (curSortOrder == "ASC") {
            sort(GP2D.begin(), GP2D.end(), sortP2DAscY);
        }
        else if (curSortOrder == "DSC") {
            sort(GP2D.begin(), GP2D.end(), sortP2DDscY);
        }
    }
    cout << setw(5) << "X" << setw(6) << "Y" << "    Dist. Fr Origin" << endl;
    cout << "-------------------------------" << endl;
    for (int i = 0; i < GP2D.size(); ++i) {
        cout << GP2D[i] << "   " << fixed << setprecision(3) << GP2D[i].getScalarValue() << endl;
    }
}

Why are all the values in the output negative? And they are all the same numbers. The values in the text file can be from any range of -999 to 999. But I have different values in each Point2D.

alonelycoder
  • 103
  • 11
  • 2
    Please create a more minimal example – Tom Feb 20 '19 at 12:13
  • @Tom will edit it. – alonelycoder Feb 20 '19 at 12:29
  • How many lines are in your file? From the "messy.txt" you provided it seems like there is only 1. Can you please provide a better example of this txt file? – Jonaswg Feb 20 '19 at 12:36
  • The values are `-858993460` which is `0xcccccccc` meaning uninitialized stack memory. https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations – drescherjm Feb 20 '19 at 12:37
  • @Jonaswg hi, i've tried separating the info as to put the next Point2D info in the next line and it does not work as well. – alonelycoder Feb 20 '19 at 12:40
  • I expect your file reading code in inputTextFile is not working. You should print the vector right after reading the file. Or just debug it line by line. You have Visual Studio so you should be able to set a breakpoint in the reading code and see what happens. Ah I see the problem, I added a comment in the answer what the second bug is. – drescherjm Feb 20 '19 at 12:43

1 Answers1

2

there's a typo in your code: x = y

Point2D::Point2D(int x, int y) 
{
    x = y;
    y = y;
    distFrOrigin = 0.0;
}
Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21
  • 2
    There is a second bug with this same code.. You need `this->x = x;` and `this->y=y;` the second bug is causing the member variables x and y to never be set and this is why they are 0xcccccccc – drescherjm Feb 20 '19 at 12:45