C++ Vector

Borland Delphi 2009 professional & c++bu...
March 16, 2010 at 09:16:21
Specs: Windows 7
I'm having problems with creating a vector. It is a homework problem and I am just a beginner with programming.

I have to use a Borland C++ GUI to have a user enter data and save it to a file then the user can retrieve the data from the file and have it displayed on the screen.

I'm having problems creating a vector to take the data in from an edit box when the user clicks a button.
Here is the part of my code that I am having problems with along with the different things I have tried. Right now it only shows one of the edit boxes that I am tying to get data from. I figured that if I can figure out how to get data from at least one of the edit boxes I should be able to get the data from the rest of the boxes

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//AnsiString Name = EdtName;

(*empPtr)[0] = EdtName;
//(*_EmpDBPtr)[0] = Name;
//empPtr*.push_back(EdtName);

}


See More: C++ Vector

Report •


#1
March 16, 2010 at 09:17:31
Here is my .cpp file with most of my code so far.

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
Employee::Employee()
{
Form1->MemDisplay->Lines->Append("Employee Default Constructor");
}
Employee::Employee(AnsiString& fname,AnsiString& lname,AnsiString& empid,AnsiString& etype)
{
Form1->MemDisplay->Lines->Append("Employee Parameterized Constructor");
memset(_FirstName,'\0',NSIZE);
strcpy(_FirstName,fname.c_str());
memset(_LastName,'\0',NSIZE);
strcpy(_LastName,fname.c_str());
try
{
_EmpID = empid.ToInt();
_EType = etype.ToInt();
}
catch(...)
{
MessageBox(NULL,"Employee Conversion Error","Conversion Error",0);
}

}

