1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

need a little help on an easy problem

Discussion in 'C++' started by cpulocksmith, Mar 23, 2009.

  1. cpulocksmith

    cpulocksmith New Member

    Joined:
    Jul 23, 2008
    Messages:
    289
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    student
    Location:
    canada
    ok well this is kinda dumb and i run into it alot. i always find a way around it but i dont know how to solve it. here is the code, it is a part of some stupid thing i am making please help me out.
    the problem is that when even you enter "command" it always outputs the response for help. please help. lol.

    the code
    Code:
    cin>>commands;
    
    
    //will output the help info
    if (commands == "help" || 'h'){
    cout<<"help files"<<endl;
    cout<<"\n\n\n ok?"<<endl;
    cin>>a;
    gameplay();
    }
    
    if (commands == "dive" || 'd'){
    cout<<"how far?"<<endl;
    cin>>varDepth;
    comments = "you have dove down "; varDepth;
    currentDepth = currentDepth + varDepth;
    gameplay();
    }
    
     
  2. jayaraj_ev

    jayaraj_ev New Member

    Joined:
    Aug 29, 2007
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Bangalore
    Hi,
    The reason is simple..
    ur trying to compare ( (commaand == "help") || 'h')
    which is always true.try to compare it with

    (command== "help") ||
    (command == "h")
     
    Last edited: Mar 23, 2009
    shabbir likes this.
  3. jayaraj_ev

    jayaraj_ev New Member

    Joined:
    Aug 29, 2007
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Bangalore
    Sorry just compare using

    strcmp or else it will check for ptr address validation.
     
  4. cpulocksmith

    cpulocksmith New Member

    Joined:
    Jul 23, 2008
    Messages:
    289
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    student
    Location:
    canada
    ah, thank you jayaraj_ev. i know it would be easy i just never read how to compair two strings, thanks
     

Share This Page