0

I want to compare user input (text) with already declared string in c++ under linux (code::blocks) i made something like this, but it doesn't work

int LoadModule()
{
    string test1 = "LOAD DEFAULT DATABASE";
    string test2;
    cout << "select cmd: " << endl;
    cin >> test2;
    //todo: compare thoose 2 strings. And if they are equal, print "done"
    return 0;
}

1 Answer 1

2

You'll need to use std::getline() to input a std::string containing white space characters:

std::getline(std::cin, test2);

doing so a simple comparison

if(test1 == test2) {
    std::cout << "Done!" << std::endl;
}

should work.


You should note that std::istream& operator>>(std::istream&, std::string&) will just copy from input up to the first whitespace character (' ', '\t', '\n') is met.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.