for loop in hangman game

closed account (L092y60M)
Ok here I have a program that reads a word from a text file randomly and matches it with the definition. The user has to guess what the word is according to the definition.

I'm having trouble with my for loop, I'm not getting any errors. But I just know something is off.

Here's my code:
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
#include <iostream>
#include <fstream>
#include <string> 
#include <cstdlib>
#include <ctime>

using namespace std;

int main(){
	int number;
	int count = 0;
	int numOfGuess = 0;
	string guess;
	string word;
	string definition;
	ifstream inFile;

	inFile.open("words.txt");
	
	srand(time(0));
	number = rand() % 10 + 1;
	
	for (int i = 0; i < number; i++)
	{
			getline(inFile, word, '#');
			getline(inFile, definition);
	}
	cout << word << endl;

	int l = word.length();
	
	while (numOfGuess <= 4){
		cout << "enter in guess: " << endl;
		cin >> guess;
		numOfGuess++;
		if (word == guess){
			cout << "you win!!!" << endl;
		}
		else
		for (int i = 0; i < l; i++){
			for (int j = 0; j < guess.length(); j++){

				if (guess[j] == word[i]){
					cout << word[i];
				}
				else
				if (guess[j] != word[i]){
					cout << " _";
					
				}
			}
		}
		
	}


	system("pause");
	return 0;

}

This is words.txt:
apple#the usually round, red or yellow, edible fruit of a small tree
boat#a vessel for transport by water
horse#a solid-hoofed plant-eating domesticated mammal with a flowing mane and tail, used for riding
television#a system for transmitting visual images and sound that are reproduced on screens
soup#a liquid dish, typically made by boiling meat, fish, or vegetables, etc.
bottle#a container, typically made of glass or plastic and with a narrow neck
barber#a person who cuts hair
toast#sliced bread browned on both sides by exposure to radiant heat
radar#a system for detecting the presence, direction, distance, and speed of aircraft, ships, and other objects
red#of a color at the end of the spectrum next to orange and opposite violet
Based on your description, you want to print "definition" not "word" in line 28.

Otherwise, I don't see what's wrong with it. Maybe give more details of the sort of output you're expecting. Please allow for cultural differences, I'm not sure I know what hangman is, and if I played it as a kid, I must have completely blocked that memory out for some reason.

Here's my guess:

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	int number;
	int count = 0;
	int numOfGuess = 0;
	string guess;
	string word;
	string definition;
	ifstream inFile;
	
	inFile.open("word.txt");
	
	srand(time(0));
	number = rand() % 10 + 1;
	
	for (int i = 0; i < number; i++)
	{
		getline(inFile, word, '#');
		getline(inFile, definition);
	}
	cout << definition << endl;
	
	while (numOfGuess <= 4)
	{
		cout << "enter in guess: " << endl;
		cin >> guess;
		numOfGuess++;
		if (word == guess)
		{
			cout << "you win!!!" << endl;
			break;
		}
		else
		{
			if (numOfGuess > 4)
			{
				cout << numOfGuess << " guesses is the limit, goodbye." << endl;
			}
			else
			{
				string tmp(word.length(), '_');
				for (int i = 0; i < guess.length(); i++)
				{
					for (int j = 0; j < word.length(); j++) // better use string::find family here
					{
						if (guess[i] == word[j])
						{
							tmp[j] = guess[i];
						}
					}
				}

				cout << tmp << endl;
			}
		}
	}

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.