L
L
Lisik2018-12-01 16:19:51
C++ / C#
Lisik, 2018-12-01 16:19:51

How to compare 2 entered words?

I'm trying with strcmp, but it doesn't work yet.

string name1;
  while (getline(cin, name1)) {
                     ...
      continue;
    }
    break;
  }
...
string name2;
  while (getline(cin, name2)) {
    ...
}
    else if (strcmp(name1, name2) == 1)
      cout << "..." << endl;
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2018-12-01
@Lisik

if (strcmp(name1, name2) == 1)

man strcmp
The strcmp() and strncmp() functions return an integer less than,
       equal to, or greater than zero if s1 (or the first n bytes thereof)
       is found, respectively, to be less than, to match, or be greater than
       s2.

To test for equality, it must be
if (strcmp(name1.c_str(), name2.c_str()) == 0)

R
Roman, 2018-12-01
@myjcom

You are reading into std::string.
https://ru.cppreference.com/w/cpp/string/basic_str...
https://ru.cppreference.com/w/cpp/string/basic_str...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question