I have read the tutorials on array, but this is an area which it doesn't seem to cover, unless I have just overlooked. It explains about passing arrays to functions but not returning them from what I can see.
I need to create a string array with 0 elements and increment it as it fills up.
my pseudocode to this would be create a method that takes an array as param and returns an array of the new length, I know this can be done in java easy enough but in c++ it seems like this may not be possible the way I would hope. So any guidance on this would be most appreciated. Does this code look ok?
Sorry, I forgot to add, the reason I am asking this without testing the code is I am currently not at home and studying on a computer(without compiliers) simply doing pseudocode on paper whilst reading... As I cannot find a syntactical solution to my pseudocode I am asking here. As I will not be close to being able to test it for a day or two, so at least If I recieve a response here I can change my pseudocode accordingly, or just change the syntax when the time comes to coding.
Unfortunately I have to use an array it is for assignment, and we have not covered vectors.
In what way are they not the same? I know that the string class is a little different in c++ because essentially it is an array of *char where as in java string is simply string. What is different with arrays?
EDIT: specific assignment criteria:
*read strings from file,
*store in array of non-duplicate strings.(max 100 strings)
*sort string array, and output to file.
*c++ string class only, not c string.
going over the assignment criteria it does not state the array must be initialized to 0, and incremented as it fills up, however he stated this in class, which means that I may be able to write it without the addElement function(initialize to 100, and use checks for !null in writing to file) and simply argue the fact when he tells me to resubmit :P
It doesn't make sense. Whereas an array is an object in Java, it's not in C++, it's just a contiguous block of elements. It doesn't know it's size or other such thing.
The assignment seems sound, you need to think about C++ arrays, not Java arrays.
I could go further, but I'd like you to think about it a bit.
Well, first off you need to pass the array *and* the array length. Arrays don't have a length member. And, in the code above, you declare the method returning a string, yet you return an array of strings.
Second, in order to meet the second criterion (non-duplicated) you are going to have to keep the strings sorted and search whether the string already exists.
My recommendations:
Create a class containing the string array.
Have a member function to add one string to the array. This member will be responsible for searching and, if the string does not exist, adding the element, then ensuring the array is sorted.
It doesn't make sense. Whereas an array is an object in Java, it's not in C++, it's just a contiguous block of elements. It doesn't know it's size or other such thing.
ahhh, of course. thanks.
Have a member function to add one string to the array. This member will be responsible for searching and, if the string does not exist, adding the element, then ensuring the array is sorted.
OOPL basics :)
and I'm starting to long for the days when we move into templates :P
So in this scenario, of incrementing array size I need to have a set counter, and when I call my addElement() simply call incCounter() which will increment a counter, and return the counter which will give me my length... Then instead of returning anything, create a new array of the new size, use a for loop to place the elements into the new array, and then using pointers point my old array location to the new array location. Is that right?