Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Slip

macrumors 6502a
Original poster
Oct 16, 2007
904
0
Wiltshire, England
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!
 
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.
 
jpyc7 is right: You are comparing characters to strings.

By the way, your construct
Code:
Position = Position++;
might not be what you intended.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.