Avatar of Dmon443
Dmon443
 asked on

random line function

void readFromFile(QLabel &output)
{
    QString message;
    int numberOfLines = 10; //file has 10 lines of quotes
    int random = QRand() % numberOfLines; //create random number 1-10
    //open file and create stream
    QFile data("messages.txt");
    if (data.open(QIODevice::ReadOnly))
        QTextStream out(&data);
    //read random line of file
    for (int i = 1; i < random; i++) {

    }
    //read line of file to message and output to QLabel
    message = out.readLine();
    output.setText(message);
}

Open in new window

I am stuck on a this function I am trying to write  in Qt, which is supposed to read a random line from a text file and output it into a QLabel. Can't figure out how I would select a random line, I'm guessing I would need a loop to the random number, but don't know how to read that specific line? Please help? Am I on the right track here?
C++Programming

Avatar of undefined
Last Comment
HooKooDooKu

8/22/2022 - Mon
SOLUTION
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
HooKooDooKu

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes