This is a program for a simple hangman game. The issue right now is the out put it gives. If I enter tackle, it ends correctly saying you have won. Right now, with the else statement commented out, it just runs until tackle is entered. When I uncomment the else statement, that is where my problem comes into play. If I type just 't' for my first letter, it will say "that letter is in the word" and then repeat 5 more times saying it is not in the word. It also adds +5 to the tries variable and the next letter you type you instantly lose. Im just not sure where to go from here. Thanks for any help!
#include <iostream>
#include <string>
usingnamespace std;
int main() {
int tries = 0;
int right = 0;
char guess;
string word = "tackle";
int length = word.length(); // Sets length to the length of the string variable word
cout << length << endl;
cout << "Try to guess the word, you have six guesses!" << endl;
while(tries <= 6) {
cout << "Enter a letter: " << endl;
cin >> guess;
cout << "You guessed: " << guess << endl;
for(int i = 0; i < length; i++) {
if(word[i] == guess) { // Letter is in word
cout << "This letter is in the word!" << endl;
right++;
}
// else {
//
// cout << "This letter is not in the word :(" << endl;
// tries++;
// }
}
if(right == length) { // Win
cout << "You win!" << endl;
cout << "The word was " << word << endl;
break;
}
if(tries == 6) { // Lose
cout << "You lost!" << endl;
cout << "The word was " << word << endl;
break;
}
}
return 0;
}
Your if statement is in a for loop. If your else statement commented out your If statement will notify the letter is in the word then it goes back into the for loop and then checks to see if T is the 2nd letter in your word and then a third time and a fourth and then a fifth until every letter of tackle is checked against the T.
There are several solutions to this problem depending on the parameters of the assignment Let me know if you need more help!
I just have one more question at the moment, I moved the else statement around, turned it into an if statement trying various spots for different outcomes. I still can't get it to just read one letter at a time. Any suggestions for that? Thanks!
I think there are some flaws with your original code. If the user keeps typing the same letter that is found in "tackle", the "right" variable keeps increasing and you can win the game only by typing the same letter... I've made some changes. Now I think everything is correct. Check it out...
#include <iostream>
#include <string>
usingnamespace std;
int main() {
int tries = 0;
int right = 0;
bool found = false;
char guess;
string word = "tackle";
char guessed[6]; // This string gets all the correct letters you have already typed
int size = 1;
int validletter = false;
int aux = 0;
int length = word.length(); // Sets length to the length of the string variable word
cout << length << endl;
cout << "Try to guess the word, you have six guesses!" << endl;
while(tries <= 6) {
validletter = false;
if (aux==0)
{
cout << "Enter a 1st letter: ";
cin >> guess;
guessed[0] = guess;
cout << "You guessed: " << guess << endl;
} else {
while (validletter==false) // You can only get out of this loop by typing a valid letter.
{
cout << "\n\nEnter a letter: ";
cin >> guess;
cout << "You guessed: " << guess << endl;
validletter = true;
for (int i=0;i<size;i++)
{
if (guess==guessed[i])
{
cout << "\nYou already said that letter. Try again\n";
validletter = false;
i=size;
}
}
} }
aux++;
for(int i = 0; i < length; i++) {
found = false;
if(word[i] == guess) { // Letter is in word
cout << "This letter is in the word!" << endl;
i=length;
found = true;
right++;
guessed[size] = guess;
size++;
}
elseif (i==length-1 && found==false) {
cout << "This letter is not in the word :(" << endl;
tries++;
}
}
if(right == length) { // winning condition
cout << "You win!" << endl;
cout << "The word was " << word << endl;
break;
}
if(tries == 6 && right!= length) { // losing condition
cout << "You lost!" << endl;
cout << "The word was " << word << endl;
break;
}
}
return 0;
}