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

5
  • 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(). Commented Aug 15, 2012 at 14:09
  • stackoverflow.com/questions/347949/… Commented Aug 15, 2012 at 14:10
  • 1
    Just a general hint - if something goes wrong with standard functions, 99.99% of the time it's your fault. Commented Aug 15, 2012 at 14:12
  • 1
    Your un-use of the member functions makes me want to guide you to this reference page. Look through and get a feel of what's available. Commented Aug 15, 2012 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. Commented Aug 15, 2012 at 14:17

5 Answers 5

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.

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

Comments

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.

2 Comments

So not all C functions can be used in C++?
@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).
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); } 

Comments

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

Comments

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())

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.