I managed to do this, but it returns it as a string:
string PickaLine(){
fstream file;
file.open("MyPhrases.txt");
string line2;
int y=0;
string selected_line; // added
char thelinetouse;
line2=y;
selected_line=y;
int nlines = 0;
while( getline( file, line ) )
if( ( rand() % ++nlines ) == 0 ) // break;
selected_line = line;
return thelinetouse;
}
Also I found this on the internet, it returns a char, but only one word, not a whole sentence:
char* PickaLine2(){
ifstream in ( "MyPhrases.txt" );
char word[22];
srand((unsigned)time(0));
if ( !in )
return 0;
int r = rand() % linescount; // Assuming 10,000 words in the file
do
in>> word;
while ( --r >= 0 );
cout<< word;
return word;
}
The txt files has one phrase per line, no more than 20 characters, including spaces which should also be included in the return value.
Please help

Start a new topic
Add Reply




MultiQuote


| 


