Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Iam using visual studio 2008 and i can't seem to rectify this error..it seems simple but i really can't seem to rectify this.
What iam trying to do is pass the values of an initialized string array to an uninitialized string array using a loop..here's my code:-
C++
Document ^doc = gcnew Document("Abdul.doc");
String ^yes = doc->GetText();

array<Char>^chars = {' '};
array<String^>^splitx = yes->Split(chars);
array<String^>^ projects;
int jamrod = 0;

for (int ok = 0; ok < splitx->Length; ok++)
{
    if(splitx[ok] == "Copyright") //splitx[250]
    {
        while(splitx[ok] != "3D")
        {
            projects[jamrod] = splitx[ok];
            jamrod++;
            ok++;
        }
        break;
    }
    else
    {
        MessageBox::Show("suck on my blue suede shoe");
    }
}

I'll be very grateful if anyone can solve this.
Thanks. :)
Posted
Updated 4-May-11 1:14am
v3
Comments
Niklas L 4-May-11 7:19am    
What makes you think you can index an uninitialized variable? (projects in while-loop)

array<string^>^ projects;
int jamrod = 0;
 
for (int ok = 0; ok < splitx->Length; ok++)
{
    if(splitx[ok] == "Copyright") //splitx[250]
    {
        while(splitx[ok] != "3D")
        {
            projects[jamrod] = splitx[ok];
You don't initialize "projects" to any value, therefore projects[jamrod] does not exist, for any value of "jamrod".
 
Share this answer
 
Comments
Shahvez Irfan 4-May-11 11:13am    
i know that. my question is how do i fix it?
First problem: you didn't instanciate the projects array. And it looks like you don't need an array here but a list (since you don't know the total number of elements added into projects).

You should replace:
C++
array<String^>^ projects;

by:
C++
List<String^>^ projects = gcnew List<String^>();


And in you while loop, replace:
C++
projects[jamrod] = splitx[ok];

by:
C++
projects->Add(splitx[ok]);


Second problem: what is the yes string supposed to contain? It is like you expect it to contain at least "3D", right? Otherwise your while loop would never exit... Unless you give more details about yes, I can't help further.

-----------------------------------

If I understood well, you want to split a string into sub-strings (separated by ' ') and collect every sub-strings between "Copyright" and "3D". If I am right, then use this code:

C++
Document ^doc = gcnew Document("Abdul.doc");
String ^yes = doc->GetText();
array<Char>^ chars = gcnew array<Char>(1) { ' ' };
array<string^>^ splitx = yes->Split(chars);
List<string^>^ projects = gcnew List<string^>();
//this will be turned to true once we find "Copyright"
bool foundStart = false;
for (int i = 0; i < splitx->Length; i++)
{
    if (splitx[i] == "3D")
    {
        //we found the end of the list
        //so exit the loop
        break;
    }
    if (foundStart)
    {
        //"Copyright" was found before
        //so let's add this string to the list
        projects->Add(splitx[i]);
    }
    else
    {
        //"Copyright" was not found yet
        //is it this one?
        foundStart = (splitx[i] == "Copyright");
    }
}
//now projects contains every strings between "Copyright" and "3D"


Otherwise, please explain one more time what you want to achieve.
 
Share this answer
 
v3
Comments
Shahvez Irfan 4-May-11 10:08am    
It contains text read in String from a Word file...i've used the built in Split function to tokenize the word file read using " " as the delimeter..then i traverse the splitx array using the loop..when i get "Copyright" in the splitx array, i move ahead and put all the contents of the splitx array which follow, into the PROJECTS array..i know it requires initialization, but i can't seem to find a solution that works..i tried using your method, but for some reason it doesn't work..i haven't used Lists in c++, so i can't really tell if i know the basics of how to use them.
Thank you :)
Shahvez Irfan 4-May-11 15:26pm    
I tried your method, but it gives some errors, of which i think the most relevant one is = 'List' : undeclared identifier

Is there any method in which i could initialize the string array and use that method, sir? I actually have to use string arrays or something of that sort so i can use them in certain String matching algorithms.

and another thing! can you recommend any method from which i can convert a String array to char*..i need to use it in a Linked List, but apparently i can't access a String array in a Linked List.

Thanks.
Olivier Levrey 5-May-11 4:07am    
The List<T> class is part of the System::Collection::Generic namespace. Make sure you have using namespace System::Collections::Generic; at the top of your cpp file.

And replace char by Char (capital letter). I wrote it by mistake (you should use System::Char struct and not the native char).

For your last question: why do you want to use char*? Can't you use String?
Shahvez Irfan 5-May-11 9:55am    
Thanks! i got it..i need to use char* because apparently a String^ from the form cannot be accessed in a linked list..iam trying to use the Marshal function for conversion from String to char*..you think that's alright?
Olivier Levrey 5-May-11 10:39am    
Oh oh I didn't understand you wanted to use mixed code! What is the type of your linked list? If it is not a .NET class, then yes you should change your String object into a char* with the Marshal::StringToHGlobalAnsi function. And don't forget to free the memory with FreeHGlobal.
Simply change projects to type vector<string></string>.
vector<string> projects;</string>

...
projects.push_back(splitx[ok]);
...


Good luck!
 
Share this answer
 
Comments
Olivier Levrey 4-May-11 7:56am    
OP is using .NET, not native C++...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900