| View previous topic :: View next topic |
| Author |
Message |
JD Guest
|
Posted: Sun Dec 14, 2003 11:59 am Post subject: Re: How do I get an array from a grid? |
|
|
"joshua" <mc_josh_vx (AT) yahoo (DOT) com> wrote:
| Quote: | [...] convert the data that i have in a string grid from
Grid->Cells[cols][rows] to a two dimensional array say
float Data[rows][cols]
|
There may be a better way. What is it exaclty that you want to
accomplish with this?
| Quote: | Data[dat1][dat2] = StrToFloat (StringGrid1->Cells[Col][Row])
|
That should read:
Data[dat1][dat2] = StrToFloat( StringGrid1->Cells[Col][Row].c_str() );
| Quote: | this doesnt seem to work coz when i try to use this perpotedly
'captured' array values to fill another grid,
|
You do realize that you can directly assign the cells of one
grid to another don't you?
| Quote: | the EConvertError exception!!
|
StrToFloat will throw that exception. You could use TryStrToFloat
if your version of BCB has it or you can also use atof. The nice
thing about atof is that it will convert as much of the string as
possible where as the other methods do not. They convert the
entire string or not at all.
~ JD
|
|
| Back to top |
|
 |
joshua Guest
|
Posted: Sun Dec 14, 2003 12:43 pm Post subject: How do I get an array from a grid? |
|
|
Hi all,
i have a little problem here!
i want to be able to do teh following in the application that am developing:
convert the data that i have in a string grid from Grid->Cells[cols][rows]
to a two dimensional array
say float Data[rows][cols]
i have tried to accomplish this by coding something like:
Data[dat1][dat2] = StrToFloat (StringGrid1->Cells[Col][Row])
where float Data[][10] has already been initialized.
this doesnt seem to work coz when i try to use this perpotedly 'captured'
array values to fill another grid,
i get errors of 'not valid floating point value'; and further still, all the
cells of the new grid get filled up with the same value (seems like i only
manage to capture a single value into the array!)
It seems like any way that i try to use to accomplish this is only earning
me the EConvertError exception!!
could somebody pliz help me with this???
regards
joshua
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Sun Dec 14, 2003 12:53 pm Post subject: Re: How do I get an array from a grid? |
|
|
joshua wrote:
| Quote: | to a two dimensional array
say float Data[rows][cols]
i have tried to accomplish this by coding something like:
Data[dat1][dat2] = StrToFloat (StringGrid1->Cells[Col][Row])
|
What is dat1 ? dat2 ? Better:
Data[Col][Row] = StrToFloat (StringGrid1->Cells[Col][Row])
| Quote: | where float Data[][10] has already been initialized.
|
which element(s) is Data[][10] ?
And why do you do thaty and where and with what ?
| Quote: | this doesnt seem to work coz when i try to use this perpotedly 'captured'
array values to fill another grid,
i get errors of 'not valid floating point value';
|
Please show the used code.
| Quote: | and further still, all the
cells of the new grid get filled up with the same value (seems like i only
manage to capture a single value into the array!)
It seems like any way that i try to use to accomplish this is only earning
me the EConvertError exception!!
|
Where did you get that error ? Please show the staement.
Hans.
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Sun Dec 14, 2003 12:54 pm Post subject: Re: How do I get an array from a grid? |
|
|
"joshua" <mc_josh_vx (AT) yahoo (DOT) com> wrote:
| Quote: | i want this code to enable me rto capture the data values
input into the string grid into a 2d array of type say
'float'.
|
I personally don't use float's. I use double's instead because
it provides for greater precision and all of the 'float'
functions work with double's.
| Quote: | [...] and compute the results of all the cols into a single
col.
|
If this is all that you're using it for, you can skip the
array of floats and simply iterate through the cells:
int Col = some column
double Total = 0;
for( int Row = StringGrid1->FixedRows; Row < StringGrid1->RowCount; ++Row )
{
Total += StrToFloat( StringGrid1->Cells[ Col ][ Row ].c_str() );
}
If you're displaying the results in a collection of TEdits,
you could try something like this:
/* In the form's constructor, create a 'link' for each Edit
total field with each StringGrid column
*/
__fastcall TFor::TFor(TComponent* Owner)
: TForm(Owner)
{
StringGrid1->Objects[ some column ][ 0 ] = reinterpret_cast<TObject*>( some edit name );
StringGrid1->Objects[ another column ][ 0 ] = reinterpret_cast<TObject*>( another edit name );
}
void __fastcall TForm1::CalculateColumnTotal( int Col )
{
TStringGrid* pGrid = StringGrid1;
double Total = 0;
for( int Row = pGrid->FixedRows; Row < pGrid->RowCount; ++Row )
{
Total += StrToFloat( pGrid->Cells[ Col ][ Row ].c_str() );
}
TEdit* pEdit = reinterpret_cast<TEdit*>( pGrid->Objects[ Col ][ 0 ] );
pEdit->Text = FloatToStr( Total );
}
~ JD
|
|
| Back to top |
|
 |
