oo sorry... i should have included the "PBEntry.h" in the post
thank you for your replies... any criticism is appreciated.
Here is my new coded .cpp file after thinking about it. Not done yet cause I still gotta add code that counts how many entries are stored in the "Phonebook.txt", if any, and then I still have to make a menu function for the program.
PBEntry.cpp
Code:
#include <fstream.h>
#include "PBEntry.h"
const string UniversalFileName = "PhoneBook.txt";
void CreateEntry(int *EntryNum,string *EntryName,string *EntryHPhone,string *EntryCPhone,string *EntryAddress)
{
PBEntry Entry = new PBEntry(&EntryNum,&EntryName,&EntryHPhone,&EntryCPhone,&EntryAddress);
}
void ReadFileContents(ifstream *iFile)
{
int i=0;
PBEntry Entries[];
string fEntryNum;
string fEntryName;
string fEntryHPhone;
string fEntryCPhone;
string fEntryAddress;
while(getline(&iFile, fEntryAddress) != NULL)
{
cout<<"\nCurrently on Entry "<<i+1<<"."<<endl;
getline(&iFile, fEntryNum, '\t');
getline(&iFile, fEntryName, '\t');
getline(&iFile, fEntryHPhone, '\t');
getline(&iFile, fEntryCPhone, '\t');
getline(&iFile, fEntryAddress);
Entries[i++]=CreateEntry(fEntryNum,fEntryName,fEntryHPhone,
fEntryCPhone,fEntryAddress);
}
}
void WriteEntries(PBEntry *Entry, ofstream *oFile)
{
oFile<<Entry.getNum()<<"\t"<<endl;
oFile<<Entry.getName()<<"\t"<<endl;
oFile<<Entry.getHPhone()<<"\t"<<endl;
oFile<<Entry.getCPhone()<<"\t"<<endl;
oFile<<Entry.getAddress()<<endl<<endl;
}
void CreateOutFile()
{
ofstream nFile(UniversalFileName); // Im creating a file if there is not one currently available
if(!nFile.is_open())
{
cout<<"File could not be created! Press Enter to exit the program...."<<endl;
cint.get();
return 0;
}
nFile.close(); // File Created....
}
void HandleFile(ifstream *iFile)
{
if(!iFile)
{
cout<<"ERROR: Data File \"PhoneBook.txt\" could not be opened or found!"<<endl;
cout<<"Create a new file named Phonebook.txt?(Y or N): ";
char answer;
cin>>answer;
cout<<"\n";
if(answer=='y' || answer=='Y')
{
CreateOutFile();
}
else if(answer=='n' || answer=='N')
{
cout<<"\nThis program can go no further without a file. Press Enter to end the program..."<<endl;
cin.get();
return 0;
}
}
ReadFileContents(&iFile);
}
int main()
{
ifstream *iDataFile;
ofstream *oDataFile;
iDataFile.open(UniversalFileName);
HandleFile(&iDataFile);
}
PBEntry.h
Code:
#include <iostream>
using namespace std;
class PBEntry
{
public:
PBEntry(int eNum, string eName, string eHPhone,
string eCPhone, string eAddress)
{
EntryNum = eNum;
EntryName = eName;
EntryPhone[0] = eHPhone;
EntryPhone[1] = eCPhone;
EntryAddress = eAddress;
};
~PBEntry() {};
int getNum() {return EntryNum;}
string getName() {return EntryName;}
string getHPhone() {return EntryPhone[0];}
string getCPhone() {return EntryPhone[1];}
string getAddress() {return EntryAddress;}
protected:
int EntryNum;
string EntryName;
string EntryPhone[2];
string EntryAddress;
};
Quote from Daved:
You are going to need some sort of data structure to hold each entry. This will take a little bit of thought. I'd recommend making a struct out of all your strings and then using a vector to hold instances of that struct.
Would this be easier since I have no derived classes from the original class structure "PBEntry"?
And jafet, the link you provided is broken, and I was learning from the book "C++ Programming in Easy Steps: Conforms to ANSI and ISO Standards". If this reference book is not up to date, I was thinking about getting a C++ Primer book. I am not familiar with the editions though..which one would meet my requirements for being a new programmer?...Havnt really checked in on it.
And Salem, the book I was learning from uses while(!file.eof()). Is this not good practice? I think I understand the link reference. Use the return value of getline to see if the end of the file has been reached?
Ex.
Code:
while(getline(&iFile, fEntryAddress) != NULL)
{
cout<<"\nCurrently on Entry "<<i+1<<"."<<endl;
getline(&iFile, fEntryNum, '\t');
getline(&iFile, fEntryName, '\t');
getline(&iFile, fEntryHPhone, '\t');
getline(&iFile, fEntryCPhone, '\t');
getline(&iFile, fEntryAddress);
Entries[i++]=CreateEntry(fEntryNum,fEntryName,fEntryHPhone,
fEntryCPhone,fEntryAddress);
}