Hi, im making a hangman game and ive got most of my code working, but I cant terminate the program when you win. I try to compare puzzle and solution and if they are equal I want the game to end. But it does nothing Please help thank you!
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <time.h>
usingnamespace std;
signedint linear_search(string a, char target);
// returns the index value of character in string a
int main() {
ifstream guesswords;
guesswords.open("words.txt", ios::in);
if(guesswords.fail()) cout << "Error reading file";
vector <string> words;
string temp;
while(guesswords >> temp) {
words.push_back(temp);
}
int min=0;
int max=words.size()-1;
srand(unsigned( time(0) )); //seed random function with current time
int x = rand()% (max-min+1)+min; // x is the random word chosen. so if int x is 0 the word would be oranges
//cout << "random integer: " << x << '\n'; //display the random integer for testing
//chooses the word in the infile that corresponds to the random integer
string word = words[x];
//test to see if it chose the xth word out of the file...keep in mind first word in file is the 0th word, not the first word
cout << word << '\n';
string solution = word; //replaces the line string solution = "literalsolution";
string puzzle = solution; // create a copy of solution called puzzle and fill it with blanks and output it to the user
for(int i = 0; i < puzzle.length(); i++) {
puzzle[i] = '-';
}
int tries = 6; // start number of tries at 6
char guess; // guess of the user
// start of game
cout << puzzle;
do {
cout << '\n' << "Guess: ";
cin >> guess; //prompt user for guess
if(linear_search(solution, guess) == -1) { // linear search for guess, if guess is not present(wrong) subtract from tries // wrong works
tries --;
if(tries == 0) {
cout << "\nThe correct word was " << solution << ". You Lose!!";
break;
}
cout << "Wrong, only " << tries << " tries left.\n\n";
}
else { //if guess is right linear search and return index value of target. Set character of puzzle = to guess
if(puzzle == solution) {
cout << "Congratulations!!!!!! You Won with " << tries << " left!!";
break;
}
int indexValue = linear_search(solution, guess);
puzzle[indexValue] = guess;
}
cout << puzzle;
} while(tries > 0);
You might want to update the puzzle (lines 66-67) before testing to see whether it is correct (line 62).
Also, your randomization is not equally likely. Check out the stuff in <random> for a more useful randomizer. Or, if you want to stick with rand() (don't, though), check out the FAQ: http://www.cplusplus.com/faq/beginners/random-numbers/
Whats wromg with the puzzle? The programi s far from.finished it has a lot of stuff that need polishing. But is whats wrong with it what is affecting my comparison. The index value is equal to the return of a the linear search function i made it returns the index value if it finds the matching character and it returns -1 of if its wrong i forgot to include the body of my function