Background: I'm trying to open a text file from my computer and randomly output a random line of text (string) onto the console. How do I go about doing this?
So for example, if my text file, "StringTextFile.txt" had 50 rows of strings datas (not integers or chars), how would I randomly generate a line of word from the 50 rows of words in the StringTextFile.txt and output it onto the console?
This is how far I've gotten, which isn't much, but somewhere. I don't even know if my code even makes sense, at this point I'm just randomly shoving code together.
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//main
int main()
{
//variables
int randomNumber = rand() % 50 + 1; // seeding random number from [1 - 50]
int input[200]; // maximum capacity of array
//opening file for input
ifstream fin;
fin.open("StringTextFile.txt");
if (!fin.good()) throw"I/O Error.";
//creating empty list
constint MAX_STRINGTEXTS = 200; // list capacity
int nSTRINGTEXTS = 0; // initially empty score
int song[MAX_STRINGTEXTS]; // the array
while (!fin.good())
{
string STRINGTEXTS[50]; // number of songs in songs.txt
for (int i = 0; i < 50; i++) // the number of rows in StringTextFile.txt (50)
{
randomNumber;
getline(fin, STRINGTEXTS[i]); //retrieving a random line from the 50 lines of text in StringTextFile.txt
cout << STRINGTEXTS[i]; //outputting the randomly generated numbered, text onto the console.
} // for loop
} //while loop
fin.close(); //closing text file
} // main
I think I do not really understand your job? What exactly do you want to do? Getting randomly any one of 50 lines from a text file an write it on standard output? But your program only copies 50 input lines to standard output.