944,158 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 18282
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 7th, 2007
0

string array size

Expand Post »
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.
Similar Threads
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Apr 8th, 2007
0

Re: string array size

strlen should work since you say you're using "string arrays"
Last edited by John A; Apr 8th, 2007 at 12:06 am.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 8th, 2007
0

Re: string array size

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.

Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Apr 8th, 2007
0

Re: string array size

How is FirstName declared?

Does it look something like this:
C++ Syntax (Toggle Plain Text)
  1. std::string FirstName;

Or is it like this:
C++ Syntax (Toggle Plain Text)
  1. char FirstName[] = "Blah blah";

The latter will require what I edited into my post later (that is, strlen).
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 8th, 2007
0

Re: string array size

srting FirstName[10];
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Apr 8th, 2007
0

Re: string array size

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:
C++ Syntax (Toggle Plain Text)
  1. #include<vector>
  2. #include<string>
  3.  
  4. //Then whatever goes in between
  5. //Declaration
  6. vector<string>FirstName(10);
  7. //For your loop
  8. for(i=0;i<FirstName.size();++i)
  9. infile>>Firstname[i]
Reputation Points: 12
Solved Threads: 2
Newbie Poster
Daniel E is offline Offline
15 posts
since Apr 2007
Apr 8th, 2007
0

Re: string array size

Microsoft Visual Studion .net 2003
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Apr 8th, 2007
0

Re: string array size

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:
C++ Syntax (Toggle Plain Text)
  1. const int ARRAY_SIZE = 10;
  2. string FirstName[ARRAY_SIZE];
  3.  
  4. // ...
  5.  
  6. for (int i=0; i < ARRAY_SIZE; i++) {
  7. // ...
  8. }
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 8th, 2007
0

Re: string array size

Click to Expand / Collapse  Quote originally posted by mrjoli021 ...
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)
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Apr 8th, 2007
0

Re: string array size

an array to hold 10 strings

stringname.length() does not work
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Setting Cursor position
Next Thread in C++ Forum Timeline: compile error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC