Reputation: 179
below is my code
ifstream& operator>>(ifstream &input,Line3D &line3d)
{
int x1,y1,z1,x2,y2,z2;
x1=0;
y1=0;
z1=0;
x2=0;
y2=0;
z2=0;
//get x1
input.ignore(2);
input>>x1;
//get y1
input.ignore();
input>>y1;
//get z1
input.ignore();
input>>z1;
//get x2
input.ignore(4);
input>>x2;
//get y2
input.ignore();
input>>y2;
//get z2
input.ignore();
input>>z2;
input.ignore(2);
//cout << x1 << "," << y1 << "," << z1 << "," <<endl;
//cout << x2 << "," << y2 << "," << z2 << "," <<endl;
Point3D pt1(x1,y1,z1);
Point3D pt2(x2,y2,z2);
line3d.setPt1(pt1);
line3d.setPt2(pt2);
line3d.setLength();
}
There this weird issue, if i comment my cout , the 2 cout above. My code will not work, but if i uncomment it, the code will work.. heres the terminal output..
Please enter your choice: 1
Please enter filename: messy.txt
10 records read in successfully!
Going back to main menu ...
But if i uncomment cout...
Please enter your choice: 1
Please enter filename: file.txt
7,12,3,
-9,13,68,
7,-12,3,
9,13,68,
70,-120,-3,
-29,1,268,
25,-69,-33,
-2,-41,58,
9 records read in successfully!
Going back to main menu ...
9 Records is the correct one. but why a cout will solve my code issue. while i thought cout just print to terminal. what did i do wrong here .
This is the text file content
Point2D, [3, 2]
Line3D, [7, 12, 3], [-9, 13, 68]
Point3D, [1, 3, 8]
Line2D, [5, 7], [3, 8]
Point2D, [3, 2]
Line3D, [7, -12, 3], [9, 13, 68]
Point3D, [6, 9, 5]
Point2D, [2, 2]
Line3D, [70, -120, -3], [-29, 1, 268]
Line3D, [25, -69, -33], [-2, -41, 58]
Point3D, [6, 9, -50]
The correct response i want from terminal is
Correct output: 9 records read in successfully
Reason: as 10 means the value fail to record to vector. and duplicate is not removed.
Thanks for all guide, what should i do to maintain the correct output yet remove away the cout..
More of my code on main.cpp:
readFile.open(filename.c_str());
//if successfully open
if(readFile.is_open())
{
//record counter set to 0
numberOfRecords = 0;
while(readFile.good())
{
//input stream get line by line
readFile.getline(buffer,25,',');
Line3D line3d_tmp;
readFile>>line3d_tmp;
done=true;
//some for loop to check for duplication
//done will be false if doesnt work
if(done==true)
{
line3d.push_back(line3d_tmp);
}
So you see if the value is push_back into my vector, the duplicate record will be remove, as i purposely put 2 records of same value. The issue is if i use the 2 cout line . The record is shown as 9 (correct as there 1 line of duplicate) and on second run to input same file. 0 records is being read..
But if i comment away my cout, on second run, it read 4 RECORDS again. The first run was 10 records..
Upvotes: 0
Views: 429
Reputation: 74078
Several points:
ifstream
to istream
input
at the end of the operatoroperator>>()
for Point3D
and use that in Line3D
Upvotes: 1