So I'm making a hangman game and the problem I'm having involves me using a random number generator
in order to pick a line from a text file. As such I get a random number but, I don't know how to
cout a specific line of the text file using the random number I got.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
usingnamespace std;
int main()
{
unsigned seed = time(0); //Gets the system time
int y = 1; // sets y as a int value type
srand(seed); // Seed the random number generator
string word;
int random;
fstream inputFile;
inputFile.open("genwords.txt", ios::in);
/*const int HM_SIZE = 100;
string word[HM_SIZE];
int choose = y;
*/
constint MIN_VALUE = 1;
constint MAX_VALUE = 100;
y = (rand() % (MAX_VALUE - MIN_VALUE +1)) + MIN_VALUE; // Limits the range of random numbers to be
// 1 -100
cout << y << endl; //couts the random number
cout << "Please re-enter your number\n";
cin >> random; // asks the user to enter their number
getline(cin, word); // gets a line from the file
inputFile << word << endl;
cout << word << endl;
inputFile.close();
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int num = 9; // this is the line we want
ifstream fin("data.txt");
string line;
int count=0;
while (getline(fin, line) && ++count < num)
; // empty loop body
if (fin)
cout << "line number " << num << ":\n" << line << endl;
else
cout << "file read error" << endl;
return 0;
}