Can somebody please tell me why this wont work?

Basically my whole program nearly works correctly, however i have made an if statement so if anyone enters anything other then a lowercase letter for the input of guess, it will tell print out something and then take them to the start of the while loop on line 78. However, instead it just executes the line 110 if statement
Thankyou to anybody who helps in advance :)

PS: I have no idea what code tags are so sorry if i haven't used them.


#include <iostream>
#include <array>
#include <time.h>

using namespace std;


int main()
{

srand(time(0));

array <string, 20>listOfWords =
{
"phenomenal",
"insane",
"haunting",
"immaculate",
"duck",
"read",
"onomatopoeia",
"eloquent",
"ridiculous",
"jurassic",
"veni vidi vici",
"who you gonna call",
"ghostbusters",
"tremendous",
"variable",
"the game",
"blue",
"cuckoo",
"danny devito",
"oscar"
};

int randomWordNumber = rand() % 20;
string guessWord = listOfWords[randomWordNumber];

array <char, 20>guesses;
guesses.fill('_');

for (int count = 0; count < guessWord.length(); count++)
{
if (guessWord[count] == ' ')
{
guesses[count] = ' ';
}
}

for (int count = guessWord.length(); count < 20; count++)
{
guesses[count] = ' ';
}

bool running = true;
int lives = 5;

array <char, 20>lettersGuessed;
lettersGuessed.fill(' ');

char guess;




/* USER INTERFACE GOES HERE. COMPLETE THIS SECTION */


cout << "Welcome to hangman!\nThe aim of the game is to guess letters in a word.\nEach underscore represents a letter!" << endl; // Line of code that explains the game to the user
for (int x = 0; x <= guessWord.length(); x++)
{ // this for loop prints out how many underscores there are for every letter
cout << guesses[x] << " ";
}
cout << endl << "You have 5 lives to start off with!" << endl; // So the user knows how many lives he starts with
cout<<"PLEASE ONLY ENTER LOWERCASE LETTERS!"<<endl; // I am asking the user to only print out lowercase letters otherwise the program will not work correctly

while (running == true)
{

cout << endl << endl;

cout << "Please enter your guess : \n";
// Where the user inputs there guess every time
cin >> guess;

if (guess == 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
{
bool test = false;
for (int i = 0; i <= guessWord.length(); i++)
{

if (guessWord[i] == guess)
{

guesses[i] = guess;

for (int x = 0; x <= guessWord.length(); x++)
{
cout << guesses[x] << " ";
}
cout << endl << endl << "Correct!" << endl << endl; // To let the user know that the letter they guessed is correct
cout << "You have " << lives << " lives remaining!\n" << endl; // Code shows how many lives the user has left every guess
test = true;
}
}

static int counter = 0;

if (test == false)
{
lives--;
lettersGuessed[counter] = guess;
counter++;
cout << "\nWrong!" << " You have " << lives << " lives remaining!" << endl; // Code shows how many lives the user has left after every guess



}
else
{
lettersGuessed[counter] = guess;
counter++;

}

if (lives < 1)
{
char letter_per_underscore_func;
cout << "YOU LOSE! The word was '";
for (int x = 0; x <= guessWord.length(); x++)
{
cout << guessWord[x];
}
cout << "'" << endl;
cin.get();
running = false;
}

bool winFlag = true;

for (int count = 0; count < guessWord.length(); count++)
{
if (guesses[count] != guessWord[count])
{
winFlag = false;
}

}

if (winFlag == true)
{
cout << endl << endl << endl << "CONGRATULATIONS YOU WIN! The word was '";

for (int x = 0; x <= guessWord.length(); x++)
{
cout << guessWord[x];
}
cout << "'" << endl;

cin.get();
running = false;
}

}
else
{
cout << "PLEASE ONLY ENTER LOWERCASE LETTERS! EXAMPLE: h " << endl << "Please try again!"<<endl;
}

}
cin.get();

system("cls");


return 1;
}
my quick guess would be that you did not use
#include <string>.
i have not read all your code yet.

i will read it and try to help more.
good luck
fix this as well
if (guess == 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')

with

if (guess == 'a' || guess == 'b' || guess == 'c' || guess == 'd'...

And anyway, don't even do that (because is awful) and use a switch instead

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	switch (guess)
	{
		case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':
		case 'g':case 'h':case 'i':case 'j':case 'k':case 'l':
		case 'm':case 'n':case 'o':case 'p':case 'q':case 'r':
		case 's':case 't':case 'u':case 'v':case 'w':case 'x':
		case 'y':case 'z':
		{
			//code
		}
		break;
		default:
		{

		}
		break;
	}
Last edited on
Thankyou so far bro
To force to lower case just:
guess = tolower( guess );

To test whether alphabetic, use isalpha( guess )
See: http://www.cplusplus.com/reference/cctype/isalpha/

Both require header <cctype>
Last edited on
Thanks for the responses, i made it a switch statement and it worked :D
yep, isalpha() && islower() are the sane solution :)
Last edited on
Yea I used isalpha an islower in the end at it looks more simpler
Topic archived. No new replies allowed.