Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error using ListBox item in C++Builder

Posted on 2000-04-13
12
Medium Priority
?
546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 6

Expert Comment

by:graham_k
ID: 2714188
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
ID: 2714206
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
ID: 2714407
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:nfcdrummer
ID: 2714408
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
ID: 2714529
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
ID: 2714923
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
 
LVL 6

Accepted Solution

by:
graham_k earned 400 total points
ID: 2714955
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
ID: 2715008
because you stole graham_k's answer

thanks for your help graham_k
0
 

Author Comment

by:nfcdrummer
ID: 2715017
you are very welcome
0
 
LVL 6

Expert Comment

by:graham_k
ID: 2715053
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
ID: 2715723
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
ID: 2716093
Either of these work fine:

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

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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

618 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