Apologies in advance for the elementary nature of the question.
I am trying to use the strcmp
function to test two strings for matching characters.
I reduced the issue to the simple code below:
#include <iostream>
#include <cstring>
using namespace std;
void compareStrings(string, string);
int main()
{
string string1 = "testString", string2 = "testString";
compareStrings(string1, string2);
return 0;
}
void compareStrings(string stringOne, string stringTwo)
{
if (strcmp(stringOne,stringTwo) == 0)
cout << "The strings match." << endl;
else
cout << "The strings don't match." << endl;
}
Could someone explain the following compiler error message?
./newProgram.cpp: In function ‘void compareStrings(std::string, std::string)’:
./newProgram.cpp:17:32: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’
if (strcmp(stringOne,stringTwo) == 0)
^
Thanks! Xz.
strcmp
with C++ strings? – Jerid