Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While Loop exiting early when reading from file

I am new to C++, so apologies if it is something obvious.

I have a data file which I am reading from, and it has information in the format: shapeName,Colour and then coordinates of the points. My polyline shape has an unknown number of coordinates, however when I try to read them, any shapes that follow do not get read at all.

This is the polyline part of the function inside the main while loop:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

while(infile>>shapeName>>stringColour){

 else if (shapeName == "Polyline"){

               int XX,YY;

               infile>>x1>>y1;//Read once and then set the values to the placeholder values in XX and YY
               XX = x1;
               YY = y1;



               while(infile>>x2>>y2){
                   myLines.push_back(line(XX,YY,x2,y2));
                   x1 = x2; //Update the start point values
                   y1 = y2;

               }
               cout<<shapeName<<endl;

               myShapes.push_back(make_unique<polyline>(polyline(colour,myLines)));
               myLines.clear(); //clear the vector so it is ready for another polyline shape

           }
}

Does anyone know why the main loop is exited after this part of the code is run?

I have tried using the break function at points, but I cannot see any improvements. I have confirmed that this is the problem by removing the polyline shape, and this results in the following shapes being read correctly.

>Solution :

It seems that the issue is with the way you are reading the input file. When the while loop for reading the coordinates of the polyline ends, the input stream infile reaches the end of the file, which causes the infile>>shapeName>>stringColour statement to fail and exit the while loop. To solve this, you can clear the end-of-file flag using the clear() function and ignore the rest of the line using the ignore() function before reading the next shape name and colour. Here’s an updated version of the code:

while(infile>>shapeName>>stringColour){

   if (shapeName == "Polyline"){

       int XX,YY;

       infile>>x1>>y1;//Read once and then set the values to the placeholder values in XX and YY
       XX = x1;
       YY = y1;

       while(infile>>x2>>y2){
           myLines.push_back(line(XX,YY,x2,y2));
           x1 = x2; //Update the start point values
           y1 = y2;
       }
       cout<<shapeName<<endl;

       myShapes.push_back(make_unique<polyline>(polyline(colour,myLines)));
       myLines.clear(); //clear the vector so it is ready for another polyline shape

       infile.clear(); //clear the end-of-file flag
       infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //ignore the rest of the line
   }
}

This should allow you to read all shapes in the input file without exiting the loop prematurely.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading