Link to home
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?
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.