i have a text document with 10 words to guess. i have the code already read a random word from the file. then the user guesses a letter and it finds whether or not if the letter is in the word. my problem is how should i loop so it remembers the previous letter and outcome and builds on so it will complete the entire word. my teacher said this as a hint " I suggest you make a word of all blanks
then replace letters with the letters that are in the word to be guessed
you can use the position found by find to put the letter in the appropriate position." but how would i go about doing that any help would be greatly appreciated.
int main()
{
int number;
int count = 0;
int position;
string word;
char letter;
ifstream infile;
infile.open("words.txt");
srand(time(NULL)); //initializes the random number generator
while (count < 1)
{
number = rand()%10 +1; // rand returns a random integer from 0 to maxInt
count++;
}
for (count=0;count<number;count++)
getline (infile, word);
guessword(word);
cin.ignore(255,'\n');
cin.get();
return 0;
}
void guessword(string word)
{
char letter;
int position;
for (int i=0;i<5;i++)
{
cout << "What letter would you like to guess?";
cin >>letter;
position = word.find(letter);
if (position > word.length())
cout<<letter<< " is not in the word "<<endl;
else
{
cout<< letter << " is in the word"<<endl;
if(position==0)
cout<<letter<<" _ _ _"<<endl;
else if(position==1)
cout<<"_ "<<letter<<" _ _"<<endl;
else if(position==2)
cout<<"_ _ "<<letter<<" _"<<endl;
else if (position==3)
cout<<"_ _ _ "<<letter<<endl;
}