C++ Regarding cin.ignore()
Posted
by
user1578897
on Stack Overflow
See other posts from Stack Overflow
or by user1578897
Published on 2012-11-15T16:00:27Z
Indexed on
2012/11/15
17:00 UTC
Read the original article
Hit count: 145
c++
i would hope someone can modify my code as its so buggy. sometime its work, sometime it dont..
so let me explain more.. Text file data is as below
Line3D, [70, -120, -3], [-29, 1, 268]
Line3D, [25, -69, -33], [-2, -41, 58]
To read the above line.. i use the following
char buffer[30];
cout << "Please enter filename: ";
cin.ignore();
getline(cin,filename);
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,20,',');
if(strstr(buffer,"Point3D"))
{
Point3D point3d_tmp;
readFile>>point3d_tmp;
// and so on...
Then i did a overload on the ifstream for Line3d
ifstream& operator>>(ifstream &input,Line3D &line3d)
{
int x1,y1,z1,x2,y2,z2;
//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);
Point3D pt1(x1,y1,z1);
Point3D pt2(x2,y2,z2);
line3d.setPt1(pt1);
line3d.setPt2(pt2);
line3d.setLength();
}
But the issue is the record work sometime and sometime it dont.. what i mean is if at this point
//i add a cout
cout << x1 << y1 << z1;
cout << x2 << y2 << z2;
//its works!
Point3D pt1(x1,y1,z1);
Point3D pt2(x2,y2,z2);
line3d.setPt1(pt1);
line3d.setPt2(pt2);
line3d.setLength();
but if i take away the cout it dont work. how do i change my cin.ignore() so the data can be handle properly , consider number range is -999 to 999
© Stack Overflow or respective owner