Results 1 to 9 of 9

Thread: Using an array and QString contains

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using an array and QString contains

    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::



    Qt Code:
    1. class cdata
    2. {//creation of the cdata class. This is created to protect dataArray from external influences, but can still be accessed through select functions.
    3. public:
    4. void popcountries();
    5. QString debugresults();
    6. void parseInput(QString);
    7.  
    8. private:
    9. QString dataArray[192][192];
    10.  
    11. };
    12.  
    13.  
    14. void MainWindow::on_parse_B_clicked()
    15. {//button clicked and program starts
    16.  
    17. QString tmp; //creates the QString for results to be shown
    18. tmp = start(); //start()'s results are the results to be shown
    19. ui.debugResultsTxt->setPlainText(tmp); //results shown
    20. }
    21.  
    22. QString start()
    23. {
    24. //calls cdata class from above
    25. cdata d;
    26.  
    27. d.popcountries(); // populates first column of the array from the given file
    28.  
    29. QString input;
    30. MainWindow mw; //calls mainwindow class
    31. input = mw.oText(); //returns the text to be searched through as the user's input
    32.  
    33. d.parseInput(input); //uses the user's input to see if it contains dataArray[iii][0] (the first column of the array)
    34.  
    35. QString tmp; //creates a qstring to return the final/debug results
    36. tmp = d.debugresults(); //debug results. This is used to format the final results as they would appear in the csv file.
    37.  
    38. return tmp; //returns the formatted final/debug results to on_parse_B_clicked()
    39.  
    40. }
    41.  
    42. void cdata::popcountries()
    43. { //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
    44. QFile file;
    45. QString filename = "countriesList.txt";
    46. file.setFileName(filename);
    47. file.open(QIODevice::ReadOnly | QIODevice::Text);
    48. QTextStream txtstream(&file);
    49. for (int iii=0; iii<192; iii++)
    50. {
    51.  
    52.  
    53. QString tmp = txtstream.readLine();
    54. dataArray[iii][0] = tmp;
    55.  
    56. int xxx = iii+1;
    57. if (txtstream.atEnd() == TRUE)
    58. {
    59. for (xxx ; xxx<192; xxx++)
    60. {
    61. QString tmp2 = "-1";
    62. dataArray[xxx][0] = tmp2;
    63. }
    64. break;
    65. }
    66.  
    67. }
    68. file.close();
    69.  
    70. }
    71.  
    72.  
    73. QString MainWindow::oText()
    74. { //grabs the user's input and returns it to start()
    75.  
    76. QString otext;
    77. otext = ui.tmpTextinput->toPlainText();
    78. return otext;
    79. }
    80.  
    81. void cdata::parseInput(QString otext)
    82. {//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.
    83.  
    84. for (int iii = 0; iii<192; iii++)
    85. {
    86.  
    87. int xxx = iii+1;
    88.  
    89. 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
    90. {
    91. dataArray[iii][1] = "yes"; //if it does, it writes yes in the next column
    92. }
    93. else
    94. {
    95. dataArray[iii][1] = "no"; //if it doesn't, it writes no in the next column
    96. }
    97. 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.
    98. break;
    99. }
    100.  
    101.  
    102.  
    103. }
    104.  
    105.  
    106. QString cdata::debugresults()
    107. { //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.
    108. QString tmp1 = "Test results:,\n";
    109. for (int iii=0; iii<192; iii++)
    110. {
    111.  
    112. QString tmp2 = dataArray[iii][0];
    113. QString tmp3 = dataArray[iii][1];
    114. tmp2.append(" , " + tmp3 + " , ");
    115. tmp1.append(tmp2 + " \n");
    116.  
    117. int xxx = iii+1;
    118.  
    119. if (dataArray[xxx][0] == "-1")
    120. break;
    121.  
    122. }
    123. return tmp1;
    124. }
    To copy to clipboard, switch view to plain text mode 

    Original code in post::
    Qt Code:
    1. class cdata
    2. {
    3. public:
    4. void popcountries();
    5. QString debugresults();
    6. void parseInput(QString);
    7.  
    8. private:
    9. QString dataArray[192][192];
    10.  
    11. };
    12.  
    13. void cdata::popcountries()
    14. {
    15. QFile file;
    16. QString filename = "countriesList.txt";
    17. file.setFileName(filename);
    18. file.open(QIODevice::ReadOnly | QIODevice::Text);
    19. QTextStream txtstream(&file);
    20. for (int iii=0; iii<192; iii++)
    21. {
    22.  
    23.  
    24. QString tmp = txtstream.readLine();
    25. dataArray[iii][0] = tmp;
    26.  
    27. int xxx = iii+1;
    28. if (txtstream.atEnd() == TRUE)
    29. {
    30. for (xxx ; xxx<192; xxx++)
    31. {
    32. QString tmp2 = "-1";
    33. dataArray[xxx][0] = tmp2;
    34. }
    35. break;
    36. }
    37.  
    38. }
    39. file.close();
    40.  
    41. }
    42.  
    43. QString cdata::debugresults()
    44. {
    45. QString tmp1 = "Test results:,\n";
    46. for (int iii=0; iii<192; iii++)
    47. {
    48.  
    49. QString tmp2 = dataArray[iii][0];
    50. QString tmp3 = dataArray[iii][1];
    51. tmp2.append(" , " + tmp3 + " , ");
    52. tmp1.append(tmp2 + " \n");
    53.  
    54. int xxx = iii+1;
    55.  
    56. if (dataArray[xxx][0] == "-1")
    57. break;
    58.  
    59. }
    60. return tmp1;
    61. }
    62.  
    63. void cdata::parseInput(QString otext)
    64. {//checks to see if the country dataArray[iii][0] is listed in otext
    65.  
    66. for (int iii = 0; iii<192; iii++)
    67. {
    68.  
    69. int xxx = iii+1;
    70.  
    71. if (otext.contains(dataArray[iii][0], Qt::CaseInsensitive)) //I believe that this is the problem
    72. {
    73. dataArray[iii][1] = "yes";
    74. }
    75. else
    76. {
    77. dataArray[iii][1] = "no";
    78. }
    79. if (dataArray[xxx][0] == "-1")
    80. break;
    81. }
    82.  
    83.  
    84.  
    85. }
    86.  
    87. QString MainWindow::oText()
    88. {
    89.  
    90. QString otext;
    91. otext = ui.tmpTextinput->toPlainText();
    92. return otext;
    93. }
    94.  
    95. QString start()
    96. {
    97. //calls cdata class
    98. cdata d;
    99.  
    100. d.popcountries(); //in cdata.cpp and populates first column of the array
    101.  
    102. QString input;
    103. MainWindow mw; //calls mainwindow class
    104. input = mw.oText(); //in cdata.cpp and returns the users input to search for
    105.  
    106. 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
    107.  
    108. QString tmp;
    109. tmp = d.debugresults(); //debug results. Used for displaying the final results.
    110.  
    111. return tmp;
    112.  
    113. }
    114.  
    115. void MainWindow::on_parse_B_clicked()
    116. {//button clicked and program starts
    117.  
    118. QString tmp;
    119. tmp = start();
    120. ui.debugResultsTxt->setPlainText(tmp);
    121. }
    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.
    Last edited by TomJoad; 11th April 2011 at 17:44.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. Converting QString to char array
    By srohit24 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 18:19
  3. Assign a wchar_t array to QString
    By jacky in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 12:28
  4. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 22:18
  5. how to copy QString content into array
    By eric in forum Qt Programming
    Replies: 12
    Last Post: 14th November 2007, 23:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.