Hello everyone. I'm writing a program that will search for particular words and count if they appear or not and then output to a csv file. I suspect that my problem lies in line 71 when it checks to see if otext contains dataArray[iii][0] and writes "yes" if it does, because the debugresults displays "no" for dataArray[iii][1].
My code:
Easier to read code/comments::
class cdata
{//creation of the cdata class. This is created to protect dataArray from external influences, but can still be accessed through select functions.
public:
void popcountries();
private:
};
void MainWindow::on_parse_B_clicked()
{//button clicked and program starts
QString tmp;
//creates the QString for results to be shown tmp = start(); //start()'s results are the results to be shown
ui.debugResultsTxt->setPlainText(tmp); //results shown
}
{
//calls cdata class from above
cdata d;
d.popcountries(); // populates first column of the array from the given file
MainWindow mw; //calls mainwindow class
input = mw.oText(); //returns the text to be searched through as the user's input
d.parseInput(input); //uses the user's input to see if it contains dataArray[iii][0] (the first column of the array)
QString tmp;
//creates a qstring to return the final/debug results tmp = d.debugresults(); //debug results. This is used to format the final results as they would appear in the csv file.
return tmp; //returns the formatted final/debug results to on_parse_B_clicked()
}
void cdata::popcountries()
{ //parses the file line by line and populates the first column of dataArray. If the end of the file is reached, it populates the rest with "-1" as a placeholder
QString filename
= "countriesList.txt";
file.setFileName(filename);
for (int iii=0; iii<192; iii++)
{
QString tmp
= txtstream.
readLine();
dataArray[iii][0] = tmp;
int xxx = iii+1;
if (txtstream.atEnd() == TRUE)
{
for (xxx ; xxx<192; xxx++)
{
dataArray[xxx][0] = tmp2;
}
break;
}
}
file.close();
}
{ //grabs the user's input and returns it to start()
otext = ui.tmpTextinput->toPlainText();
return otext;
}
void cdata
::parseInput(QString otext
) {//checks to see if the word in dataArray[iii][0] (the first column as populated above) is in otext (the user's input) and writes "yes" in the next column if it is, and "no" if it is not.
for (int iii = 0; iii<192; iii++)
{
int xxx = iii+1;
if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //Check to see if otext has the word from the first column of dataArray --- I believe that this is the problem
{
dataArray[iii][1] = "yes"; //if it does, it writes yes in the next column
}
else
{
dataArray[iii][1] = "no"; //if it doesn't, it writes no in the next column
}
if (dataArray[xxx][0] == "-1") // checks the next row in the first column to see if it is the placeholder "-1". If it is, it breaks the loop.
break;
}
}
{ //formats the array into a string that will eventually become the csv file. For now, it formats them and returns the formatted string to start(), which returns that to on_parse_B_clicked() and displays it.
for (int iii=0; iii<192; iii++)
{
tmp2.append(" , " + tmp3 + " , ");
tmp1.append(tmp2 + " \n");
int xxx = iii+1;
if (dataArray[xxx][0] == "-1")
break;
}
return tmp1;
}
class cdata
{//creation of the cdata class. This is created to protect dataArray from external influences, but can still be accessed through select functions.
public:
void popcountries();
QString debugresults();
void parseInput(QString);
private:
QString dataArray[192][192];
};
void MainWindow::on_parse_B_clicked()
{//button clicked and program starts
QString tmp; //creates the QString for results to be shown
tmp = start(); //start()'s results are the results to be shown
ui.debugResultsTxt->setPlainText(tmp); //results shown
}
QString start()
{
//calls cdata class from above
cdata d;
d.popcountries(); // populates first column of the array from the given file
QString input;
MainWindow mw; //calls mainwindow class
input = mw.oText(); //returns the text to be searched through as the user's input
d.parseInput(input); //uses the user's input to see if it contains dataArray[iii][0] (the first column of the array)
QString tmp; //creates a qstring to return the final/debug results
tmp = d.debugresults(); //debug results. This is used to format the final results as they would appear in the csv file.
return tmp; //returns the formatted final/debug results to on_parse_B_clicked()
}
void cdata::popcountries()
{ //parses the file line by line and populates the first column of dataArray. If the end of the file is reached, it populates the rest with "-1" as a placeholder
QFile file;
QString filename = "countriesList.txt";
file.setFileName(filename);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream txtstream(&file);
for (int iii=0; iii<192; iii++)
{
QString tmp = txtstream.readLine();
dataArray[iii][0] = tmp;
int xxx = iii+1;
if (txtstream.atEnd() == TRUE)
{
for (xxx ; xxx<192; xxx++)
{
QString tmp2 = "-1";
dataArray[xxx][0] = tmp2;
}
break;
}
}
file.close();
}
QString MainWindow::oText()
{ //grabs the user's input and returns it to start()
QString otext;
otext = ui.tmpTextinput->toPlainText();
return otext;
}
void cdata::parseInput(QString otext)
{//checks to see if the word in dataArray[iii][0] (the first column as populated above) is in otext (the user's input) and writes "yes" in the next column if it is, and "no" if it is not.
for (int iii = 0; iii<192; iii++)
{
int xxx = iii+1;
if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //Check to see if otext has the word from the first column of dataArray --- I believe that this is the problem
{
dataArray[iii][1] = "yes"; //if it does, it writes yes in the next column
}
else
{
dataArray[iii][1] = "no"; //if it doesn't, it writes no in the next column
}
if (dataArray[xxx][0] == "-1") // checks the next row in the first column to see if it is the placeholder "-1". If it is, it breaks the loop.
break;
}
}
QString cdata::debugresults()
{ //formats the array into a string that will eventually become the csv file. For now, it formats them and returns the formatted string to start(), which returns that to on_parse_B_clicked() and displays it.
QString tmp1 = "Test results:,\n";
for (int iii=0; iii<192; iii++)
{
QString tmp2 = dataArray[iii][0];
QString tmp3 = dataArray[iii][1];
tmp2.append(" , " + tmp3 + " , ");
tmp1.append(tmp2 + " \n");
int xxx = iii+1;
if (dataArray[xxx][0] == "-1")
break;
}
return tmp1;
}
To copy to clipboard, switch view to plain text mode
Original code in post::
class cdata
{
public:
void popcountries();
private:
};
void cdata::popcountries()
{
QString filename
= "countriesList.txt";
file.setFileName(filename);
for (int iii=0; iii<192; iii++)
{
QString tmp
= txtstream.
readLine();
dataArray[iii][0] = tmp;
int xxx = iii+1;
if (txtstream.atEnd() == TRUE)
{
for (xxx ; xxx<192; xxx++)
{
dataArray[xxx][0] = tmp2;
}
break;
}
}
file.close();
}
{
for (int iii=0; iii<192; iii++)
{
tmp2.append(" , " + tmp3 + " , ");
tmp1.append(tmp2 + " \n");
int xxx = iii+1;
if (dataArray[xxx][0] == "-1")
break;
}
return tmp1;
}
void cdata
::parseInput(QString otext
) {//checks to see if the country dataArray[iii][0] is listed in otext
for (int iii = 0; iii<192; iii++)
{
int xxx = iii+1;
if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //I believe that this is the problem
{
dataArray[iii][1] = "yes";
}
else
{
dataArray[iii][1] = "no";
}
if (dataArray[xxx][0] == "-1")
break;
}
}
{
otext = ui.tmpTextinput->toPlainText();
return otext;
}
{
//calls cdata class
cdata d;
d.popcountries(); //in cdata.cpp and populates first column of the array
MainWindow mw; //calls mainwindow class
input = mw.oText(); //in cdata.cpp and returns the users input to search for
d.parseInput(input); //parses the input;compares against the first column of dataAray[iii][0]; writes "yes" or "no" to column two; and in parseInput.cpp
tmp = d.debugresults(); //debug results. Used for displaying the final results.
return tmp;
}
void MainWindow::on_parse_B_clicked()
{//button clicked and program starts
tmp = start();
ui.debugResultsTxt->setPlainText(tmp);
}
class cdata
{
public:
void popcountries();
QString debugresults();
void parseInput(QString);
private:
QString dataArray[192][192];
};
void cdata::popcountries()
{
QFile file;
QString filename = "countriesList.txt";
file.setFileName(filename);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream txtstream(&file);
for (int iii=0; iii<192; iii++)
{
QString tmp = txtstream.readLine();
dataArray[iii][0] = tmp;
int xxx = iii+1;
if (txtstream.atEnd() == TRUE)
{
for (xxx ; xxx<192; xxx++)
{
QString tmp2 = "-1";
dataArray[xxx][0] = tmp2;
}
break;
}
}
file.close();
}
QString cdata::debugresults()
{
QString tmp1 = "Test results:,\n";
for (int iii=0; iii<192; iii++)
{
QString tmp2 = dataArray[iii][0];
QString tmp3 = dataArray[iii][1];
tmp2.append(" , " + tmp3 + " , ");
tmp1.append(tmp2 + " \n");
int xxx = iii+1;
if (dataArray[xxx][0] == "-1")
break;
}
return tmp1;
}
void cdata::parseInput(QString otext)
{//checks to see if the country dataArray[iii][0] is listed in otext
for (int iii = 0; iii<192; iii++)
{
int xxx = iii+1;
if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //I believe that this is the problem
{
dataArray[iii][1] = "yes";
}
else
{
dataArray[iii][1] = "no";
}
if (dataArray[xxx][0] == "-1")
break;
}
}
QString MainWindow::oText()
{
QString otext;
otext = ui.tmpTextinput->toPlainText();
return otext;
}
QString start()
{
//calls cdata class
cdata d;
d.popcountries(); //in cdata.cpp and populates first column of the array
QString input;
MainWindow mw; //calls mainwindow class
input = mw.oText(); //in cdata.cpp and returns the users input to search for
d.parseInput(input); //parses the input;compares against the first column of dataAray[iii][0]; writes "yes" or "no" to column two; and in parseInput.cpp
QString tmp;
tmp = d.debugresults(); //debug results. Used for displaying the final results.
return tmp;
}
void MainWindow::on_parse_B_clicked()
{//button clicked and program starts
QString tmp;
tmp = start();
ui.debugResultsTxt->setPlainText(tmp);
}
To copy to clipboard, switch view to plain text mode
What could I be doing wrong? I wrote a similar program that uses only a 1d array and it works fine.
Bookmarks