User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,187 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,852 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3380 | Replies: 11 | Solved
Reply
Join Date: Mar 2007
Posts: 101
Reputation: mrjoli021 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Help string array size

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,503
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: string array size

  #2  
Apr 7th, 2007
strlen should work since you say you're using "string arrays"
Last edited by John A : Apr 7th, 2007 at 11:06 pm.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Mar 2007
Posts: 101
Reputation: mrjoli021 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array size

  #3  
Apr 7th, 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  
Join Date: Apr 2006
Location: Canada
Posts: 4,503
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: string array size

  #4  
Apr 7th, 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).
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Mar 2007
Posts: 101
Reputation: mrjoli021 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array size

  #5  
Apr 7th, 2007
srting FirstName[10];
Reply With Quote  
Join Date: Apr 2007
Location: Texas
Posts: 15
Reputation: Daniel E is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Daniel E Daniel E is offline Offline
Newbie Poster

Re: string array size

  #6  
Apr 7th, 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  
Join Date: Mar 2007
Posts: 101
Reputation: mrjoli021 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array size

  #7  
Apr 8th, 2007
Microsoft Visual Studion .net 2003
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,503
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: string array size

  #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. }
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: May 2006
Posts: 2,723
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: string array size

  #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)
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Mar 2007
Posts: 101
Reputation: mrjoli021 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: string array size

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

stringname.length() does not work
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC