A Digital Age Deserves A Digital Leader

CodeGear 2009 help c++ [SOLVED]

CodeGear 2009 help c++ [SOLVED]

Postby airwolf » Sat Feb 19, 2011 10:11 am

im using codegear 2009 rad c++ builder and i cand think how to dynamically allocate memory to an array i tried using this code:

Code: Select all
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "masyvai.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int *a;  //dynamic array
int n;   //variable for the dynamic array
n =StrToInt(Edit1->Text);
a = new int[n];
for(int i=1;i<n;i++)
{
   a[i] = StrToInt(Edit2->Text[i]);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2MouseEnter(TObject *Sender)
{
Edit2->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1MouseEnter(TObject *Sender)
{
Edit1->Text = "";
}
//--


No luck everything runs but doesnt allocate memory coretly any ideas? :eek:
Last edited by airwolf on Sun Apr 10, 2011 7:57 pm, edited 1 time in total.
Intel Duo Core 2.66 Ghz
2.0Gb of RAM
640 GB HDD
GeForce 8600GT [The GTS burned ;( ]
Proud Intel User
PRO Level 7
User avatar
Posts: 299
Joined: Tue Dec 26, 2006 10:35 pm
Location: Lithuania siauliai

Re: CodeGear 2009 help c++

Postby jbullard » Sat Feb 19, 2011 11:15 am

Have you tried using malloc?
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Re: CodeGear 2009 help c++

Postby airwolf » Tue Feb 22, 2011 10:17 pm

no i haven't tried to use it cold you write an example how to use it lets say user enters text in a memo or textbox doesnt mather how to create a dynamic array from the user input

user enters in textbox 1 2 3 4 5 6

and i need the program to create me an array[]={1,2,3,4,5,6} in the code :whistle
Intel Duo Core 2.66 Ghz
2.0Gb of RAM
640 GB HDD
GeForce 8600GT [The GTS burned ;( ]
Proud Intel User
PRO Level 7
User avatar
Posts: 299
Joined: Tue Dec 26, 2006 10:35 pm
Location: Lithuania siauliai

Re: CodeGear 2009 help c++

Postby jbullard » Wed Feb 23, 2011 6:40 am

Alright, I took a look at your code and the reason it may not be allocating memory is that StrToInt will not return the length of the string, rather an integer representation of the string. Therefore, if the user typed 100 in the textbox, the StrToInt function would return 100 and not 3.

In order to get the length of the string you would want to use strlen which would return a length of 3. However, I don't see where you are doing any processing of the string. For instance, if a user typed '1 2 3 4 5' in the textbox, where are you splitting that string on the whitespace character?

Code: Select all
#include <stdio.h>
#include <string.h>

void main()
{
   // Size needed for array
   int i = 0;

   // Pointer to int
   int* a = NULL;

   // Used to store split string value
   char * pch;

   // This portion of the code will split the string by tokens.  You can
   // add as many tokens as you want.
   pch = strtok(Edit2->Text, ' ');

   // Loop until NULL
   while (pch != NULL)
   {
      // Convert value to int
      a[i++] = StrToInt(pch);

      pch = strtok(NULL, ' ');
   }

   // Free memory when done
   delete [] a;

   // Deallocate memory
   a = NULL;

   // return
   return;
}


I have not tested this at all so there may be some parts that are incorrect and/or not working.
VP - Software
User avatar
Posts: 3653
Joined: Sun Jun 06, 2004 10:17 pm
Location: Utah
Real Name: Jason Bullard

Re: CodeGear 2009 help c++

Postby airwolf » Sun Apr 10, 2011 7:55 pm

well basically i had a bug somewhere i used this to create a dynamic array :

int *array;
int size;
size = StrToInt(Edit1->Text);
array = new int[size];
//that created the array of my specified size and i can put the data in it i need :() but thanks for the help ^*^
Intel Duo Core 2.66 Ghz
2.0Gb of RAM
640 GB HDD
GeForce 8600GT [The GTS burned ;( ]
Proud Intel User
PRO Level 7
User avatar
Posts: 299
Joined: Tue Dec 26, 2006 10:35 pm
Location: Lithuania siauliai

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 8 guests

cron
cron