I added a line to this program to read the length of the the string , then display the test. strlen() is not cooperating! Am I using the wrong function? the compiler says the arg should be char*. I'm confused!

#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector <string> strv;

int main()
{
        string input;

        do
        {
                getline (cin,input);
        strv.push_back(input);
        }
        while(input !="x");
        cout << "exited first loop" << endl;

        vector <string>::iterator itr = strv.begin(); //put itr init here instead
        while (itr != strv.end())
        {

        cout<< << "length= "<< strlen(*itr) << " "<< *itr++  << endl;

        }
return 0;
}

Recommended Answers

All 2 Replies

*itr returns a string, not a char*. Use the .length() for the string instead.

*itr returns a string, not a char*. Use the .length() for the string instead.

Yes, that's what I needed. Thanks
I also had to fix the line so that the results printed where they belonged and stuck an equivalent method of member addressing on the end for kicks.

cout<<  "length= "<< ((*itr).length())<< " "<< *itr <<" " << itr -> length() <<endl;
        itr++;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.