Hi I'm slightly new by C++ standards to this lanaguge and i was wondering if there is a swift way to send the getline function back to the top of the file with out closing and reopening it? the code is attached and below it currently will just start repeating output when it reaches eof. Sorry if this is some dumb question with a super easy solution I am kinda new (basic pointer knowledge) and i cant find help through google.
const void DyResActor::printRndLine()
{
int selection;
string line;
ifstream respGre(gFile);
int i = 0;
int past[4];
bool rep;
srand(time(NULL));
for (int l = 0; l < 3; l++)
{
past[l] = 120;
}
if(fstLn && !isPlayer)
{
fstLn = false;
selection = rand() % greLen;
for(i; i < selection; i++)
{
getline(respGre, line);
}
getline(respGre, line);
}
else if(fstLn && isPlayer)
{
fstLn = false;
while (i < 3)
{
i++;
do
{
selection = rand() % greLen;
for (int k = 0; k < 3; k++)
{
if(selection != past[k])
{
rep = false;
}
else
{
rep = true;
}
}
} while (rep);
past[i] = selection;
for (int j = 0; j < selection; j++)
{
getline(respGre, line);
}
getline(respGre, line);
cout << line << endl;
}
cout << past[3];
}
}
gFile.txt contents:
Hi
Hello
Salutaions
Greetings
Hai
Hoi :3
Sup
Expected output (3 lines from gFile selected at random):
Sup
Hoi :3
Hi
----------
Actual output:
Greetings (or other random line)
Sup
Sup