How to compare string with const char*?

前端 未结 6 1171
灰色年华
灰色年华 2021-02-19 06:51
#include 
#include 
#include 
#include 
using namespace std;
int main()
{
        string cmd;
        whil         


        
6条回答
  •  攒了一身酷
    2021-02-19 07:01

    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.

提交回复
热议问题