Employee::~Employee()
{
Form1->MemDisplay->Lines->Append("Employee Destructor");
}
int Employee::GetEType()
{
return _EType;
}
FullTime::FullTime()
{
Form1->MemDisplay->Lines->Append("FullTime Default Constructor");
}
FullTime::FullTime(AnsiString& fname,AnsiString& lname,AnsiString& empid,AnsiString& etype,AnsiString& salary):Employee(fname,lname,empid,etype)
{
Form1->MemDisplay->Lines->Append("FullTime Parameterized Constructor");
try
{
_Salary = salary.ToDouble();
}
catch(...)
{

}

}
FullTime::~FullTime()
{
Form1->MemDisplay->Lines->Append("FullTime Destructor");
}
PartTime::PartTime()
{
Form1->MemDisplay->Lines->Append("PartTime Default Constructor");
}
PartTime::PartTime(AnsiString& fname,AnsiString& lname,AnsiString& empid,AnsiString& etype,AnsiString& hours,AnsiString& rate):Employee(fname,lname,empid,etype)
{
Form1->MemDisplay->Lines->Append("PartTime Parameterized Constructor");
try
{
_Hours = hours.ToDouble();
_HRate = rate.ToDouble();
}
catch(...)
{

}
}
PartTime::~PartTime()
{
Form1->MemDisplay->Lines->Append("PartTime Destructor");
}
FileIO::FileIO()
{
Form1->MemDisplay->Lines->Append("FileIO Default Constructor");
_FilePtr = NULL;
openDlgPtr = new TOpenDialog(NULL);

}
FileIO::~FileIO()
{
Form1->MemDisplay->Lines->Append("FileIO Destructor");
delete openDlgPtr;
delete _FilePtr;
}
void FileIO::OpenFile()
{
try
{
openDlgPtr->Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt|Binary Files (*.bin)| *.bin|Serialized File (*.ser)|*.ser";
openDlgPtr->FilterIndex = 1;
openDlgPtr->Execute();
FileName = openDlgPtr->FileName;
Form1->MemDisplay->Lines->Append("Filename="+FileName);
_FilePtr = new fstream(FileName.c_str());
FStatus = _FilePtr->rdstate();
if(FStatus!=0)
{
bool eofs = _FilePtr->eof();
bool fails = _FilePtr->fail();
bool bads = _FilePtr->bad();
throw new Exception("File didn't open");
}
Form1->MemDisplay->Lines->Append("File Open Status="+AnsiString(FStatus));
}
catch(Exception* ioexp)
{
//Form1->MemDisplay->Lines->Append("FileIO Error "+AnsiString(ioexp.Message()));
_FilePtr->clear();
delete ioexp;

}
catch(...)
{
_FilePtr->clear();
}
}
void FileIO::CloseFile()
{

}
void FileIO::WriteFile(vector<Employee*>* empDBPtr)
{
empPtr = (*empDBPtr)[0];
EType = empPtr->GetEType(); //FullTime
switch(EType)
{
case 1:
_FilePtr->write((char*)&EType,sizeof (int));
_FilePtr->write((char*)empPtr,sizeof SFT);
break;
case 2:
_FilePtr->write((char*)&EType,sizeof (int));
_FilePtr->write((char*)empPtr,sizeof SFT);
break;
default:

break;
}
empPtr = NULL;
}
void FileIO::ReadFile()
{
_FilePtr->read((char*)&EType,sizeof (int));
switch(EType)
{
case 1:
empPtr = new FullTime();
_FilePtr->read((char*)empPtr,sizeof (FullTime));
break;
case 2:
empPtr = new PartTime();
break;
default:

break;

}
}
void FileIO::SeekFile()
{

}
void FileIO::FileStatus()
{

}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
_FileIOPtr = new FileIO();
_EmpDBPtr = new vector<Employee*>(3);
//vector<ansiString>* myVectorPtr = new vector<int>(5);
}
__fastcall TForm1::~TForm1()
{
delete _FileIOPtr;
for(int idx=0; idx<_EmpDBPtr->size(); idx++)
delete (*_EmpDBPtr)[idx];
delete _EmpDBPtr;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnOpenFileClick(TObject *Sender)
{
_FileIOPtr->OpenFile();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//AnsiString Name = EdtName;

(*empPtr)[0] = EdtName;
//(*_EmpDBPtr)[0] = Name;
//empPtr*.push_back(EdtName);

}
//---------------------------------------------------------------------------


Here is my Header File

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
#include <fstream>
#include <vector>
using namespace std;
const int NSIZE = 40;
const int SINT = sizeof (int);
class Employee
{
private:
char _FirstName[NSIZE];
char _LastName[NSIZE];
int _EmpID;
int _EType;
public:
Employee();
//Full_Time(AnsiString,AnsiString,AnsiString);
Employee(AnsiString&,AnsiString&,AnsiString&,AnsiString&);
virtual ~Employee();
int GetEType();

};
class FullTime : public Employee
{
private:
double _Salary;
public:
FullTime();
FullTime(AnsiString&,AnsiString&,AnsiString&,AnsiString&,AnsiString&);
~FullTime();

};
const int SFT = sizeof (FullTime);
class PartTime : public Employee
{
private:
double _Hours;
double _HRate;
public:
PartTime();
PartTime(AnsiString&,AnsiString&,AnsiString&,AnsiString&,AnsiString&,AnsiString&);
~PartTime();
};
class FileIO
{
private:
fstream* _FilePtr;
TOpenDialog* openDlgPtr;
AnsiString FileName;
int FStatus;
Employee* empPtr;
//FullTime xxx;
int EType;
public:
FileIO();
~FileIO();
void OpenFile();
void CloseFile();
void WriteFile(vector<Employee*>*);
void ReadFile();
void SeekFile();
void FileStatus();
};//end class FileIO
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *BtnExit;
TMemo *MemDisplay;
TButton *BtnOpenFile;
TButton *Button1;
TEdit *EdtName;
TEdit *EdtTitle;
TEdit *EdtSalaryCommRate;
void __fastcall BtnExitClick(TObject *Sender);
void __fastcall BtnOpenFileClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
FileIO* _FileIOPtr;
vector<Employee*>* _EmpDBPtr;
Employee* _AEmpDB[4];
Employee* empPtr;
Employee** empArray;
unsigned int aIndex;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


Report •

#2
March 16, 2010 at 10:27:29
I'm not seeing where you declare the vector.

Report •

#3
March 16, 2010 at 11:45:24
I may be doing this wrong but I'm trying to send the data to my fileIO code so in the GUI class I declare it as following:

__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{

_EmpDBPtr = new vector<Employee*>(3);

}

Then I try to use it when writing to the fileIO

void FileIO::WriteFile(vector<Employee*>* empDBPtr)
{
empPtr = (*empDBPtr)[0];
EType = empPtr->GetEType(); //FullTime
...
}


Report •

Related Solutions

#4
March 17, 2010 at 08:27:40
A disclaimer and then a few things: I haven't bothered to plug your code into a complier. Partly because you didn't post the entire project, and partly because I use a complier released within the past decade.

1) I strongly recommend you keep the vector on the stack, and define the vector as vector<Employee>. There are a lot of subtle memory leaking bugs you can avoid when you get into error handling. (For the rest of the post, I'll assume you don't intend to.)

2) std::vector<Employee*> is a pain to write. use a typedef to make the pain manageable.

typedef std::vector<Employee*> vEmp;
vEmp* _EmpDBPtr;

3) _EmpDBPtr = new vector<Employee*>(3); should be _EmpDBPtr = new vector<Employee*>;.

4) void FileIO::WriteFile(vector<Employee*>* empDBPtr)
You really should be using references instead of pointers, if just because it gets rid of one level of indirection. void FileIO::WriteFile(vector<Employee*>& empDBPtr)

5) empPtr = (*empDBPtr)[0];
EType = empPtr->GetEType(); //FullTime

Use iterators.

//Assuming you followed the above tips, excluding #1 . . .
for (vEmp:iterator empPtr = empDBPtr.begin(); empPtr != empDBPtr.end(); ++empPtr) {
  EType = *empPtr->GetEType(); //FullTime
 . . . . . . . 
}

6) To add something to the vector, just make a new Employee, and push it on the vector.

Employee* emp = new Employee;
_EmpDBPtr.push_back(emp);


Report •

#5
March 17, 2010 at 14:54:28
Razor2.3 Thank you for the response. I will give that a try this week on my program.

Report •


Ask Question