Hangman SOS

Hey,
Got an assignment with little time to get it done. I'm really confused on how to tackle hangman. I read some other posts about it and I'm still lost. My major malfunction is how to replace characters and compare them.
For instance lets say I have a word seven characters long when I display it to the user it will be ******* Now they guess the letter H so then it outputs H******
and so and and so forth. I don't understand how to go about this. This is the code I have so far. I was just writing to test the functions.


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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream fin;

void DisplaynameAndGame()
{
	cout << "Word Guessing game by Jordan" << endl;



}


string GetWord(string& str1)
{
	fin >> str1;
	return str1;

}



void  codedWord(string str1)
{	
	int i = 0;
	int length = 0;
	
	
	length=str1.length(); 

	for (i=0 ; i<str1.length(); i++)
		cout << "*";
	
	

}







void main()

{	
	int coded= 0;
	string str1 = "";
	string star ="*";
	
	
	fin.open("input.txt");
	DisplaynameAndGame();
	  GetWord(str1);
	 cout << str1;
	
	 codedWord(str1);
	
system("pause");

}


I thought that with the codedWord function I would display characters as long as the word that was to be guessed. But this is a problem because I don't know how to compare the word to be guessed to the ***** characters since it is not a string just a loop that outputs a certain number of times based on the str1 length.


Man did I hit a wall in my thoughts. I really need some help with this concept. Thanks
In C++, a string is a type of array. The size of an external array (ie, an array passed in from outside the function) cannot be determined. The solution to this is to pass the array and the length of the array into the funcion, like this:

void codedWord(string str1, int length)
Last edited on
If I have correctly understood you then you can use the idea from the following code snip


1
2
3
4
5
6
7
8
9
10
11
12
13
std::string s( "Hello World" );
std::string t( s.size(), '*' );

std::string::size_type pos = 0;
		
while ( ( pos = s.find( 'o', pos ) ) != std::string::npos )
{
	t[pos] = 'o';
	pos++;
}

std::cout << s << std::endl;
std::cout << t << std::endl;


The only thing you should do is ti substitute std::find_if algorithm for member function find that to be able to compare letters independently of case.
The post by BrintSpinor is false. If you pass a string into a function, you can still get its length with string.length(). Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<string>
using namespace std;

int length(string s)
{
return s.length();
}

int main()
{
string t("testing testing 123");
cout << length(t) << endl;

return 0;
}


1
2
//Output of above program:
19


Edit: To clarify, he's kind of correct in that you can't determine the size of a normal array, but that doesn't have anything to do with being inside of a function. And for strings, you can determine their size anywhere as the .length() function is as much a part of the class as the actual characters.
Last edited on
Why not use a 2d array, the first holds the characters of the word and the second the stars. If the user inputs a correct letter that matches within the first demension, the program replaces the * at the position that corresponds to the second demension with the letter.

Or you could just use two arrays:

1
2
char wordtobeguessed[] = "HammerTime";
char guessedletters[] =  "**********";


Then every time the user inputs a letter, if it matches at position X, you replace a star at position X with the letter and display the guessedletters array.

You would need to use dynamic arrays for his to work. Or you could c++string since it has [] operator built into the class.
Last edited on
Thanks for the IDEAS!!!!!!
Topic archived. No new replies allowed.