in c++ how do I get the size of a string array.
sterlen(arrayname) does not work or size(arrayname) either.

I am getting sterlen or size as idenitfier not found at compile time.

9 Years
Discussion Span
Last Post by WaltP
Featured Replies
  • 1
    WaltP 2,905   9 Years Ago

    Did you stop reading my post after the questions? I followed it with the answers... :rolleyes: Read More


ok here is what I got. I have
#include <string> and the for loop is

for (i = 0; i < FirstName.length(); i ++)
infile >> FirstName;

it still wont compile. now it says that left of length must have a class/strut/union type.


How is FirstName declared?

Does it look something like this:

std::string FirstName;

Or is it like this:

char FirstName[] = "Blah blah";

The latter will require what I edited into my post later (that is, strlen).


What compiler are you using? I don't have the .length() function in MSVC++
To get what are you are trying do to do, the way I would do it would be:


//Then whatever goes in between
//For your loop

Ah, I see what you're getting at.

Well, you could use the vector array example, or you could use a const to keep track of the array size:

const int ARRAY_SIZE = 10;
string FirstName[ARRAY_SIZE];

// ...

    for (int i=0; i < ARRAY_SIZE; i++) {
        // ...

srting FirstName[10];

Do you mean to make
1) an array of 10 strings
2) a string to hold 10 characters?

If 1, you reference the length with stringname[index].length() If 2, you create the string with char stringname[10] then reference the length with strlen(stringname)


an array to hold 10 strings

stringname.length() does not work

Of course it doesn't, you want the actual size of the container.:rolleyes:

I.e if there is ten strings in there you want it to return 10. As mentioned before if you declare it as

string stuff[10]

Just do for (int i=0; i<10; i++) If you were using a vector of strings you could have done. for ( int i=0; i < myVector.size(); i++) Again mentioned before.


Did you stop reading my post after the questions? I followed it with the answers... :rolleyes:

I know what you mean! - Salem.
This question has already been answered. 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.