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.
cout << "Word Guessing game by Jordan" << endl;
string GetWord(string& str1)
fin >> str1;
void codedWord(string str1)
int i = 0;
int length = 0;
for (i=0 ; i<str1.length(); i++)
cout << "*";
int coded= 0;
string str1 = "";
string star ="*";
cout << str1;
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:
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.
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.