Results 1 to 9 of 9

Thread: Getting a random string from a QStringList

  1. #1
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4

    Default Getting a random string from a QStringList

    I am trying to read in a group of words from a file, then select a random word. This is what I have, it makes sense to me logically, but obviously its wrong, because you cannot pull a QString from a QStringList. Yes, this is a homework assignment, but it is part of a very large project, a dictionary to be exact.
    Qt Code:
    1. QFile inputFile("C:\\Users\\hisownfoot\\Documents\\Dictionary_3\\wordList_new.txt");
    2.  
    3. // Create an inputstream to handle the file
    4. QTextStream in(&inputFile);
    5.  
    6. // Insert the text from the file into the QString dictionaryString
    7. QString dictionaryString = in.readAll();
    8. inputFile.close();
    9.  
    10. // Fills the QStringList with each word in the dictionary string, which are all
    11. // seperated by 2 spaces.
    12. wordList = dictionaryString.split(" ");
    13.  
    14.  
    15.  
    16. int n = wordList.size();
    17. int random_selector;
    18.  
    19. random_selector = (rand()%n)+1;
    20.  
    21. QString gameword;
    22. gameword = wordList.at(random_selector);
    23.  
    24. ui->textBox->append(gameword);
    To copy to clipboard, switch view to plain text mode 

    I receive a runtime error upon running this specific area of the program, and it causes it to crash.

    Invalid parameter passed to C runtime function.

    That is what appears in the application output, and using debugging i determined the line it goes bad at is,

    gameword = wordList.at(random_selector);
    Last edited by nickb; 20th October 2010 at 00:35.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting a random string from a QStringList

    What is the value of random_selector when the program fails? Why do you add one to your random selector value?

  3. #3
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4

    Default Re: Getting a random string from a QStringList

    the value of random selector is 1 every time the program fails, but regardless it should output the wordlist.at(1) right?

    And your right, the +1 makes no sense.

  4. #4
    Join Date
    Oct 2010
    Location
    Poland
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting a random string from a QStringList

    Whai is value of wordList.size() ?

    Try replace method at( int ) by value( int ). Method value( int ) should work even if int will be out of bounds.http://doc.trolltech.com/4.7/qlist.html#value
    Maybe a problem is in different place.

  5. #5
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4

    Default Re: Getting a random string from a QStringList

    It appears that wordList.size() is = to 1. I just dont know what to do, it does not have to be done my way specifically, I just need to grab one random word from a text file.

  6. #6
    Join Date
    Oct 2010
    Location
    Poland
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting a random string from a QStringList

    If size is 1 then you have only one "word" in wordList. (rand()%1)+1 = 1 but you had a text only under 0 index.

    Qt Code:
    1. wordList = dictionaryString.split(" ");
    To copy to clipboard, switch view to plain text mode 

    In this line is something wrong. Could you give a wordList_new.txt? Maybe separator which you insert into split is bad.

  7. #7
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4

    Default Re: Getting a random string from a QStringList

    accordion acorn acrobat actor actress adult advertisement aim air airplane airport terminal, thats the start of it, but there are around 1200 words in it. That exact command works elsewhere in my program.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting a random string from a QStringList

    Opening the file before trying to read it might be helpful.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4

    Default Re: Getting a random string from a QStringList

    Fantastic, thank you for your sarcasm laced but efficient and perfect reply.

Similar Threads

  1. USB Keyboard is not responding in qt-embedded-4.4.3
    By grsandeep85 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 7th April 2011, 03:13
  2. Designer not responding to mouse
    By brtip in forum Newbie
    Replies: 0
    Last Post: 28th July 2010, 05:09
  3. QGraphicsTextItem Linkactivated not responding
    By Pancho in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2009, 22:39
  4. Window not responding
    By markcole in forum Qt Programming
    Replies: 10
    Last Post: 18th April 2007, 21:53

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.