Can you compare strings with if, or do you have to use something else? Can you compare strings at all? Im attempting to write a console program just for fun. Eventually I want it to be able to move around files and such but im not even close to that point yet. Right now im adding basic commands to see if I even have the knowledge to complete it.

int recognition (string command) {
    if (command == add) {
        add();
        };
    };

That is the recognition function. Im going to use it to recognize a command that a user enters(the only command right now is add) but to do that I need to compare the string command to see if it is add. Thanks in advance for the help.

Recommended Answers

All 5 Replies

You can use strcmp() function to check the strings.

Secondly.
I would also recommend that you get all the input in lowercase with tolower() function for each character of the string.

THis is because

ADD!=add!=adD

Thanks a lot. So if stringcomp() comes back as true I can use the if statement? Very helpful thanks again.

You can compare strings using the if-statement:
e.g:

string password = "password123";
string userinput = "lalala";

if(userinput == password)
{
    /* The strings are equal */
    /* REMARK: the evaluation is case-sensitive !! */
}

Thanks a lot. So if stringcomp() comes back as true I can use the if statement? Very helpful thanks again.

An if-statement evaluates a condition, and a condition can be TRUE or FALSE ...
Thus, everything what return true/false or something comparable can be evaluated using an if-statement ...

Hope this helps !

Thanks for the help sky diploma and tux4life, I got it working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.