Solved

Error using ListBox item in C++Builder

Posted on 2000-04-13
12
484 Views
Last Modified: 2012-05-04
Well, I need to create a ListBox item that lists names recieved out of a file.  So far, I think my coding is right, all runs fine until I try to use this certain area of my program.  The program will abort and give me this error:

Project Trodholm.exe raised exception class EStringListError with message 'List index out of bounds (0)'. Process stopped. Use Step or Run to continue.

here is my source code for it:

void charstuff::ListChars(void) {
   CodeIt(PlayFile);
   PlayerFile = fopen(PlayFile, "r+");
   CharCount = -1;
   while (!feof(PlayerFile)) {

      fscanf(PlayerFile,yourname.c_str());

   fscanf(PlayerFile,yourpassword.c_str());
   CharCount++;
   LoadCharForm->CharListBox->Items->Strings[CharCount]
      = yourname.c_str();
   }
   fclose(PlayerFile);
   CodeIt(PlayFile);
}
0
Comment
Question by:nfcdrummer
  • 5
  • 4
  • 3
12 Comments
 
LVL 6

Expert Comment

by:graham_k
Comment Utility
well, you don't say which line is giving problems & withoout the full project, we can only code read. If any sugegstions here don't help, perhpas you can zip up the project & put it somewhere that we can d/l it (if its not too private).

here are some thoughts:

- I never used fscanf() before, but if it's like scanf() and scscanf() then the first parameter is a source e.g file handle, the next is a format string an then come some parameters. So, if you're trying to read in a string from the file, you might want to use

  char name[255], password[255];
  fscanf(PlayerFile, "%s %s",  name, password);

note that you can't read _into_ AnsiString::c_str() it _outputs_ the char * value of an AnsiString - it's read only.

Also, TListBox->Items->Strings[] is an array (a vector, actually) of type AnsiString, so
LoadCharForm->CharListBox->Items->Strings[CharCount] = yourname.c_str();  in your example above should be LoadCharForm->CharListBox->Items->Strings[CharCount] = yourname;   // note, no .c_str()    if yoruname were of type AnsiString.  If it is a char[] or char* (samne thing), use = AnsiString (yourname);


Let me know if that helps, or if any of it is not clear.
0
 
LVL 6

Expert Comment

by:graham_k
Comment Utility
hmm, I've just re-read the question and, while I stand by all of what I wrote, it doesn't explain the problem with charCount.

Can you stop the debugger on that line & examine CharCount and the strings. You ought to find CharCount == 0. What about LoadCharForm->CharListBox->Items->Strings.Count ?  What size is the string array? Normally, you shouldn't have to allocate any memory for it, but if, for instance, you had dynamically created your listbox at run time, you might have too.

What I'm saying here, is that I suspect that you are trying to write element zer0 of an array (vector) that doesn't even have memory allocated for a zer0th element.  I dimly remember having the same, probelm, but can't remember the answer. Can you post the code anywhere?
0
 

Author Comment

by:nfcdrummer
Comment Utility
What I have read from the error, the out of bounds index (0) is saying I can't write into Strings[0].  I tried it at [1] as well, and it doesn't access it.  Thats my problem, its when it gets to the

LoadCharForm->CharListBox->Items->Strings[CharCount] = yourname;

line.

by the way, I noticed the problem with the c_str();, and fixed that before I checked this.

So what I'm saying is, when it has read the name out of the file, it can't assign it to Strings[0].  Thats basically it and I cannot seem to fix it.
0
 

Author Comment

by:nfcdrummer
Comment Utility
What I have read from the error, the out of bounds index (0) is saying I can't write into Strings[0].  I tried it at [1] as well, and it doesn't access it.  Thats my problem, its when it gets to the

LoadCharForm->CharListBox->Items->Strings[CharCount] = yourname;

line.

by the way, I noticed the problem with the c_str();, and fixed that before I checked this.

So what I'm saying is, when it has read the name out of the file, it can't assign it to Strings[0].  Thats basically it and I cannot seem to fix it.

Unfortunately, I do not wish to post my source code as it is private, and I don't have the time to upload it I'm sorry.
0
 
LVL 6

Expert Comment

by:graham_k
Comment Utility
don't worry about not posting private code. That's quite o.k.  With a C++ Builder project it would be all or nothing anyway, if we were to debug it.

Now, what the error is telling you is that LoadCharForm->CharListBox->Items->Strings has no members. No one index by [1], and not even one indexed by [0]. There is simply no memory allocated for these strings.

Now, we have to ask why. if you create your listbox at design time by selecting the standard listbox component from the toolbar & dropping it onto your form, then C++ Builder _should_ automatically create the memory for you.

If not, you mnust allocate it yourself with something like LoadCharForm->CharListBox->Items->Strings = new TStringList  In which case, don't forget to delete the stringlist at the appropriate time.

I am in the office now (Japan) but will play about with some TListBOx stuff this weekend & look at some of my own old code.

In the meantime, try as I said, examine LoadCharForm->CharListBox->Items->Strings, especially LoadCharForm->CharListBox->Items->Strings->Count just before it crashes.


STOP PRESS - am I not thinking stright?  Instead of assigning LoadCharForm->CharListBox->Items->Strings[0] = sometext;

why not use LoadCharForm->CharListBox->Items->Strings.Add(sometext);  ?


That ought to allocate the memory. I'm *sure* that that is it. Sorry, but I'm trying to concentrate on something else @ work. That should have obvious to me from the start.

Note that you may want to Clear() the strings before starting the loop, so that when you call the fn() again, you don;t end up adding a second copy of each string.
0
 
LVL 1

Expert Comment

by:jbodom
Comment Utility
Here's your problem--graham_k is right:
You've not allocated memory for items in the listbox.  Instead of

LoadCharForm->CharListBox->Items-Strings[CharCount]= yourname.c_str();

Try this:
LoadCharForm->CharListBox->Items->Add(yourname.c_str());

This should work fine.
   
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Accepted Solution

by:
graham_k earned 100 total points
Comment Utility
well, thanks for backing me up (and proposing my comment as your answer)


btw not

LoadCharForm->CharListBox->Items->Add(yourname.c_str());

but rather

LoadCharForm->CharListBox->Items->Add(yourname);
0
 

Author Comment

by:nfcdrummer
Comment Utility
because you stole graham_k's answer

thanks for your help graham_k
0
 

Author Comment

by:nfcdrummer
Comment Utility
you are very welcome
0
 
LVL 6

Expert Comment

by:graham_k
Comment Utility
well, thanks for the points. More important is getting the problem solved. Sorry that it took me a few hours for the answer to occur. But at least you got to see how my thinking (if you can call that) works & we tidied up a few other things with fscanf() and c_str().
0
 
LVL 1

Expert Comment

by:jbodom
Comment Utility
Well, maybe I should've made it a comment but as far as I saw, graham k didn't post the code to make it work.  I don't see how you think that I stole his answer.  As a matter of fact he even stated "I dimly remember having the same, probelm, but can't remember the answer".  Not a big deal, I was just trying to help and I'm pretty sure, you're using the code that I showed you.
0
 
LVL 1

Expert Comment

by:jbodom
Comment Utility
Either of these work fine:

LoadCharForm->CharListBox->Items->Add(yourname.c_str());

LoadCharForm->CharListBox->Items->Add(yourname);

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now