Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:59:07+00:00 2026-06-16T00:59:07+00:00

I am looking to access a completely random line of a small text file,

  • 0

I am looking to access a completely random line of a small text file, and import the same line in another text file in a C++ program. I need to do this fairly simply, I am a beginner to C++ programming. I will include main.cpp. If you need the other .cpp or the .h, just let me know, I will post it.

Main.cpp:

#include <fstream>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>

#include "getQuestion.h"

using namespace std;

int main() {
    int mainMenuChoice;
    ifstream Bibliography;
    //string easyBib;
    string easyBib;
    ifstream inputFile;

    cout << "Quiz Menu\n\n";
    cout << "1. Play Game!\n";
    cout << "2. Bibliography\n";
    cout << "3. Developer Info\n";
    //cout << "4. Admin Menu\n";
    cout << "4. Exit\n";
    cout << "Menu Choice: ";
    cin >> mainMenuChoice;

    switch (mainMenuChoice) {
    case 1:
        //int getQuestion(string Q,A);
        //cout << Q;
        break;
    case 2:
        inputFile.open("Bib.rtf");
        inputFile >> easyBib;
        cout << easyBib << endl;
        break;
    case 3:
        cout << "Program made by: XXXX XXXXXXXX" << endl;
        cout << "XXX. XXXXXXX'X Period 4 Social Studies Class" << endl;
        break;
    /*case 4:
        break;*/
    case 4:
        cout << "Thank you for playing!" << endl;
        return(0);
    default:
        cout << "Sorry, Invalid Choice!\n";
        return(0);
    }
    return(0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:59:08+00:00Added an answer on June 16, 2026 at 12:59 am

    The easiest solution would be to read the entire file line by line (using getline) into a vector<string>. Then it’s trivial to select a random element from that vector.

    You can read a line from an input stream like this:

    string line;
    getline( inputFile, line );
    

    It returns a reference to the stream, which can be tested directly for error. So this is easily turned into a loop like this:

    vector<string> lines;
    for( string line; getline(inputFile,line); )
    {
        lines.push_back(line);
    }
    

    Now you can use the size function of vector to determine how many lines you have read, and then choose a random one.

    size_t iRandLine = rand() % lines.size();
    string randomLine = lines[iRandLine];
    cout << "Line " << (iRandLine+1) << ": " << randomLine << endl;
    

    Of course, you need to know that there will be fewer than RAND_MAX lines in your file. Otherwise you’ll have to combine multiple calls to rand just to cover the range.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to open an Access MDB file inside a Java
I am looking to get access to all HTTP traffic on my machine (my
I am looking for a java API to access mainframe remotely. I am looking
I am looking for a way in Microsoft Access to create a temporary table
I am looking for an OSS data access block for using with .Net and
I'm looking for feedback on the Data Access Object design pattern and using it
I am looking for a data access layer for ado.net. I am not interested
I am looking at a way to access parameter passed from different Url to
I'm looking to see if it's possible to access the value of a key
What I'm looking for is a way to mimic the MS-Access style continuous form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.