Hangman - runs, but has a logic issue?

Hi - I'm just getting started in programming. This game compiles & runs with 2 warnings;
-----------------------------------------------------------------------------
1>c:\users\jeff\documents\visual studio 2008\stringemup\stringemup\hangman.cpp(27) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\jeff\documents\visual studio 2008\stringemup\stringemup\hangman.cpp(135) : warning C4018: '<' : signed/unsigned mismatch
-----------------------------------------------------------------------------
and I have trouble with displaying letters that have already been guessed to the end user? Thanks for any advice.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

string THE_WORD; // word to guess
int wrong;
string soFar;
string used;

bool match(char letter, string word);
char askGuess(string usedLettersStr); //tells the compiler of method askGuess
bool playAgain();

int main()
{
srand(time(0)); //random selection of words in the list

vector<string> words; // collection of possible words to guess

words.push_back("ARMY");
words.push_back("IRAQ");
words.push_back("DRONE");
words.push_back("HANGMAN");
words.push_back("RECON");
words.push_back("DIFFICULT");
words.push_back("ROCKET");
words.push_back("BATTLE");
words.push_back("MORTAR");
words.push_back("DIGITAL");
words.push_back("PROGRAM");
words.push_back("TANK");

cout << "Welcome to Hangman. Good luck!\n";



// loop starts here
bool done = false;
do
{
const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed

random_shuffle(words.begin(), words.end());
THE_WORD = words[0]; // word to guess

soFar = string(THE_WORD.size(), '-'); // word guessed so far
used = ""; // letters already guessed

// loop for current word
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
cout << "\nYou've picked the following correct letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;

used += askGuess(used);

{



} // end of while ((wrong < MAX_WRONG) && (soFar != THE_WORD))

// shut down



}
if (wrong == MAX_WRONG)
{
cout << "\nYou've been hanged!";
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | / \\ "<<endl
<<" |You are dead! "<<endl
<<" ==============="<<endl<<endl;
}

cout << "\nThe word was " << THE_WORD << endl;

} while(playAgain());

return 0;
}

inline bool match(char letter, string word)
{
return ( word.find(letter) != string::npos );
}

char askGuess(string usedLettersStr)
{
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess); //make uppercase since secret word in uppercase
while (used.find(guess) != string::npos)
while (match(guess, used))
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}


// if (THE_WORD.find(guess) != string::npos)
if (match(guess, THE_WORD))
{
cout << "That's right! " << guess << " is in the word.\n";


}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}

// update soFar to include newly guessed letter
for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;

return 0;
}

bool playAgain() // function to play again while clearing system
{
char again;
cout << "\n\nWould you like to play again? <y/n>: ";
cin >> again;

cin.clear(); //clear and ignore cin
cin.ignore();

again = toupper(again);

system("cls");

return (again == 'Y');
}
It seems to run fine with this minor modification at line 120:

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

string THE_WORD; // word to guess
int wrong;
string soFar;
string used;

bool match(char letter, string word);
char askGuess(string usedLettersStr); //tells the compiler of method askGuess
bool playAgain();

int main()
{
	srand(time(0)); //random selection of words in the list

	vector<string> words; // collection of possible words to guess

	words.push_back("ARMY");
	words.push_back("IRAQ");
	words.push_back("DRONE");
	words.push_back("HANGMAN");
	words.push_back("RECON");
	words.push_back("DIFFICULT");
	words.push_back("ROCKET");
	words.push_back("BATTLE");
	words.push_back("MORTAR");
	words.push_back("DIGITAL");
	words.push_back("PROGRAM");
	words.push_back("TANK");

	cout << "Welcome to Hangman. Good luck!\n";

	// loop starts here
	bool done = false;
	do
	{
		const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed

		random_shuffle(words.begin(), words.end());
		THE_WORD = words[0]; // word to guess

		soFar = string(THE_WORD.size(), '-'); // word guessed so far
		used = ""; // letters already guessed

		// loop for current word
		while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
		{
			cout << "\n\nYou have " << (MAX_WRONG - wrong)
				<< " incorrect guesses left.\n";
			cout << "\nYou've picked the following correct letters:\n" << used
				<< endl;
			cout << "\nSo far, the word is:\n" << soFar << endl;

			used += askGuess(used);

			{

			} // end of while ((wrong < MAX_WRONG) && (soFar != THE_WORD))

			// shut down


		}
		if (wrong == MAX_WRONG)
		{
			cout << "\nYou've been hanged!";
			cout << endl << endl << " +----+ " << endl << " | | " << endl
				<< " | O " << endl << " | /|\\ " << endl << " | / \\ " << endl
				<< " |You are dead! " << endl << " ===============" << endl
				<< endl;
		}

		cout << "\nThe word was " << THE_WORD << endl;

	} while (playAgain());

	return 0;
}

inline bool match(char letter, string word)
{
	return (word.find(letter) != string::npos);
}

char askGuess(string usedLettersStr)
{
	char guess;
	cout << "\n\nEnter your guess: ";
	cin >> guess;
	guess = toupper(guess); //make uppercase since secret word in uppercase
	while (used.find(guess) != string::npos)
		while (match(guess, used))
		{
			cout << "\nYou've already guessed " << guess << endl;
			cout << "Enter your guess: ";
			cin >> guess;
			guess = toupper(guess);
		}

	// if (THE_WORD.find(guess) != string::npos)
	if (match(guess, THE_WORD))
	{
		cout << "That's right! " << guess << " is in the word.\n";

	}
	else
	{
		cout << "Sorry, " << guess << " isn't in the word.\n";
		++wrong;
	}

	// update soFar to include newly guessed letter
	for (unsigned int i = 0; i < THE_WORD.length(); ++i)
		if (THE_WORD[i] == guess)
			soFar[i] = guess;

	return 0;
}

bool playAgain() // function to play again while clearing system
{
	char again;
	cout << "\n\nWould you like to play again? <y/n>: ";
	cin >> again;

	cin.clear(); //clear and ignore cin
	cin.ignore();

	again = toupper(again);

	system("cls");

	return (again == 'Y');
}
Thanks Galik! I really appreciate the help.
- jf007
Topic archived. No new replies allowed.