I need help with a fairly simple C++ program I'm working on (hangman). I'm using two different cases; the first case is where the word is defined by the person running the program and the second case is where the word is guessed.
In the second case, there's just something that goes wrong in the do-while loop where the word is guessed. It doesn't replace the question marks with any correctly guessed letters, and it's impossible to guess the right word. I can't figure out what's wrong. Help would be much appreciated!
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string word = "MONKEY";
string secretword;
char guess;
int b;
b = word.length();
for (int w = 0;w < b;w++)
{
secretword.push_back('?');
}
do{
cout << "Word: " << secretword << endl << endl;
cout << "Guess a letter! " << endl << endl;
cin >> guess;
for(int i = 0; i++; i < b)
{
if (guess == word[i])
{
secretword[i] = guess;
}
}
system("cls");
}
while(secretword != word);
cout << "Congratulations! You have correctly guessed the word " << secretword <<"! " << endl << endl;
system("pause");
system("cls");
}
In the code attached, I only included the second case, and I already defined the word (MONKEY) for simplicity. I didn't bother to limit the amount of guesses until I get the other things working.
Note: We haven't studied functions yet, so I'm not supposed to use those. But if that's the only way to solve the problem, then that's fine as it's a pretty important assignment.
For lines 13 to 18, you can simply replace all these lines on 11 with a constructor: std::string secretword(word.length(), '?');
That will construct the same string as lines 13 to 18.
Lynx876:
Thank you for taking the time to look at it. I figured the error would have to do with the loop, but I don't know what's wrong to be honest. Could you give me a clue about what's wrong with the program?
wolfgang:
Thanks for the tip! Although, I will need to keep the 'b' integer as that's used in the loop.
Okay guys, I have another problem. I've done a lot more work on other parts and the program is basically done, there's just one thing I can't get working. Storing incorrect guesses. I'll only be posting the important part of the code, once again.
All of this is part of a larger for-loop that runs up to the maximum amount of attempts (a). The 'a' integer is the number of attempts, the maximum limit is 13. So please disregard that it isn't declared in the code below.
The first if statement after the for-loop is something I use so that the displayed number of remaining attempts (I did not attach that part of the code) doesn't change if the user guessed correctly. That part actually works perfectly.
This makes it difficult for me to understand why the else statement causes a "string subscript out of range" error. Does anyone know why?
I hope the fact that I didn't include the rest of the code doesn't make it difficult to understand. I've written 200 lines so I figured that would be too much. If you want me to clarify anything, then just ask.
EDIT: I've tried several different ideas, and I always get the subscript out of range error.
I'm putting the [a] after characters so that each character will be saved in different parts of the string, in the order they are guessed. It was the only way I could think of to get that working. If you know of a better way, I would be more than willing to use a different method. :)
I no longer get the out of range error. But it's not working the way it's supposed to, still. When I enter an incorrect letter, the printed string looks something like "0023F998", for example, rather than printing the incorrect letter.
This code does what you want to do. Take a look at it and apply it to your situation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
#include <sstream> //You will need to add this library to your project.
int main()
{
int a = 0, n = 0;
std::ostringstream x;
while(a < 15)
{
std::cout << "Enter number: ";
std::cin >> n;
x << n << " "; //Add the number to our string stream, with a space.
++a;
}
std::cout << "String is: " << x.str() << "\n"; //This will output contents of the stringstream x.
return 0;
}
I do not think a 2d array is what you are looking for, or any kind of array for that matter.
I did it a different way. Just posting for variation.
Here's how the strings are defined:
1 2 3 4 5
//create a copy of the word
std::string wordCopy = word;
//change the word in to *'s
wordCopy = createWordCopy( wordCopy );
Then, create a copy of the word, but in *'s:
1 2 3 4 5 6 7 8 9 10 11 12
std::string createWordCopy( std::string wc )
{
for( unsignedint letter = 0; letter < wc.length(); ++letter )
{
// if the current letter is NOT a space,
// or the NULL character change the letter to a *
if( ( wc[ letter ] != ' ' ) && ( wc[ letter ] != '\0' ) )
wc[ letter ] = '*';
}
return wc;
}
This function takes the word to be guessed, the word copy and the key that was entered:
bool checkKey( const std::string w, std::string &wc, constchar key )
{
int found = 0;
for( unsignedint letter = 0; letter < w.size(); ++letter )
{
//if the key pressed matches a letter in the
//word to be guessed...
if( w[ letter ] == key )
{
//the wordCopy( which is *'s )
//copy in to the *'s the actual letter
wc[ letter ] = w[ letter ];
++found;
}
}
//if a letter is found/not found, return accordingly
if( found > 0 )
returntrue;
elsereturnfalse;
}
Wow thanks guys, really appreciate it. I did what Mats suggested using a single string and I've got it working now.
However, I have one minor problem. When the game is finished and I return to the main menu to play the game once more, all of the incorrect letters from the previous game show up.
This is despite the fact that I have placed characters.clear() where I should, in order to clear the string before a new game. Is there a different method that should be used to clear the string?