The dreaded hangman program...
I can't, for the life of me, figure out why my program won't accept any input from the user. I feel it's simply organization or bad use of functions. Every time I enter a letter to guess, it says it isn't a single letter, when it is. Please help me, I've lost almost all my hair over this.
#include "pch.h"
#include<iostream>
#include<stdlib.h>
#include<string>
usingnamespace std;
int main() {
string wordtoguess;
string lines;
string userGuess;
char inletter;
int i;
int guess = 5;
int len;
cout << "Insert a word for player two to guess: ";
cin >> wordtoguess;
for (int i = 0; i < wordtoguess.length(); i++)
{
lines += "_ ";
}
system("cls");
cout << lines << endl;
while (guess > 0)
{
cout << "You have " << guess << " guesses left. Guess a letter: ";
cin >> userGuess;
//checking if player entered single char or not
while (true)
{
if (userGuess.length() == 1)
{
inletter = userGuess[0];
}
cout << "You did not enter a letter! Try again: ";
cin >> userGuess;
}
//find letters guessed and positions
string guess_positions = "";
for (int i = 0; i < wordtoguess.length(); i++)
{
if (wordtoguess[i] == inletter || abs(wordtoguess[i] - inletter) == 32)
{
lines[2 * i] = inletter;
char numstr[21];
sprintf_s (numstr, "%d", i);
guess_positions = guess_positions + numstr; //store position
}
}
if (guess_positions.length() > 0)
{
cout << "You guessed the letters at position ";
for (i = 0; i < guess_positions.length(); i++)
{
if (i == 0)
{
cout << guess_positions[i];
}
else
{
cout << " and " << guess_positions[i];
}
}
cout << " correct" << endl;
cout << lines << endl;
//check all places got filled in lines wordtoguess
len = 0;
for (i = 0; i < lines.length(); i++)
{
if (lines[i] != ' '&&lines[i] != '-') {
len += 1;
}
}
//decide to continue or win
if (len == wordtoguess.length())
{
cout << "You win!! You guessed the word right in " << (5 - guess) + 1 << " guesses." << endl;
}
cout << "You have " << guess << " guesses left. Take a guess at the word:";
cin >> userGuess;
//compare actual word with guessed word i.e, each char
if (wordtoguess.length() == userGuess.length())
{
len = 0;
for (i = 0; i < userGuess.length(); i++)
{
if (wordtoguess[i] == userGuess[i] || abs(wordtoguess[i] - userGuess[i]) == 32)
{
len++;
}
}
if (len == wordtoguess.length())
{
cout << "You win!! You guessed the word right in " << (5 - guess) + 1 << " guesses." << endl;
}
else
{
guess--;//if entered word is not same decrease guess count
}
}
else
{
guess--;//if entered word is not same decrease guess count
}
}
else
{
guess--;//if entered word is not same decrease guess count
}
}
if (guess = 0)
{
cout << lines << endl;
cout << "Sorry! you have lost the game! Secret word is: " << wordtoguess << endl;
}
return 0;
}
^^ also I would recommend creating a hangman class or even breaking the code up into functions,the main function should be generally as concise as possible right now it's quite hard to read and if you are looking back at this code maybe in 6 months to a years time or so it will be very hard to decypher.