Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 20th, 2006, 12:54 PM   #1
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
String Array

How do i return the amount of elements within a string array!

script[i].Length() (shoudl work? haven't tried)

script.Length() (doesn't work)

any ideas?
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old May 20th, 2006, 1:13 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
I don't understand why people post the question before they even try ..

Yes, script[i].Length(); works.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old May 20th, 2006, 1:37 PM   #3
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
sizeof(StringArray)/sizeof(StringArray[0]);

Although I would suggest using std::vector instead of an array:
std::vector<std::string> StringVector;
size_t size = StringVector.size();
Cache is offline   Reply With Quote
Old May 20th, 2006, 1:43 PM   #4
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
maybe because thats no my question jayme... i know that it works!

i need to find out how many elemets are in the array no how many char's are in an element.

Thanks Cache!

New question

void Engine()
{
	// Error Initilizations
	i_curError = 0;
	s_errorArray[25] = 0x0;
}

i get undeclared identifier error for both lines

and why can't i return a damn string array!!! god damn't C# has corrupted my farkin mind!

/*std::string[] Engine::ReturnErrors()
{
	if(s_errorArray[0] != "")
		return s_errorArray;
	else
	{
		s_errorArray[0] = "none";
		return s_errorArray;
	}
}*/
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old May 20th, 2006, 1:47 PM   #5
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,194
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by jayme
I don't understand why people post the question before they even try ..

Yes, script.Length(); works.
Incorrect. Remember, C++ is case-sensitive. You can use either script.length() or script.size(), whichever you prefer.

[edit] Oh, you meant elements of the array, not elements of the string in the array. In that case, you can use sizeof(script)/sizeof(string), assuming that you allocated the space statically. If you are using dynamic allocation, you'll have to track it yourself, so if that is your intent, a vector is better, as Cache pointed out. [/edit]
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old May 20th, 2006, 4:10 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by Kilo
i get undeclared identifier error for both lines
Probably because you didn't declare either of the variables you're assigning. Unless they're at the global scope, but you probably don't want that. As it is, your compiler doesn't know what type they are.

Quote:
Originally Posted by Kilo
and why can't i return a damn string array!!! god damn't C# has corrupted my farkin mind!
Again, declare your variables.

I would also recommend using a std::vector for your collection of strings, it's a lot easier to handle than dynamic arrays.
Jimbo is offline   Reply With Quote
Old May 20th, 2006, 4:21 PM   #7
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
im familiar with vectors.. again im just attemping to port my code from C# to c++ as close as i can (hence the string array)

:banana:

Oh and i didn't declare all of my variables... thats insulting? i forgot to post the class declaration. I do program for a living you can give me a little more credit than that lol.

code is at work so i will write exactly what i have at work:

class Engine
{

public:

     Engine();
     ~Engine();

      int i_curError;
      std::string s_errorArray[25];

private:

      //code at work
}


void Engine()
{
	// Error Initilizations
	i_curError = 0;
	s_errorArray[25] = 0x0;
}

std::string[] Engine::ReturnErrors()
{
	if(s_errorArray[0] != "")
		return s_errorArray;
	else
	{
		s_errorArray[0] = "none";
		return s_errorArray;
	}
}

this bombs out for "undeclared identifier"... they are obviously declared...somehow the Constructor isn't being link to the class declaration.

EDITlectricpharaon: would this work ->
[php]
int len = sizeof(script)/sizeof(script[0]);
[/php]
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old May 20th, 2006, 5:05 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by Kilo
would this work ->
[php]
int len = sizeof(script)/sizeof(script[0]);
[/php]
As a std::string has a variable length, therefore it's unlikely to. What's wrong with the length method?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 20th, 2006, 6:21 PM   #9
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by Kilo
this bombs out for "undeclared identifier"... they are obviously declared...somehow the Constructor isn't being link to the class declaration.
If I'm reading the code correctly, you have your constructor defined as:
void Engine()
{
	// Error Initilizations
	i_curError = 0;
	s_errorArray[25] = 0x0;
}
This just declares a global function called Engine. The constructor should be
Engine::Engine()
{
   // stuff here
}
Because of the difference, you did have definition problems. I was wrong on the 2nd one though :o
Jimbo is offline   Reply With Quote
Old May 20th, 2006, 6:22 PM   #10
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Kilo
I do program for a living you can give me a little more credit than that lol.
We can't guess what you know or don't know, oh and please don't curse, C# is not the problem, PEBKAC.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC