-5

Where is this code wrong? It won't let me cast strlen(s) into an int that I can iterate it until i is equal to the length of the string.

#include <iostream>
#include <string.h>
#include <string>

using namespace std;

int main()
{

   string s;
   cin >> s;

   for (int i = 0; int n = (int)strlen(s); i < n, i++)
   {
      cout << s[i] << endl;
   }
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • use `s.size()` `strlen` is for `char` array strings. – 101010 Oct 07 '15 at 06:25
  • Do I have to change the header file? – Louis Germanotta Oct 07 '15 at 06:27
  • 1
    You only have to `#include ` and `#include ` – 101010 Oct 07 '15 at 06:28
  • I think the underlying problem is that you don't know C++ well enough. – RamblingMad Oct 07 '15 at 06:29
  • I just started learning, so fair enough. – Louis Germanotta Oct 07 '15 at 06:30
  • 1
    If you're learning C++ from a good source (such as a [good book](http://stackoverflow.com/q/388242/1782465)), I would expect that source to teach you about `std::string` and its member functions *long before* the first mention of `strlen` and similar C-legacy stuff. If they're teaching you C++ by starting with C, *they're doing it wrong.* – Angew is no longer proud of SO Oct 07 '15 at 06:32
  • It wouldn't let me use < ... > – Louis Germanotta Oct 07 '15 at 06:32
  • I'm learning CS50 online, but it's a different language than C++, and I'm forced to use C++ with no background on the language, because I can't run a virtual computer on my laptop. – Louis Germanotta Oct 07 '15 at 06:34
  • Then you might want to also follow a C++ learning source (such as one of the books I linked to) to learn C++ (assuming you want to learn it, that is). – Angew is no longer proud of SO Oct 07 '15 at 06:36
  • 1
    @LouisGermanotta `and I'm forced to use C++ with no background on the language, because I can't run a virtual computer on my laptop` Sorry that makes no sense at all. Why would you need a VM in order to learn the language ? – Blacktempel Oct 07 '15 at 06:39
  • Because I have to turn my work in that way. It's stupid. but thanks for your advice – Louis Germanotta Oct 07 '15 at 06:42
  • 1
    @LouisGermanotta The start of CS50 seems to focus on C, using its own type named "string". Doing the course in C++ if it teaches C will only complicate things. (It honestly doesn't seem like a very good course.) – molbdnilo Oct 07 '15 at 06:47
  • I mean C is used mostly at Apple to make games and stuff, and eventually that's what I want to do . – Louis Germanotta Oct 07 '15 at 06:56
  • 2
    @LouisGermanotta No, it isn't. Very few games or other applications are made in C these days, particularly at Apple (they don't make any games at all). (You are aware that C and C++ are different languages, right?) CS50 seems more geared towards web development than games. – molbdnilo Oct 07 '15 at 07:02

3 Answers3

3

The problem is not that the result of strlen can't be cast to an int, but that strlen can't be used with std::string.

strlen is for "C strings", zero-terminated arrays of characters.

std::string has a size member function:

int main()
{
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i++)
    {
        cout << s[i] << endl;
    }
}

There is nothing inside the C header <string.h> that applies to std::string.
A reference can be useful.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

Because strlen expects to get a char const* as it's argument. You should probably use s.size() instead as s is a std::string.

In addition you should probably not compute strlen inside a loop like that due to performance issues (s.size on the other hand should be able to complete in O(1) time so it would be OK).

Then there's a few other problems with your program, already at line one. You can't include a file by using #include //iostream, you should use #include <iostream> instead.

Second you cannot declare a variable in the condition of a for loop as you try to do, and you probably shouldn't assign instead of comparing (one equal sign is not the same as two). You should have written for( int i = 0; i != s.size(); i++ )

Third you shouldn't do the check in the update expression in the for construct. What goes there will only be evaluate to update the loop variables and such and the truth value will be disregarded.

Overall I think you should pick up an introduction to C++ book or find a good tutorial online. Your code simply has to much problems to conclude that you have learnt even the most basic C++.

skyking
  • 13,817
  • 1
  • 35
  • 57
  • I'm learning CS50, not C++. I'm being forced to learn the way CS50 works by doing it in C++, because I am unable to run virtual computers, but thanks. – Louis Germanotta Oct 07 '15 at 06:39
  • The // ....// was because it didn't look like it would show #include in the post – Louis Germanotta Oct 07 '15 at 06:41
  • 1
    @LouisGermanotta In that case it looks like you have to learn C++ as part or prerequisite of the course, at least the basics... – skyking Oct 07 '15 at 06:44
0

strlen is for char arrays.

std::string has a member function called size and length.

If you still want to use strlen, you can call c_str and pass the returned string to strlen.

std::size_t length = s.size();
for (int i = 0; i < length; ++i)

You can also iterate over the elements of the std::string:

for (std::string::iterator itr = s.begin(); itr != s.end(); ++itr)
{
    std::cout << *itr << std::endl;
}
Blacktempel
  • 3,935
  • 3
  • 29
  • 53