Creating Hangman in C++

Discussion in 'Mac Programming' started by Slip, May 16, 2009.

  1. Slip macrumors 6502a

    Slip

    Joined:
    Oct 16, 2007
    Location:
    Wiltshire, England
    #1
    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!
     
  2. jpyc7 macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #2
    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.
     
  3. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    jpyc7 is right: You are comparing characters to strings.

    By the way, your construct
    Code:
    Position = Position++;
    might not be what you intended.
     
  4. Slip thread starter macrumors 6502a

    Slip

    Joined:
    Oct 16, 2007
    Location:
    Wiltshire, England

Share This Page