开发者

How to compare string with const char*?

开发者 https://www.devze.com 2023-01-01 05:09 出处:网络
#include <stdlib.h> #include <stdio.h> #include <iostream> #include <string.h> using namespace std;
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
        string cmd;
        while(strcmp(cmd.c_str(),"exit")==0 && strcmp(c开发者_运维知识库md.c_str(),"\exit")==0)
        {
                cin>>cmd;
                cout<<cmd;
        }
        return 0;
}

I am stuck.


A std::string instance can be compared directly with a string literal using != or == operators. This makes your comparison clearer.

Note that \e isn't a valid character escape, you need to double the \ if you meant a literal \\.

while( cmd == "exit" && cmd == "\\exit" )

Obviously cmd can't be equal to two different strings at the same time, presumably you meant !=.

Also, consider whether std::getline( std::cin, cmd ) is more appropriate than std::cin >> cmd;. In either case you should check for success of the read operation otherwise you may end in an infinite loop if the stream is closed or enters a failed state.

Personally I'd go with something like this, assuming that you want to echo the exit command as your code does.

#include <string>
#include <iostream>
#include <ostream>

int main()
{
    std::string cmd;
    while (std::getline(std::cin, cmd))
    {
        std::cout << cmd << std::endl;
        if (cmd == "exit" || cmd == "\\exit")
            break;
    }
    return 0;
}


After fixing a couple of small bugs, this works on my machine:

#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>

int main()
{
        std::string cmd;
        while( std::strcmp(cmd.c_str(),"exit")!=0
            && std::strcmp(cmd.c_str(),"\\exit")!=0)
        {
                std::cin>>cmd;
                std::cout<<cmd << '\n';
        }
        return 0;
}

However, I wonder why you want to use std::strcmp() at all. As you have just found out, it's not as easy to use as the std::string class. This

while(cmd!="exit" && cmd!="\\exit")

works just as well, is easier to understand, and thus easier to get right.


strcmp returns 0 when they are equal. So I think you want != 0

Surely strcmp won't return 0 for both, because it can't be equal to both.

Also it looks like you have a backslash at the start of your string, you should escape that with a double backslash.


The condition in your while will never evaluate to true because you're testing to check the cmd string is equal to "exit" and "\\exit". One string can never be equals to two values at the same time.


Your problem are the while conditions.

You probably would want do exit the loop when the user enters exit, so you should use:

while(strcmp(cmd.c_str(),"exit")!=0 && strcmp(cmd.c_str(),"\exit")!=0)


Just a few things to keep in mind, i am reiterating some of the suggestions that are worth repeating while(1) times.

  1. You are using C++, it is object oriented, i.e., Its best to combine together the data and the function that works on it. In this case use the string compare options provided by string class rather than strcmp.

  2. There is a logic error in your program, well it will compile, but i am afraid thats not what you want. if ( a == x && a == y ) this will always be false, as a cannot be both equal to x and y unless x=y, in your case clearly x!=y.

Cheers, Pavan

0

精彩评论

暂无评论...
验证码 换一张
取 消