- Code: Select all
#include "fileutil.cpp"
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
#include<ctype.h>
#include<fstream.h>
#if !defined(microsoft)
#include<stdlib.h>
#define cls() system("cls");
#endif
class Voter
{
private:
int idnumber;
char lastname[25];
char firstname[25];
char party[2];
int birthyear;
int currentage;
public:
Voter();
Voter(int,char[], char[], char[], int, int);
Voter(const Voter&);
void FileIn(Voter voterlist[],ifstream&, int);
void SaveDisk(Voter voterlist[], ofstream& outfile, int arraysize);
void print();
~Voter();
};
//*****************************************************************************
// Default Constructor *
//*****************************************************************************
Voter::Voter()
{
idnumber = 0;
lastname[0] = '\0';
firstname[0] = '\0';
party[2] = '\0';
birthyear = 0;
currentage = 0;
}
//*****************************************************************************
// Constructor Arguments *
//*****************************************************************************
Voter::Voter(int id,char ln[], char fn[], char pid[], int year, int by)
{
idnumber = id;
strcpy(lastname, ln);
strcpy(firstname, fn);
strcpy(party, pid);
birthyear = year;
currentage = 2003 - birthyear;
}
//*****************************************************************************
// Copy Constructor *
//*****************************************************************************
Voter::Voter(const Voter& v1)
{
idnumber = v1.idnumber;
strcpy(lastname, v1.lastname);
strcpy(firstname, v1.firstname);
strcpy(party, v1.party);
birthyear = v1.birthyear;
currentage = 2003 - v1.birthyear;
}
//*****************************************************************************
// ReadInData *
//*****************************************************************************
void Voter::FileIn(Voter voterlist[], ifstream& infile, int arraysize)
{
int age,
idnumber,
birthyear,
currentage = 0,
numVoters = 0;
char lastname[25],
firstname[25],
party[2],
trash[2];
OpenInput( infile, "Give name of Data File: " );
while (infile.good() )
{
for ( int x = 0; x < arraysize; x++)
{
infile >> idnumber;
infile.getline(trash,2);
infile.getline(lastname,25,'\t');
infile.getline(firstname, 25, '\t');
infile.getline(party, 2, '\t');
infile >> birthyear;
age = 2003 - birthyear;
voterlist[numVoters] = Voter(idnumber,lastname, firstname, party, birthyear, currentage);
++ numVoters;
}
if (infile.fail() )
break;
}
infile.close();
}
//*****************************************************************************
// Destructor *
//*****************************************************************************
Voter::~Voter()
{
}
void PrintList(Voter voterlist[], int);
void main()
{
const int arraysize = 8;
Voter voterlist[arraysize];
ifstream infile;
ofstream outfile;
voterlist[arraysize].FileIn(voterlist,infile, arraysize);
PrintList(voterlist, arraysize);
voterlist[arraysize].SaveDisk(voterlist, outfile, arraysize);
}
void PrintList(Voter voterlist[], int arraysize)
{
for (int n = 0;n<(arraysize) ;n++)
{
voterlist[n].print();
}
}
