Search code examples
c++xmlfstreamifstreamofstream

c++ while loops, using getline function


Ok so to start because people like to know, this is homework....

I am writing a C++ Program to convert a text file to XML (this is not where I need help) I am having this issue.

I am writing a while loop to cycle through the companies in the txt file (up to 15) and at the end of each group it is marked with the words --END_MANAGER_DATA--. I am trying to write a while loop that will loop through the company's and finish looping when it reaches --END_MANAGER_DATA--. below is an example of the txt file

19936 WALKER KOLTON PORTLAND TN
HARMAN INTERNATIONAL INDUSTRIES INC
LUCENT TECHNOLOGIES INC
COMPUTER SCIENCES CORP
COMMUNICATIONS CORPORATION
--END_MANAGER_DATA--

this is the code I am trying to use.....

getline(inFile,company);
inFile.ignore();
while(company != "--END_MANAGER_DATA--"|| !inFile.eof())
{
    outputfile <<"\t\t\t <company> "<<company << "</company>"<<endl;
    getline(inFile,company);
    inFile.ignore();
}

this is not working....it just stays in the loop.... can someone offer advice as to a route I can take. I am not asking for you to finish my homework....just need a nudge in the right direction


Solution

  • Try this:

    string company = "";
    while(company.compare("--END_MANAGER_DATA--") != 0 && !inFile.eof()) {
        getline(inFile, company);
        outFile << "\t\t\t <company> "<< company << "</company>" << endl;
        inFile.ignore();
    }