I currently have a problem where the program when it is running will also choose one word and one letter in the word file. For example, in the word file the program only chooses the 2nd word "Dependent" The only letter that is considered correct is "d"
My code is :
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string LoadFile();
void RunProgram(string word);
void displayHangMan();
string LoadFile() {
int counter = 0;
ifstream textfile;
string s;
string words[20];
textfile.open("wordfile.txt");
for (counter = 0; !textfile.eof(); counter++)
{
getline(textfile, s);
words[counter] = s;
}
int r = rand() % counter;
textfile.close();
return words[r];
}
void RunProgram(string word) {
int r;
int WordSize;
int guessesLost = 0;
int totalGuesses = 8;
int lettersRemaining;
string letter;
char guess;
bool guessChecker;
WordSize = word.size();
lettersRemaining = WordSize;
for (r = 0; r < word.size(); r++) {
letter += '*';
}
while (totalGuesses > 0 && guessesLost < 8) {
cout << "Guess a letter: ";
cin >> guess;
for (r = 0; r < word.size(); r++) {
if ((char)tolower(guess) == (char)tolower(word[r])) {
if (guess == word[r]) {
cout << "Correct" << endl;
break;
}
}
else {
cout << "One Guess Lost, You have " << totalGuesses-- << " left" << endl;
guessesLost++;
break;
}
}
for (r = 0; r < word.size(); r++) {
if (guess == word[r]) {
cout << guess << endl;
lettersRemaining--;
break;
}
else {
cout << "*";
}
}
}
if (guessesLost != totalGuesses) {
cout << "Sorry, User ran out of chances " << endl;
cout << "The word was : " << word << endl;
system("PAUSE");
}
else if (lettersRemaining == 0) {
cout << ("User wins! The word was: ") << word << endl;
system("PAUSE");
}
}
void main(){
cout << "-------Hangman Game -------" << endl;
string word = LoadFile();
RunProgram(word);
}