This what I have done so far... (to cscgal and inscissor) As you can probably tell I have't done C++ for long; only a few weeks now so please point out any mistakes that I have made. Here is some evidence to prove that I have tried, even though it isn't perfect, I am pleased with it because it is one of my first proper programs... Due to the fact that I have demonstrated that I have done some of the code (And by the way, I didn't pay anyone to write it for me from rentacoder.com even though if I need to have something coded that is beyond my cababilities, I will use that website so thanks for the link inscissor!)
I have had a few problems with this application ... For instance, I know that I have to include the 'string.h' library file but whenever I try and compile the code, I get an error saying that the file/directory can not be found. Any help with this?
I also need to add three other classes - an application class which can be used as the entry-point into the application: CApplication, Two derived classes to split the information about the 'individual' into details about themselves like their name and event dates for example; and another class to gather information about their business, if they have one like, for example, their company name.
The code that I have put 'debug' by obviously means that it needs debugging when something happens during the program that shouldn't. I am mentioning this because that code is ok, even though it needs correcting - its to show that flaws of this application i.e what it can and can't do.
As you might have realised... this code won't actually work - don't worry! its not supposed to! I just have to demonstrate that I can use classes and be able to handle data. Unfortuanately, because of this, I don't know if the seperate parts of it work so if someone can tell me if they think it looks ok, I will be grateful!
#include "string.h"
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include "utility.h"
////////////////////////////////////////////////////////////////////////////
Abook::Abook()
:MaxRecSize(200)
{
table.num=1;
table.name=15; // (refer to number 1)
table.address=23;
table.phone=14;
table.email=19;
}
//////////////////////////////////////////////////////////////////////////////
bool Abook::SetAbook(String filenameArg, bool create)
{
filename=filenameArg+".pbk";
cout<<"filename: "<<filename<<endl;//debug
if(file.is_open())
file.close();
if(create==true)
file.open(filename.c_str(), ios::in|ios::out);
else
{
file.open(filename.c_str(), ios::in|ios::out|ios::nocreate);
if(file.fail())
return(false);
}
numEntries=NumRecords(); //(Number 4)
cout<<"numentrinit"<<numEntries<<endl;//debug
return(true);
}
//////////////////////////////////////////////////////////////////////////////
Entry Abook::InputEntry()
{
cout<<"Enter name: ";
cin.ignore(1, '\n');
getline(cin, entry.name);
cout<<"Enter adress: ";
getline(cin, entry.adress);
cout<<"Enter phone number: ";
getline(cin, entry.phone);
cout<<"Enter email adress: ";
getline(cin, entry.email);
return(entry);
}
//////////////////////////////////////////////////////////////////////////////
void Abook::SaveEntry(Entry p, int recNum)
{
cout<<"numentrb4save"<<numEntries<<endl;//debug
if(recNum==-1) //-1 is the default param
recNum=(numEntries);
cout<<"Byte saved at: "<<(recNum*MaxRecSize)<<endl;
file.seekp(recNum*MaxRecSize);
cout<<"Tell p b4: "<<file.tellp()<<endl;//debug
file<<p.name<<endl;
file<<p.adress<<endl;
file<<p.phone<<endl;
file<<p.email<<endl;
cout<<"Tell p after: "<<file.tellp()<<endl;//debug
numEntries+=1;
cout<<"numentr after save"<<numEntries<<endl;
}
//////////////////////////////////////////////////////////////////////////////
void Abook::AddEntry()
{
SaveEntry(InputEntry());
}
//////////////////////////////////////////////////////////////////////////////
int Abook::FindEntry(String name)
{
String tempName; // int j;
for(int i=0; i<numEntries; i++)
{
if(GetEntry(i).name==name)
return(i);
else
{
if((i%(numEntries/1))==0)
cout<<".";
}
}
cout<<"Name not found, try again.\n";
return(-1);
}
//////////////////////////////////////////////////////////////////////////////
Entry Abook::GetEntry(int recNum)
{
file.seekg(recNum*MaxRecSize);
getline(file, entry.name);
getline(file, entry.adress);
getline(file, entry.phone);
getline(file, entry.email);
return(entry);
}
//////////////////////////////////////////////////////////////////////////////
void Abook::EditEntry(int recNum, Entry e)
{
cout<<"NumEntr b4 edit: "<<numEntries<<endl;
SaveEntry(e, recNum);//recNum*MaxRecSize
numEntries--;
cout<<"NumEntr after edit: "<<numEntries<<endl;
}
//////////////////////////////////////////////////////////////////////////////
int Abook::GetLastEntryNum()
{
return(numEntries);
}
//////////////////////////////////////////////////////////////////////////////
void Abook::DisplayEntry(int recNum)
{
Entry entry=GetEntry(recNum);
//MakeHeader("Entry # ", 2); // (Number 5)
/*cout<<"Entry # "<<recNum<<endl;
cout<<"==========\n";
cout<<endl<<endl;
SglUnderline("Name");
cout<<endl<<entry.name<<endl<<endl;
SglUnderline("Address", 1);
cout<<endl<<'\t'<<entry.adress<<endl<<endl;
SglUnderline("Phone number", 2);
cout<<endl<<"\t\t"<<entry.phone<<endl<<endl;
SglUnderline("Email", 3);
cout<<endl<<"\t\t\t"<<entry.email<<endl<<endl;*/
cout.setf(ios::left);
int nameWid=30;
int phoneWid=30;
cout<<"\t\tEntry # "<<recNum<<endl;
cout<<"\t ===========\n\n";
cout.width(nameWid);
cout<<"Name";
cout<<"Address\n";
cout.width(nameWid);
cout<<"====";
cout<<"=======\n";
cout.width(nameWid);
cout<<entry.name;
cout<<entry.adress<<endl;
cout<<endl<<endl;
cout.width(phoneWid);
cout<<"Phone";
cout<<"Email\n";
cout.width(phoneWid);
cout<<"=====";
cout<<"=====\n";
cout.width(phoneWid);
cout<<entry.phone;
cout<<entry.email<<endl;
}
//////////////////////////////////////////////////////////////////////////////
void Abook::DisplayAll()
{
Entry entry;
char major=char(205), minor=char(196), vert=char(179), connector=char(197); //table's characters
char farLeft=char(195), farRight=char(180), topLeft=char(218), topRight=char(191);
char botLeft=char(192), botRight=char(217), top=char(194), bot=char(193);
char majorLeft=char(198), majorRight=char(181);
//advanced chars
char majorConnector=char(216);
//char major='=', minor='-', vert='|', connector='+'; //table's characters
int numWid=1, nameWid=15, addressWid=23, phoneWid=14, emailWid=19; //table widths
int j=0;
cout.setf(ios::left);
//Overliner to header
cout<<topLeft;
for(j=0; j<table.num; j++)
cout<<minor;
cout<<top;
for(j=0; j<table.name; j++)
cout<<minor;
cout<<top;
for(j=0; j<table.address; j++)
cout<<minor;
cout<<top;
for(j=0; j<table.phone; j++)
cout<<minor;
cout<<top;
for(j=0; j<table.email; j++)
cout<<minor;
cout<<topRight;
cout<<endl;
//header to table
cout<<vert;
cout.width(table.num); cout<<"#";
cout<<vert;
cout.width(table.name); cout<<"Name";
cout<<vert;
cout.width(table.address); cout<<"Adress";
cout<<vert;
cout.width(table.phone); cout<<"Phone";
cout<<vert;
cout.width(table.email); cout<<"E-mail";
cout<<vert<<endl;
//underliner to header
cout<<majorLeft;
for(j=0; j<table.num; j++)
cout<<major;
cout<<majorConnector;
for(j=0; j<table.name; j++)
cout<<major;
cout<<majorConnector;
for(j=0; j<table.address; j++)
cout<<major;
cout<<majorConnector;
for(j=0; j<table.phone; j++)
cout<<major;
cout<<majorConnector;
for(j=0; j<table.email; j++)
cout<<major;
cout<<majorRight;
cout<<endl;
//Display entries
for(int i=0; i<numEntries; i++)
{
entry=GetEntry(i);
entry=TruncateEntry(entry); //Makes the entry fit into the table widths
cout<<vert;
cout.width(table.num); cout<<i;
cout<<vert;
cout.width(table.name); cout<<entry.name;
cout<<vert;
cout.width(table.address); cout<<entry.adress;
cout<<vert;
cout.width(table.phone); cout<<entry.phone;
cout<<vert;
cout.width(table.email); cout<<entry.email;
cout<<vert<<endl;
//underliner
if(i==numEntries-1)
{
connector=bot;
farLeft=botLeft;
farRight=botRight;
}
cout<<farLeft;
for(j=0; j<table.num; j++)
cout<<minor;
cout<<connector;
for(j=0; j<table.name; j++)
cout<<minor;
cout<<connector;
for(j=0; j<table.address; j++)
cout<<minor;
cout<<connector;
for(j=0; j<table.phone; j++)
cout<<minor;
cout<<connector;
for(j=0; j<table.email; j++)
cout<<minor;
cout<<farRight;
cout<<endl;
}
cout.unsetf(ios::left);
}
//////////////////////////////////////////////////////////////////////////////
void Abook::DeleteEntry(int recNum)
{
int l=0;
int curSpot=0; //current save spot
String tempName=filename+".tmp";
ofstream newf(tempName.c_str());
for(l=0; l<numEntries; l++)
{
if(l!=recNum)
{
newf.seekp(curSpot*MaxRecSize);
newf<<GetEntry(l).name<<endl;
newf<<GetEntry(l).adress<<endl;
newf<<GetEntry(l).phone<<endl;
newf<<GetEntry(l).email<<endl;
curSpot++;
}
}
newf.close();
file.close();
remove(filename.c_str());
rename(tempName.c_str(), filename.c_str());
SetAbook(filename.substr(0, filename.length()-4), false);
}
//////////////////////////////////////////////////////////////////////////////
long Abook::NumRecords()
{
file.seekg(0, ios::end);
cout<<"tellg: "<<file.tellg()<<endl;//debug
if(file.tellg()==0)
return(0);
else
{//th4 debug
cout<<"Result tellg: "<<(1+file.tellg()/MaxRecSize)<<endl;//debug
return(1+file.tellg()/MaxRecSize);
}//th4 debug
}
//////////////////////////////////////////////////////////////////////////////
Entry Abook::TruncateEntry(Entry e)
{
if(e.name.length()>table.name) //if entries name is greater than the name-column
//width for the table
e.name=e.name.substr(0, (table.name-2))+".."; //cut down to 2 minus available
//width, add a ".." to end
if(e.adress.length()>table.address) //same for rest
e.adress=e.adress.substr(0, (table.address-2))+"..";
if(e.phone.length()>table.phone)
e.phone=e.phone.substr(0, (table.phone-2))+"..";
if(e.email.length()>table.email)
e.email=e.email.substr(0, (table.email-2))+"..";
return(e);//debug
}