joshua Guest
|
Posted: Sun Dec 14, 2003 1:26 pm Post subject: Re: How do I get an array from a grid? |
|
|
i want this code to enable me rto capture the data values input into the
string grid into a 2d array of type say 'float'.
i thought that this could be an easy way to get data of a size say like
50x10 - that is 50 rows by 10 cols.
i am trying to develop anm application that will take int this amount of
data and compute the results of all the cols into a single col. so i thought
that converting this into an array could be better.
am a bit nw in builder, so pliz dont mind if i sound a bit dumb.
thanx en rerards
josh
|
|
| Back to top |
|
 |
joshua Guest
|
Posted: Sun Dec 14, 2003 1:48 pm Post subject: Re: How do I get an array from a grid? |
|
|
hi,
i had the intentioon of converting the data entered in a string grid
(numeric variables) into an array of type float(2dimension array)
so i declared an array of type float[100][10] and tried to assign the values
on the grid cells[ARow][ACol] itno this array.
basically, the user should define the size of grid(rows and cols) depending
on the amount of data he/she has to enter. the data should be then converted
into a 2d array - i thought this would make it easier to analyze the data.
am in a cyber, but i will get my code and mail it later.
am a bit new in prog. and i hope u dont mind if i sound dull!!!
cheers
joshua
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sun Dec 14, 2003 10:21 pm Post subject: Re: How do I get an array from a grid? |
|
|
"JD" <nospam (AT) nospam (DOT) com> wrote
| Quote: | That should read:
Data[dat1][dat2] = StrToFloat( StringGrid1->Cells[Col][Row].c_str() );
|
No, it shouldn't. StrToFloat() expects an AnsiString. The Cells[][]
property returns an AnsiString. It will work fine just as joshua had it
originally.
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sun Dec 14, 2003 10:22 pm Post subject: Re: How do I get an array from a grid? |
|
|
"joshua" <mc_josh_vx (AT) yahoo (DOT) com> wrote
| Quote: | this doesnt seem to work coz when i try to use this
perpotedly 'captured' array values to fill another grid,
i get errors of 'not valid floating point value';
|
Please show your actual code.
Gambit
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Mon Dec 15, 2003 11:32 am Post subject: Re: How do I get an array from a grid? |
|
|
"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote:
| Quote: | [...] No, it shouldn't. StrToFloat() expects an AnsiString.
|
Aaahhh!! I should have caught that. Truth is I hate StrToFloat
because in almost all cases, I want what ever is there - even
if it's formatted incorrectly - so I use atof (mostly) to get
the 'good' stuff which I then put back to the control AND ...
atof does need the c_str() :-&
~ JD
|
|
| Back to top |
|
 |
josuha Guest
|
Posted: Sun Dec 21, 2003 12:31 pm Post subject: Re: How do I get an array from a grid? |
|
|
"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote
| Quote: |
"JD" <nospam (AT) nospam (DOT) com> wrote in message
news:3fdc5eb0$1 (AT) newsgroups (DOT) borland.com...
That should read:
Data[dat1][dat2] =
StrToFloat( StringGrid1->Cells[Col][Row].c_str() );
No, it shouldn't. StrToFloat() expects an AnsiString. The Cells[][]
property returns an AnsiString. It will work fine just as joshua had it
originally.
Gambit
|
sure thing, that conversion works out well if you use atof.
but i wonder, does atof have its counterpart or it just uses FloatToStr??
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Sun Dec 21, 2003 3:28 pm Post subject: Re: How do I get an array from a grid? |
|
|
"josuha" <mc_josh_vx (AT) yahoo (DOT) com> wrote:
| Quote: | sure thing, that conversion works out well if you use atof.
but i wonder, does atof have its counterpart
|
If you mean converting a float back to a string, there are a
couple but don't use them. They're cumbersome and once you
know you have a float, you need not worry about exceptions
being thrown so use FloatToStr.
~ JD
|
|
| Back to top |
|
 |
|