Thread: I simple little problem but i cant figure it out

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    35

    I simple little problem but i cant figure it out

    alright my code works perfectly and it runs but my only problem is that whether or not i input the proper responce it always does the else statement

    Code:
    void Character::Command()
    {
         cout<<"\n";
         cout<<"Your Turn\n";
         cin>>command;
         cin.ignore();
         if(command=="Attack"||command=="attack"){Character::Attack();}
         else if(command=="Skill"||command=="skill"){Character::Skill();}
         else if(command=="Magic"||command=="magic"){Character::Magic();}
         else if(command=="Summon"||command=="summon"){Character::Summon();}
         else if(command=="Item"||command=="item"){Character::Item();}
         else{cout<<"(I don't think I should do that.)\n";Character::Command();}
    }
    whats my problem?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    35
    If needed I can add the full program so you can look at it all

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    35
    nevermind i fixed it but now it doesnt do something else its supposed to do

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I replaced the calls to member functions with couts and it worked fine for me....

    [edit]What did you fix? What is it doing that it shouldn't[/edit]
    There is a difference between tedious and difficult.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    35
    what did you do??
    I just put string command; and it fixed that problem i post the new problem in a sec

    Quote Originally Posted by Decrypt
    I replaced the calls to member functions with couts and it worked fine for me....

    [edit]What did you fix? What is it doing that it shouldn't[/edit]

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    35
    ok i fixed it thanks everything works now i just need to integrate the random so everything will make sence and not be pointless

  7. #7
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    oops, I forgot I declared command, too. I figured you had it elsewhere since it shouldn't compile without it....

    Do you have another string named command valid in that scope? If so, you should change the name one of them.
    There is a difference between tedious and difficult.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    35
    no i have char command[15]; which is what i was using as the string but appearntly it wasnt working so do I still need it?

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by SebastionV3
    no i have char command[15]; which is what i was using as the string but appearntly it wasnt working so do I still need it?
    Apparently not. The string container overloads the equality operator (==) to work as you were using it. If command is made to be a character array however, what you end up doing in that code fragment is trying to compare the address of the first array element with the address of those various string literals which are NEVER going to be equal... hence the else statement always being executed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    In addition to what the above poster said, you also might want to transform the user's command to lower-case or something so that you have less comparisons to make. Here's an example.
    Code:
    #include <algorithm>
    #include <cctype>
    #include <iostream>
    #include <string>
    
    int main(int argc, char *argv[])
    {
        std::string s("sTRIng to LowEr CAse");
        std::cout << s <<"\n";
        std::transform(s.begin(), s.end(), s.begin(), ::tolower);
        std::cout << s <<"\n";
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM
  2. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 04:07 PM
  3. Simple code :: Cant figure out the problem...
    By pritin in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:02 AM
  4. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  5. Simple boolean problem
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2001, 08:49 AM