How to compare string with const char*?

前端 未结 6 1923
一生所求
一生所求 2021-02-19 06:20
#include 
#include 
#include 
#include 
using namespace std;
int main()
{
        string cmd;
        whil         


        
6条回答
  •  灰色年华
    2021-02-19 07:26

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

    #include 
    #include 
    #include 
    #include 
    #include 
    
    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<

    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.

提交回复
热议问题