PDA

View Full Version : Creating Hangman in C++




Slip
May 16, 2009, 06:52 AM
Hi, I've ran in to a slight hiccup with creating a console based Hangman game.

I'm basing the code off of an example I was given at college written in VisualBasic. Basically, it takes a phrase from the user and then creates a char array of either '*' or ' ' which is then displayed and updated everytime the user guesses a letter correctly (here's the source code):

Dim Position As Integer
For Position = 1 To Len(NewPhrase)
IndividualLettersArray(Position) = Mid(NewPhrase, Position, 1)
If IndividualLettersArray(Position) = " " Then
GuessStatusArray(Position) = " "
Else
GuessStatusArray(Position) = "*"
End If
Next

Now when I try and do the same thing in C++:

Length = Phrase.length();

while (Position <= Length)
{
if (Phrase.at(Position) == " ")
{
GuessArray[Position] = " ";
}

else
{
GuessArray[Position] = "*";
}

Position = Position++;
}

I get an error: error: ISO C++ forbids comparison between pointer and integer.

I'm writing the code as plain text, saving as .cpp and the compiling the code with g++ in Terminal if that helps.

I've searched various way's of getting a character from string and always the same error.

Any help would be greatly appreciated!



jpyc7
May 16, 2009, 08:14 AM
Hi, I've ran in to a slight hiccup with creating a console based Hangman game.

. . .

Now when I try and do the same thing in C++:

Length = Phrase.length();

while (Position <= Length)
{
if (Phrase.at(Position) == " ")
{
GuessArray[Position] = " ";
}

else
{
GuessArray[Position] = "*";
}

Position = Position++;
}

I get an error: error: ISO C++ forbids comparison between pointer and integer.

I'm writing the code as plain text, saving as .cpp and the compiling the code with g++ in Terminal if that helps.

I've searched various way's of getting a character from string and always the same error.

Any help would be greatly appreciated!

Try changing the double quotes (around the asterisk and space character) into a single quotes. On my keyboard, the double quote is the shifted key of the single quote.

Sander
May 16, 2009, 10:13 AM
jpyc7 is right: You are comparing characters to strings.

By the way, your construct Position = Position++; might not be what you intended.

Slip
May 18, 2009, 06:43 AM
Thank you, worked perfectly. :)