3

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

user1575615
  • 439
  • 1
  • 4
  • 7
  • 5
    Just use `s.length()` or `s.size()`. Strings don't implicitly convert to character arrays. If you really need one (which you don't for length), use `s.c_str()`. – chris Aug 15 '12 at 14:09
  • http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char?rq=1 – Matt K Aug 15 '12 at 14:10
  • 1
    Just a general hint - if something goes wrong with standard functions, 99.99% of the time it's your fault. – Cubic Aug 15 '12 at 14:12
  • 1
    Your un-use of the member functions makes me want to guide you to this [reference page](http://en.cppreference.com/w/cpp/string/basic_string). Look through and get a feel of what's available. – chris Aug 15 '12 at 14:12
  • @Cubic: Exactly. I've had experiences where something *had* to have been an standard lib error, but that was an error in my code. Anything you think is a bug is most likely implementation defined behavior. Just read the docs. – Linuxios Aug 15 '12 at 14:17

5 Answers5

4

strlen() is for C const char* strings. To get the length of a string s, use s.size() or s.length(). If you want to get a C string from a string, use s.c_str().

Although C++ makes it seem that const char* and string are interchangeable, that only goes one way when converting const char* to string.

There is no reason why you would want to use strlen either. strlen is most likely defined with a loop, which will never be as efficiant as size(), which is most likley just a getter for a length property of the string class. Only convert string to C strings when calling C functions for which there is not a C++ alternative.

Linuxios
  • 33,279
  • 13
  • 86
  • 114
1

You should not mix C and C++ string functions. Instead of strlen() (a C-style function), use string::size():

for(int i = 0, sLen = s.size(); i < sLen; i++) {
    cout << s[i];
    Sleep(TimeLength);
}

Here you have a reference with all methods from class string.

betabandido
  • 18,008
  • 10
  • 57
  • 72
  • So not all C functions can be used in C++? – user1575615 Aug 16 '12 at 01:34
  • @user1575615 C++ "reimplements" many of the C features (e.g., using class `std::string` instead of just a char array and `strlen`, `strcpy`, etc.). You can still use the C-style features, though. Actually you can make a C++ program that looks like a C one, but you should not mix C and C++ solutions, such as `std::string` (from C++) with `strlen`, `strcmp`, etc. (from C). – betabandido Aug 16 '12 at 08:06
1

As chris said in his comment, strlen is used for a const char *, whereas you're passing it a string. Instead of

for(int i = 0, sLen = strlen(s); i < sLen; i++){
    cout << s[i];
    Sleep(TimeLength);
}

Use this:

for(int i = 0, sLen = s.length(); i < sLen; i++){
    cout << s[i];
    Sleep(TimeLength);
}
Sean Cogan
  • 2,446
  • 2
  • 24
  • 42
0

strlen operates on char const *. If you really wanted to, you could do strlen(s.c_str()), but std::string has a lot of functionality, including a length() method, which returns the number of characters in the string

Tom Tanner
  • 9,095
  • 2
  • 31
  • 60
0

Few remarks:

  • You do not modify the string within the function so better pass a const reference to it(const string& s)

  • The string itself defines methods for getting its length - s.size() and s.length() both will work. Additional benefit is both this methods are constant with respect to complexity as opposed to the linear complexity of strlen

  • If you REALLY want to use strlen use s.c_str(): strlen(s.c_str())

Ivaylo Strandjev
  • 66,530
  • 15
  • 117
  • 170