I have this function and the compiler yells at me saying “Cannot convert string to const char”.
void
DramaticLetters(string s, short TimeLength)
{
for(int i = 0, sLen = strlen(s); i < sLen; i++){
cout << s[i];
Sleep(TimeLength);
}
}
There’s something wrong with the strlen, I think
strlen()is for Cconst char*strings. To get the length of a strings, uses.size()ors.length(). If you want to get a C string from astring, uses.c_str().Although C++ makes it seem that
const char*andstringare interchangeable, that only goes one way when convertingconst char*tostring.There is no reason why you would want to use
strleneither.strlenis most likely defined with a loop, which will never be as efficiant assize(), which is most likley just a getter for a length property of thestringclass. Only convertstringto C strings when calling C functions for which there is not a C++ alternative.