im stuck, cant fix..HELP

This is a discussion on im stuck, cant fix..HELP within the C++ Programming forums, part of the General Programming Boards category; I have written a small game program text program using if and else commands. when i compiled it i got ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,470

    im stuck, cant fix..HELP

    I have written a small game program text program using
    if and else commands.

    when i compiled it i got this message:

    " C++ forbids comparison between pointer and integer"

    below is the error line of my code:

    if (attack =="y") {

    its really got me as the rest of my program is fine.
    does anyone know what the error means??

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,673
    "y" is a string literal and considered a pointer for purposes of the comparison in that bit of code. You can't test for equality in the manner you are trying to do it. If you are testing C-style strings (characters array) you need to use the strcmp function. If you are using C++ string containers then you can do it as you have it. If you are just testing for a single character, you can do this:

    Code:
    char attack;
    ...
    if( attack == 'y' )
    Make sure your attack variable is an appropriate type to be comparing in the test.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,470
    thanks hk_,mp!! it works great!! one more question, when the player takes the hit, is there any way of displaying it on the screen, here is my example:

    int PlayerH = 10; // declare varible

    // you get hit so

    PlayerH=PlayerH-5;

    how do i display on the screen that there is only 5 helf points left??

    any help would be greatly appriciated

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    cout<<"You have "<<PlayerH<<" points left.";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. compiling my code, i can't fix error
    By howru in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 03:38 AM
  3. sort of stuck
    By bobbie18 in forum C Programming
    Replies: 5
    Last Post: 04-04-2008, 04:38 PM
  4. C++ code need help to fix
    By McReal in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2007, 02:48 PM
  5. Replies: 6
    Last Post: 10-23-2006, 07:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21