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]
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.