0

here is my code
main

#include <iostream>
#include "account.h"

using namespace std;
int main()
{
	account a;
	
	return 0;
}

account.h

#include <iostream>
#include "Lentext.h"
#include "Deltext.h"

#include <string>
using namespace std;
class account
{
private: 
	char account_Number[11];
	char name[1000];
	char address[1000];
	char city[16];
	char state[3];
	char zip_Code[10];
	char account_Balance[1000];
public:

	//default account constructor
	account();

	//set each field to a empty string
	void Clear();

	//sets the private variables above
	void set_account_number(string account_Number_Value);
	void set_title(string first_Last);
	void set_address(string location);
	void set_city(string county);
	void set_state(string loc);
	void set_zip_code(string zip);
	void set_account_balance(string user_balance);

	//methods for Lentext
	int Pack(LengthTextBuffer&) const;
	int UnPack(LengthTextBuffer&);

	//methods for Deltext
	int Pack(DelimTextBuffer&) const;
	int UnPack(DelimTextBuffer&);

	//print data
	void Print(ostream &);
};

account.ccp

#include "Account.h"
#include <iostream>
#include <iomanip>
using namespace std;

void account::set_account_number(string account_Number_Value)
{
	char* a=new char[account_Number_Value.size()+1];
	a[account_Number_Value.size()]=0;
	memcpy(account_Number, account_Number_Value.c_str(),account_Number_Value.size());
	//account_Number=account_Number_Value;
}

void account::set_title(string first_Last)
{
	char* a=new char[first_Last.size()+1];
	a[first_Last.size()]=0;
	memcpy(name, first_Last.c_str(),first_Last.size());
	//name=first_Last;
}

void account::set_address(string location)
{
	char* a=new char[location.size()+1];
	a[location.size()]=0;
	memcpy(address, location.c_str(),location.size());
	//address=location;
}

void account::set_city(string county)
{
	char* a=new char[county.size()+1];
	a[county.size()]=0;
	memcpy(city, county.c_str(),county.size());
	//city=county;
}

void account::set_state(string loc)
{
	char* a=new char[loc.size()+1];
	a[loc.size()]=0;
	memcpy(state, loc.c_str(),loc.size());
	//state=loc;
}

void account::set_zip_code(string zip)
{
	char* a=new char[zip.size()+1];
	a[zip.size()]=0;
	memcpy(zip_Code, zip.c_str(),zip.size());
	//zip_Code=zip;
}

void account::set_account_balance(string user_balance)
{
	char* a=new char[user_balance.size()+1];
	a[user_balance.size()]=0;
	memcpy(account_Balance, user_balance.c_str(),user_balance.size());
	//account_Balance=user_balance;
}

account::account()
{
	Clear();
}

void account::Clear()
{
	account_Number[0]=0;
	name[0]=0;
	address[0]=0;
	city[0]=0;
	state[0]=0;
	zip_Code[10]=0;
	account_Balance[1000]=0;
}

//pack the fields into a FixedTextBuffer
//return true if all succeed
int account::Pack(LengthTextBuffer & Buffer) const
{
	int result;
	Buffer.Clear();
	result=Buffer.Pack(account_Number);
	result=result&&Buffer.Pack(name);
	result=result&&Buffer.Pack(address);
	result=result&&Buffer.Pack(city);
	result=result&&Buffer.Pack(state);
	result=result&&Buffer.Pack(zip_Code);
	result=result&&Buffer.Pack(account_Balance);
	return result;
}

int account::UnPack(LengthTextBuffer & Buffer)
{
	int result;
	Buffer.Clear();
	result=Buffer.Pack(account_Number);
	result=result&&Buffer.Pack(name);
	result=result&&Buffer.Pack(address);
	result=result&&Buffer.Pack(city);
	result=result&&Buffer.Pack(state);
	result=result&&Buffer.Pack(zip_Code);
	result=result&&Buffer.Pack(account_Balance);
	return result;
}

Lentext.h

#include <iostream>

using namespace std;

//a buffer which holds length based text fields
class LengthTextBuffer
{
public:
	//construct with a maximum of maxFields
	LengthTextBuffer(int maxBytes=1000);

	//clear fields from buffer
	void Clear();
	
	int Read(istream &);

	int Write(ostream &) const;

	//set the value of the next field of the buffer
	int Pack(const char *, short size=-1);

	//extract the value of the next field of the buffer
	int Unpack(char *);

	void Print(ostream &) const;

	void Init(int maxBytes=1000);
	
private:
	//character array to hold field values
	char * Buffer;

	//size of packed fields
	int BufferSize;

	//maximum number of characters in the buffer
	int MaxBytes;

	//packing/unpacking position in buffer
	int NextByte;

};

Lentext.cpp

#include "Lentext.h"
#include <string.h>

//constructor with a maximum of maxFields
LengthTextBuffer::LengthTextBuffer(int maxBytes)
{
	Init(maxBytes);
}

//clear fields from buffer
void LengthTextBuffer::Clear()
{
	BufferSize=0;
	MaxBytes=0;
	NextByte=0;
}

int LengthTextBuffer::Read(istream & stream)
{
	Clear();
	stream.read((char*)&BufferSize, sizeof(BufferSize));
	if(stream.fail())
	{
		return false;
	}
	//buffer overflow
	if(BufferSize>MaxBytes)
	{
		return false;
	}
	stream.read(Buffer,BufferSize);
	return stream.good();
}

int LengthTextBuffer::Write(ostream & stream) const
{
	stream.write((char *)&BufferSize, sizeof(BufferSize));
	stream.write(Buffer, BufferSize);
	return stream.good();
}

