Thread: Problems Comparing Char's

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    England, Norwich
    Posts
    10

    Problems Comparing Char's

    Hello All,

    To start with i think tell you that i started learning c++ about 2 years ago and abanded it but i found that it is the only true programing language that is really any good, so basically i'm back to being a complete beginner.

    my problem is with a program which I'm creating I'm trying to make sort of virtual command line for the program here is the code:

    Code:
    int main()
    {
    char command[256];
    int cmdloop;
    
    EchoProjectInfoIntoConsole();
    
    cmdloop = 0;
    
            while (cmdloop == 0) {
            cout<< "#";
            cin>> command;
            if (strncmp(command,"help",4)){
    
            }else{
                cout<< "@ Invaild Command\n";
            }
    
            }
    
    	return 0;
    }
    as i'm sure you can see i'm trying to compare the char's being using the srtncmp function when i run the program and type the ommand nothing happens sept it says that the command is invaild.

    can anyone help me please?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    strncmp() returns 0 when the strings match.
    So you need
    if ( strncmp(command,"help",4) == 0 )

    But look into using std::string to store strings, not char arrays.
    They're a lot safer, and have many more features.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    England, Norwich
    Posts
    10
    Thank you extreemly much for your help, i had been trying to figure that out for hours.

    your a life saver!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  2. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM
  3. Comparing Chars
    By Jperensky in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 12:29 PM
  4. Comparing Chars
    By drdroid in forum C++ Programming
    Replies: 12
    Last Post: 02-26-2002, 02:06 PM
  5. comparing chars with pointer to char
    By papous in forum C Programming
    Replies: 5
    Last Post: 11-24-2001, 01:23 AM