string array size

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

string array size

 
0
  #1
Apr 7th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string array size

 
0
  #2
Apr 8th, 2007
strlen should work since you say you're using "string arrays"
Last edited by John A; Apr 8th, 2007 at 12:06 am.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array size

 
0
  #3
Apr 8th, 2007
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.

Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string array size

 
0
  #4
Apr 8th, 2007
How is FirstName declared?

Does it look something like this:
  1. std::string FirstName;

Or is it like this:
  1. char FirstName[] = "Blah blah";

The latter will require what I edited into my post later (that is, strlen).
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array size

 
0
  #5
Apr 8th, 2007
srting FirstName[10];
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 15
Reputation: Daniel E is an unknown quantity at this point 
Solved Threads: 2
Daniel E Daniel E is offline Offline
Newbie Poster

Re: string array size

 
0
  #6
Apr 8th, 2007
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:
  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]
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array size

 
0
  #7
Apr 8th, 2007
Microsoft Visual Studion .net 2003
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string array size

 
0
  #8
Apr 8th, 2007
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:
  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. }
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: string array size

 
0
  #9
Apr 8th, 2007
Originally Posted by mrjoli021 View Post
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)
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array size

 
0
  #10
Apr 8th, 2007
an array to hold 10 strings

stringname.length() does not work
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC