Comparing if 2 strings are equal?

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!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <time.h>

using namespace std;

signed int 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/

Hope this helps.
Last edited on
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
Here is the function
1
2
3
4
5
6
7
8
signed int linear_search(string a, char target) {
    for(int i = 0; i < a.length(); i++) {
        if(target == a[i]) {
            return i;
        }
    }
    return -1;
}
it doesn't work with double letters but im going to work on that later
Topic archived. No new replies allowed.