Trouble with If's and Else's

This is a discussion on Trouble with If's and Else's within the Game Programming forums, part of the General Programming Boards category; Kay, so I'm working on a text RPG, and I ran into a snag that if I try to use ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Trouble with If's and Else's

    Kay, so I'm working on a text RPG, and I ran into a snag that if I try to use a CHAR as a string (by making an array out of it) I can't compile, this is my code for initilizing my CHAR ARRAY:
    Code:
    char input[6];
    And this is for reading it off:
    Code:
        if (input=="left") {x=x-1;}          //Go Left
        else if (input=="right") {x=x+1;}    //Go Right
        else if (input=="north") {y=y+1;}    //Go North
        else if (input=="south") {y=y-1;}    //Go South
        else if (input=="jump") {if (rope_above==true) {z=z+1;} else cout<< "Nothing above me\n";//Jump up, if a rope is above, grab it
        else if (input=="dig") {if (hole_below==true) {z=z-1;} else cout<< "Nothing below me\n";//Dig if you can
    I am using Dev-C++ and I get this error:
    18 C:\Documents and Settings\Glitchguy2\Desktop\My Creations\Text Based Games\directions.h ISO C++ forbids comparison between pointer and integer

    Where it says "left" is what the action depends on, such as typing left to make x equal itself minus 1.
    Note that I am fairly experienced with C++ console game programming, but no where near the best.
    NOTE: These codes both take place in seperate HEADER files, however they do convers properly I think.
    Last edited by GlitchGuy2; 11-06-2008 at 05:56 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Strings are compared with strcmp (or some more secure version).

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    char arrays are compared using the strcmp function. Like so:

    Code:
    if(strcmp(input, "Left"))
         // Do stuff
    The way you are tying to do it, you would have to declare a string.
    Code:
    std::string input;
    
    if(input == "Left")
         // Do stuff

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    I think you need more lessons in C++ before tackling a game.
    Oh and code like this:
    Code:
        else if (input=="jump") {if (rope_above==true) {z=z+1;} else cout<< "Nothing above me\n";//Jump up, if a rope is above, grab it
        else if (input=="dig") {if (hole_below==true) {z=z-1;} else cout<< "Nothing below me\n";//Dig if you can
    Is only going to cause headaches.
    It should probably look like this:
    Code:
       else if (input == "jump")
        {
            if (rope_above == true)
                z = z + 1;
            else
                cout << "Nothing above me\n"; //Jump up, if a rope is above, grab it
        }
        else if (input == "dig")
        {
            if (hole_below == true)
                z = z - 1;
            else
                cout << "Nothing below me\n"; //Dig if you can
        }
    See the difference in the easiness in reading? I do, and I can even see you missed a }.
    Last edited by Elysia; 11-07-2008 at 05:27 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

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