So here I am again, first off I wanted to say thank you to the people who helped me before but now I ran into another problem. Which involves me getting a word of which I want to mask (cover) so a user can't see the word. Overall this is for a hangman game I'm working on for class of which I really have no idea of doing. Anyway if someone is willing to give me some advice, it will be greatly appreciated.
#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;
char letter;
fstream inputFile;
inputFile.open("genwords.txt", ios::in);
cout << "Welcome to Hangman\n" << "Below this line is your random number please enter it it when prompted\n" << endl;
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 << "\n" << endl; //couts the random number
cout << "Please re-enter your number" << endl;
cin >> random; // asks the user to enter their number
int num = y; // this is the line we want
ifstream fin("genwords.txt");
string line;
int count = 0;
while (getline(fin, line) && ++count < num)
; // empty loop body
if (fin)
cout << "line number " << num << ":\n" << "*Your word*" << endl; // "line" is the output word
else
cout << "file read error" << endl;
cout << line << endl; // "line" is the output word
cout << "Enter a letter";
cin >> letter;
if (letter != line)
cout << "wrong choice";
else
cout << "Correct choice";
inputFile.close();
system("pause");
return 0;
}
Lines 31-33: Why do you ask the user to reenter the random number? What purpose does that serve?
Line 47: If you have a read error, you probably want to exit the program rather than continuing.
Line 54: You're comparing a character (letter) to a string (line). That's legal, but not what you want. You want to determine if letter occurs one or more times in line. You also want to keep track of which characters in the word have been unmasked.
It's so the program can grab a line from a file and cout it to the program, line 47 is not a problem and as for line 54 I was trying to figure how to mask the characters. My apologies for not being specific.