C++ - getline() keeps reading the same line over and over again for some reason

Posted by Jammanuser on Stack Overflow See other posts from Stack Overflow or by Jammanuser
Published on 2012-10-07T03:20:58Z Indexed on 2012/10/07 3:37 UTC
Read the original article Hit count: 156

Filed under:
|

I am wondering WTF my while loop which calls istream& getline ( istream& is, string& str ); keeps reading the same line again. I have the following while loop (nested down several levels of other while loops and if statements) which calls getline, but my output statement which is the first code line in the while loop's block of code tells me it is reading the same line over and over again, which explains why my output file doesn't contain the right data when my program is finished.

while (getline(file_handle, buffer_str)) {
      cout<< buffer_str <<endl;
      cin.get();
      if ((buffer_str.find(';', 0) != string::npos) && (buffer_str.find('\"', 0) != string::npos)) { //we're now at the end of the 'exc' initialiation statement
         buffer_str.erase(buffer_str.size() - 2, 1);
         buffer_str += '\n';
         for (size_t i = 0; i < pos; i++) {
             buffer_str += ' ';
         }
         buffer_str += "throw(exc);\n";
         for (size_t i = 0; i < (pos - 3); i++) {
             buffer_str += ' ';
         }
         buffer_str += '}';
      }

      else if (buffer_str.find(search_str6, 0) != string::npos) { //we're now at the second problem line of the first case
         buffer_str += " {\n";
         output_str += buffer_str;
         output_str += '\n';
         getline(file_handle, buffer_str); //We're now at the beginning of the 'exc' initialiation statement
         output_str += buffer_str;
         output_str += '\n';

         while (getline(file_handle, buffer_str)) {
               if ((buffer_str.find(';', 0) != string::npos) && (buffer_str.find('\"', 0) != string::npos)) { //we're now at the end of the 'exc' initialiation statement
                 buffer_str.erase(buffer_str.size() - 2, 1);
                 buffer_str += '\n';
                 for (size_t i = 0; i < pos; i++) {
                     buffer_str += ' ';
                 }
                 buffer_str += "throw(exc);\n";
                 for (size_t i = 0; i < (pos - 3); i++) {
                     buffer_str += ' ';
                 }
                 buffer_str += '}';
               }

               output_str += buffer_str;
               output_str += '\n';

               if (buffer_str.find("return", 0) != string::npos) {
                  getline(file_handle, buffer_str);
                  output_str += buffer_str;
                  output_str += '\n';
                  about_to_break = true;
                  break; //out of this while loop
               }
         }
      }
      if (about_to_break) {
         break; //out of the level 3 while loop (execution then goes back up to beginning of level 2 while loop)
      }
      output_str += buffer_str;
      output_str += '\n';
}

Because of this problem, my if statement and then my else statement in my loop are not functioning as they should, and it doesn't break out of that loop when it should (though it eventually does break out of it, but I don't know exactly how yet).

Anyone have any idea what could be causing this problem??

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c++

Related posts about getline