0

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;
}
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by JRM
0

*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++;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.