How to compare string with const char*?

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


        
相关标签:
6条回答
  • 2021-02-19 07:05

    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 讨论(0)
  • 2021-02-19 07:16

    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.

    0 讨论(0)
  • 2021-02-19 07:20

    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.

    0 讨论(0)
  • 2021-02-19 07:25

    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)
    
    0 讨论(0)
  • 2021-02-19 07:26

    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.

    0 讨论(0)
  • 2021-02-19 07:26

    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;
    }
    
    0 讨论(0)
提交回复
热议问题