//set the value of the next field of the buffer
//if size=-1(default) use strlen(str) as length of field
int LengthTextBuffer::Pack(const char * str, short size)
{
	//length of string to be packed
	size_t len;
	if(size>=0)
	{
		len=size;
	}
	else
	{
		len=strlen(str);
	}
	//str is too short
	if(len>strlen(str))
	{
		return false;
	}
	//first character to be packed
	int start= NextByte;
	NextByte += (len+sizeof(len));
	if(NextByte>MaxBytes)
	{
		return false;
	}
	memcpy(&Buffer[start],&len,sizeof(len));
	strncpy(&Buffer[start+sizeof(len)],str, len);
	BufferSize=NextByte;
	return true;
}

//extract the value of the next field of buffer
int LengthTextBuffer::Unpack(char * str)
{
	//length of packed string
	short len;
	//no more fields
	if(NextByte>=BufferSize)
	{
		return false;
	}
	//first character to be unpacked;
	int start=NextByte;
	memcpy(&len, &Buffer[start], sizeof(short));
	NextByte += len +sizeof(short);
	if(NextByte > BufferSize)
	{
		return false;
	}
	strncpy(str, &Buffer[start+sizeof(short)],len);
	//zero termination for string
	str[len]=0;
	return true;
}

void LengthTextBuffer::Print(ostream & stream) const
{
	stream<< "Buffer has characters"<<MaxBytes
		<<" and Buffer Size " <<BufferSize<<endl;
}

//construct with a maximum of maxFields
void LengthTextBuffer::Init(int maxBytes)
{
	if(maxBytes<0)
	{
		maxBytes=0;
	}
	MaxBytes=maxBytes;
	Buffer=new char[MaxBytes];
	Clear();
}

Deltext.h

#include <iostream>

using namespace std;

//a buffer which holds delimited text fields
class DelimTextBuffer
{
public:
	//constructor with fields with delimeters
	DelimTextBuffer(char Delim='|', int maxBytes=1000);
	//clear fields from buffer
	void Clear();
	int Read(istream &);
	int Write(ostream &) const;
	int Pack(const char *,int size=-1);
	int Unpack(char *);
	void Print(ostream &) const;
	void Init(char delim, int maxBytes=1000);
private:
	char Delim;
	//zero terminated string for Delim
	char DelimStr[2];
	//char array to hold field values
	char * Buffer;
	//size of packed fields
	int BufferSize;
	//maximum number of characters in the buffer
	int MaxBytes;
	//packing/unpacking position in buffer
	int NextByte;
};

Deltext.cpp

#include "Deltext.h"
#include <string.h>



//construct with a maximum number of maxFields
DelimTextBuffer::DelimTextBuffer(char Delim, int maxBytes)
{
	
	Init(Delim,maxBytes);
}

//clear fields from buffer
void DelimTextBuffer::Clear()
{
	BufferSize=0;
	MaxBytes=0;
	NextByte=0;
}

int DelimTextBuffer::Read(istream & stream)
{
	Clear();
	stream.read((char*)&BufferSize, sizeof(BufferSize));
	if(stream.fail())
	{
		return false;
	}
	//buffer overflow
	if(BufferSize>MaxBytes)
	{
		return false;
	}
	stream.read(Buffer,BufferSize);
	return stream.good();
}

int DelimTextBuffer::Write(ostream & stream) const
{
	stream.write((char *)&BufferSize, sizeof(BufferSize));
	stream.write(Buffer, BufferSize);
	return stream.good();
}

//set the value of the next field of the buffer
//if size=-1(default) use strlen(str) as Delim of field
int DelimTextBuffer::Pack(const char * str, int size )
{
	//length of string to be packed
	size_t len;
	if(size>=0)
	{
		len=size;
	}
	else
	{
		len=strlen(str);
	}
	//str is too short
	if(len>strlen(str))
	{
		return false;
	}
	//first character to be packed
	int start=NextByte;
	NextByte +=len + 1;
	if(NextByte>MaxBytes)
	{
		return false;
	}
	memcpy(&Buffer[start],str, len);
	//add delimeter
	Buffer[start+len]=Delim;
	BufferSize=NextByte;
	return true;
}

//extract the value of next field of the buffer
int DelimTextBuffer::Unpack(char * str)
{
	//length of packed string
	int len=-1;
	//first character to be unpacked
	int start=NextByte;
	for(int i=start; i<BufferSize; i++)
	{
		if(Buffer[i]==Delim)
		{
			len=i-start;
			break;
		}
	}
	//delimeter not found
	if(len==-1)
	{
		return false;
	}
	NextByte +=len + 1;
	if(NextByte>BufferSize)
	{
		return false;
	}
	strncpy(str, &Buffer[start], len);
	//zero termination for string
	str[len]=0;
	return true;
}

void DelimTextBuffer::Print(ostream & stream) const
{
	stream<< "Buffer has max characters" <<MaxBytes
		<<" and Buffer Size "<<BufferSize<<endl;
}

void DelimTextBuffer::Init(char delim, int maxBytes)
{
	Delim=delim;
	DelimStr[0]= Delim;
	DelimStr[1]=0;
	if(maxBytes <0)
	{
		maxBytes=0;
	}
	MaxBytes=maxBytes;
	Buffer= new char[MaxBytes];
	BufferSize=0;
}

Thanks

1
Contributor
1
Reply
3
Views
7 Years
Discussion Span
Last Post by lotrsimp12345
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.