954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

string array size

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.

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

<a href="http://www.cplusplus.com/strlen">strlen</a> should work since you say you're using "string arrays"

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

ok here is what I got. I have
#include 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.

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

srting FirstName[10];

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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:

#include<vector>
#include<string>

//Then whatever goes in between
//Declaration
vector<string>FirstName(10);
//For your loop
for(i=0;i<FirstName.size();++i)
infile>>Firstname[i]
Daniel E
Newbie Poster
15 posts since Apr 2007
Reputation Points: 12
Solved Threads: 2
 

Microsoft Visual Studion .net 2003

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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++) {
        // ...
    }
John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 
srting FirstName[10];


Do you mean to make
1) an array of 10strings
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)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

an array to hold 10 strings

stringname.length() does not work

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 
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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You