C++ I have a hangman related project, how do I compare \"- - - - -\" which is be
ID: 3591163 • Letter: C
Question
C++
I have a hangman related project, how do I compare "- - - - -" which is being dynamicly written. to the given word from user 1 to, for example "hello" I already have it set up so that it will write from the length of the given word to += "- " so whatever word given will give the appropriate amount of spaces. But I cant flag the word as correct that the word was solved so it will turn from "- - - - -" into "h e l l o", but it will keep running. and my original while loop wont work for this i found out way later so I was hoping to turn it into some kind of bool flag. I had it set to while(strHangman != strWordGiven) {}; but after i solved my issue of setting it up to replace the '-' in the appropriate positionsi realized it will NEVER == 'hello'. Is their some kind of work around or way I can flag it so its correct when the word is solved? and I can't change it back to '-----' as much as i'd like to. it needs to be '- - - - -'.
CODE: // Not everything is posted. leaving a lot of functions out. should help get an idea of my problem though.
int main()
{
int i = 0;
string sPlayer1;
string sPlayer2;
string sSurprise;
string sHangman;
int y = 1;
int lGuess = 5; // letter attempts
int wGuess = 5; // word attempts
string sLetter;
string sWord;
//intro();
i = menu(i);
//cout << i;
int v = 0;
bool numGuess = false;
if (i == 1)
{
sPlayer1 = player1();
cout << "Welcome to the game, " << sPlayer1 << endl;
cout << "We're going to pull from our pool of words for you to try to guess from! ";
}
else if(i == 2)
{
sPlayer1 = player1();
sPlayer2 = player2();
cout << "Welcome players, " << sPlayer1 << " & " << sPlayer2 << " to our game. may the best player win. " << endl;
if (sPlayer1 < sPlayer2) //not sure how to randomize 2 variables so whoevers name is bigger goes first.
{
cout << sPlayer1 << " why dont you go first and pick the word for the game, no peeking " << sPlayer2 << endl;
sSurprise = word1(sPlayer1, sPlayer2);
for (int x = 0; x < sSurprise.length(); x++)
{
sHangman += "- ";
}
while (numGuess == false) // still need to make it check for when hangman is completed when all of - - - are filled out.
{
cout << sPlayer2 << " this is your word to solve." << endl;
cout << sHangman << endl;
if (y % 2) //even;
{
y++;
cout << "You have " << lGuess << " guesses left. Guess a letter: ";
cin >> sLetter;
while (sLetter.length() > 1)
{
cout << "You did not enter a letter! " << sPlayer2 << ". Try again: ";
cin >> sLetter;
}
for (int z = 0; z < sSurprise.length(); z++)
{
if (sLetter[0] == sSurprise.at(z))
{
if (z == 0) // pos 0
{
sHangman.at(z) = sSurprise.at(z);
}
else // pos 2, 4, 6, 8 etc
{
sHangman.at(z + z) = sSurprise.at(z);
}
}
v++;
}
if (v == sSurprise.length())
{
lGuess = lpointLost(lGuess);
v = 0;
}
if (lGuess == 0)
{
numGuess = true;
}
if (numGuess == true)
{
cout << "You ran out of attempts to try to guess. Play again." << sPlayer1 << " wins this game. ";
}
}
Explanation / Answer
Comments:
1. I added a variable letters_found to check how many letters got filled and I'm running loop to find No. of letters filled in sHangman.
2. While loop terminates when letters_found reaches length of surprise word.
3. Find code i added with comments //Added by me
4. Comment full code if this is not working and want a running code Thanks.
int main()
{
int i = 0;
string sPlayer1;
string sPlayer2;
string sSurprise;
string sHangman;
int y = 1;
int lGuess = 5; // letter attempts
int wGuess = 5; // word attempts
string sLetter;
string sWord;
//intro();
i = menu(i);
//cout << i;
int v = 0;
bool numGuess = false;
if (i == 1)
{
sPlayer1 = player1();
cout << "Welcome to the game, " << sPlayer1 << endl;
cout << "We're going to pull from our pool of words for you to try to guess from! ";
}
else if(i == 2)
{
sPlayer1 = player1();
sPlayer2 = player2();
cout << "Welcome players, " << sPlayer1 << " & " << sPlayer2 << " to our game. may the best player win. " << endl;
if (sPlayer1 < sPlayer2) //not sure how to randomize 2 variables so whoevers name is bigger goes first.
{
cout << sPlayer1 << " why dont you go first and pick the word for the game, no peeking " << sPlayer2 << endl;
sSurprise = word1(sPlayer1, sPlayer2);
for (int x = 0; x < sSurprise.length(); x++)
{
sHangman += "- ";
}
int surLen = sSurprise.length(); //Added by me
int letters_found=0; //Added by me
while (numGuess == false && letters_found<surLen) // Added by me.
{
cout << sPlayer2 << " this is your word to solve." << endl;
cout << sHangman << endl;
if (y % 2) //even;
{
y++;
cout << "You have " << lGuess << " guesses left. Guess a letter: ";
cin >> sLetter;
while (sLetter.length() > 1)
{
cout << "You did not enter a letter! " << sPlayer2 << ". Try again: ";
cin >> sLetter;
}
for (int z = 0; z < sSurprise.length(); z++)
{
if (sLetter[0] == sSurprise.at(z))
{
if (z == 0) // pos 0
{
sHangman.at(z) = sSurprise.at(z);
}
else // pos 2, 4, 6, 8 etc
{
sHangman.at(z + z) = sSurprise.at(z);
}
}
v++;
}
//Added by me
letters_found=0;
//Check how many letters got filled
for(int k=0;k<sHangman.length();k++){
if(sHangman[k]!=' ' || sHangma[k]!='-')
letters_found+=1;
}
//till here
if (v == sSurprise.length())
{
lGuess = lpointLost(lGuess);
v = 0;
}
if (lGuess == 0)
{
numGuess = true;
}
if (numGuess == true)
{
cout << "You ran out of attempts to try to guess. Play again." << sPlayer1 << " wins this game. ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.