Mrchacha Mrchacha - 2 months ago 21
C++ Question

Continue a while loop until EOF (Qt)

I'm working on a program to create a list from a large QStringList. So basically after a string match, a while loop will start to add the next strings into a list. This part is working fine. The only problem I have is that the program quits unexpectedly because I don't know how I should add the EOF mechanism.

Update with a more detailed code

Sorry for not providing with enough details about my code. This is how my code looks now. So after the first time the string "PACKAGE TYPE" is detected, I use the function

storeLines()
function to store the next strings into one of three lists dependent. And this will continue until the next "PACKAGE TYPE" match or EOF. The only thing that is not working correctly now is when the iterator is on the last string of the QStringList. It somehow doesn't detect that the next is
inputline.end()


void storeLines(QString department, QStringList::iterator current_line, QStringList::iterator endline){
while(QString::compare(*(current_line + 1),"PACKAGE TYPE") && (++current_line != endline)){ //this is not working
if(!QString::compare(department, "MDA")) mda_list.push_back(*current_line);
else if(!QString::compare(department, "SDA")) sda_list.push_back(*current_line);
else mix_list.push_back(*current_line);
}
}

void void MainWindow::on_pushButton_clicked(){
QString input = ui->listinput->toPlainText().toLatin1();
QStringList inputline = input.split("\n", QString::SkipEmptyParts );

for(QStringList::iterator pkg_header(inputline.begin()); pkg_header != inputline.end(); ++pkg_header){
if(!QString::compare(*pkg_header,"PACKAGE TYPE")){
++pkg_header;
if(!QString::compare(*pkg_header,"Department-mda:")) storeLines("MDA", pkg_header, inputline.end());
else if(!QString::compare(*pkg_header,"Department-sda:")) storeLines("SDA", pkg_header, inputline.end());
else storeLines("MIX", pkg_header, inputline.end());
}
}
}


Thanks in advance!

Answer

Your apparent intent is to find the string "PACKAGE TYPE" in the existing list, and then copy the remaining elements in the list to a new list.

If that's the case, then why not do exactly that?

for(QStringList::iterator current_line(inputline.begin()); current_line != inputline.end(); ++current_line){
    if(!QString::compare(*current_line,"PACKAGE TYPE"){
        list.insert(list.end(), ++current_line, inputline.end());
        break;
    }
}