Hey Everyone,
I am trying to compile and run a program for my C++ class, and I am getting a Segmentation Fault as a runtime error. I looked at my code and my datafile to make sure that there was not an issue but I can not seem to find whats giving me the error. Below I have placed my code and the datafile I am using during runtime. I have to use the terminal in Ubuntu to compile/link/run, and this is the way my professor wants me to write the code.
Thanks in advance for any help!
Source Code:
//Assignment 7 DNJ--> CSCI 208
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
//****************************************************************************************
/* Global Variables */
const int NAMESIZE=25;
const int FILENAME=51;
const int MAXLISTSIZE=200;
typedef char Name_t[NAMESIZE];
typedef char Filename_t[FILENAME];
typedef int ID_t;
typedef double Bal_t;
typedef fstream Infile_t;
typedef fstream Outfile_t;
typedef int Addr_t;
const Addr_t NIL=-1;
const Addr_t END=-2;
//****************************************************************************************
/* Class Declerations */
class cAccount
{
private:
Name_t fname;
Name_t lname;
ID_t aID;
Bal_t aBal;
public:
cAccount();
void Read (Infile_t &Infile);
void Print (Outfile_t &Outfile);
void Key_Input();
void write2data(Infile_t &Infile);
void Display();
void write2list(Infile_t &Infile);
int compID2ID(cAccount NewAccount);
int Search4ID();
};// end cAccount
//****************************************************************************************
/* Struct Declerations */
struct ListEle_t
{
cAccount Account;
Addr_t Link;
};// end ListEle_t
//****************************************************************************************
/* Array Decleration */
typedef ListEle_t List_t[MAXLISTSIZE];
//****************************************************************************************
/* Class Function Implementations */
cAccount::cAccount()
{
strcpy(fname,"");
strcpy(lname,"");
aID=0;
aBal=0;
}// end cAccount::cAccount
void cAccount::Read(Infile_t &Infile)
{
Infile>>aID>>lname>>fname>>aBal;
}// end cAccount::Read
void cAccount::Print(Outfile_t &Outfile)
{
Outfile<<setw(10)<<"ACCOUNT-ID: "<<aID<<endl;
Outfile<<setw(10)<<"ACCOUNT NAME: "<<lname<<","<<fname<<endl;
Outfile<<setw(10)<<"ACCOUNT BALANCE: $"<<aBal<<endl;
Outfile<<"------------------------------------------------"<<endl;
}// end cAccount::Print
void cAccount::Key_Input()
{
cout<<"Begining KeyBoard Input.........."<<endl<<endl;
cout<<"Enter 5 digit Account-ID #:"<<endl;
cin>>aID;
cout<<"Enter the first name:"<<endl;
cin>>fname;
cout<<"Enter the last name:"<<endl;
cin>>lname;
cout<<"Enter the Account Balance:"<<endl;
cin>>aBal;
cout<<endl<<endl;
}// end cAccount::Key_Input();
void cAccount::write2data(Infile_t &Infile)
{
Key_Input();
Infile<<fname<<lname<<aID<<aBal<<endl;
}// end cAccount::write2data
void cAccount::write2list(Infile_t &Infile)
{
Infile<<fname<<lname<<aID<<aBal<<endl;
}// end cAccount::write2list
void cAccount::Display()
{
cout<<setw(10)<<"ACCOUNT-ID: "<<aID<<endl;
cout<<setw(10)<<"ACCOUNT NAME: "<<lname<<","<<fname<<endl;
cout<<setw(10)<<"ACCOUNT BALANCE: $"<<aBal<<endl;
cout<<"------------------------------------------------"<<endl;
}// end cAccount::Display
int cAccount::compID2ID(cAccount NewAccount)
{
int count;
count=0;
if(this->aID > NewAccount.aID)
{
count=-1;
}// end if
else if(this->aID < NewAccount.aID)
{
count=1;
}// end else if
else
{
count=0;
}//end else
return count;
}// end cAccount::compID2ID
int cAccount::Search4ID()
{
int i=0;
if(aID==this->aID)
{ i=1; }
else
{ i=0; }
return i;
}// end cAccount::Search4ID
//****************************************************************************************
/* Function Prototypes */
void Insert( List_t &List, Addr_t &Start, Addr_t Pred, Addr_t &Avail,cAccount NewAccount);
bool Search(List_t List, cAccount Account, Addr_t Start, Addr_t &Pred);
void Search4Pred(Addr_t &Pred,List_t List,Addr_t Start, cAccount Account);
void Add(Addr_t &Avail, List_t &List, Addr_t &Start, cAccount NewAccount);
void Load(List_t &List,Addr_t &Start,Addr_t &Avail);
void Display(List_t List, Addr_t Start, Addr_t Avail);
void Print(List_t List,Addr_t Start,Addr_t Avail);
void SaveList(List_t List, Addr_t Start, Addr_t Avail);
void Save(cAccount TempAccount, Infile_t &Infile);
//****************************************************************************************
/* MAIN */
int main()
{
int COUNT=0;
Addr_t Start;
Start=NIL;
List_t List;
Addr_t Avail;
cAccount Account;
Infile_t Infile;
Load(List,Start,Avail);
Display(List,Start,Avail);
Print(List,Start,Avail);
SaveList(List,Start,Avail);
return 0;
}// end main
//****************************************************************************************
/* Function Implementations */
void Insert( List_t &List, Addr_t &Start, Addr_t Pred, Addr_t &Avail,cAccount NewAccount)
{
Addr_t Curr;
Curr=Avail;
Avail=Avail+1;
List[Curr].Account=NewAccount;
if(Pred!=NIL)
{
List[Curr].Link=List[Pred].Link;
List[Pred].Link=Curr;
}
else
{
List[Curr].Link = Start;
Start = Curr;
}
};// end Insert
bool Search(List_t List, cAccount Account, Addr_t Start, Addr_t &Pred)
{
Addr_t Curr;
Pred=NIL;
bool Found;
bool Done;
Done=false;
Curr=Start;
while((Done!=true)&&(Curr!=NIL))
{
if(List[Curr].Account.compID2ID(Account)>0)
{
Pred=Curr;
Curr=List[Curr].Link;
}
else if(List[Curr].Account.compID2ID(Account)<0)
{
Done=true;
Found=false;
}
else
{
Done=true;
Found=true;
Account=List[Curr].Account;
}
}//end while
return Found;
};//end Search
void Search4Pred(Addr_t &Pred,List_t List,Addr_t Start, cAccount Account)
{
bool Done=false;
Addr_t Curr;
Curr=Start;
Pred=NIL;
while((Done!=true)&&(Curr!=NIL))
{
if(List[Curr].Account.compID2ID(Account)>0)
{
Pred=Curr;
Curr=List[Pred].Link;
}
else
{
Done=true;
}
}//end while
}// end Search4Pred
void Add(Addr_t &Avail, List_t &List, Addr_t &Start, cAccount NewAccount)
{
Addr_t Pred;
Search4Pred(Pred,List,Start,NewAccount);
Insert(List,Start,Pred,Avail,NewAccount);
}// end Add
void Load(List_t &List,Addr_t &Start,Addr_t &Avail)
{
Filename_t Filename;
Infile_t Infile;
Addr_t Curr;
Addr_t Pred;
cout<<"Begining Load.........."<<endl<<endl;
cout<<"Enter the name of the data file to be used:"<<endl;
cin>>Filename;
Infile.open(Filename,ios::in);
cAccount NewAccount;
NewAccount.Read(Infile);
while(!Infile.eof())
{
Add(Avail,List,Start,NewAccount);
NewAccount.Read(Infile);
}//end while
Infile.close();
cout<<"Read Complete........."<<endl;
};//end load
void Display(List_t List, Addr_t Start, Addr_t Avail)
{
Addr_t Curr;
Curr=Start;
Name_t choice;
cout<<"Would you like to view the output file?"<<endl;
cout<<"(YES or NO)"<<endl;
cin>>choice;
if(choice=="YES"||choice=="yes"||choice=="Yes")
{
cout<<setw(20)<<"ACCOUNT-DATA"<<endl<<endl;
while(Curr!=NIL)
{
List[Curr].Account.Display();
Curr=List[Curr].Link;
}//end while
}//end if
else
{
cout<<"Very Well.........."<<endl<<endl;
}
};// end Display
void Print(List_t List,Addr_t Start,Addr_t Avail)
{
Addr_t Curr;
Curr=Start;
Filename_t Filename;
Outfile_t Outfile;
cout<<"Enter the name of the output file:"<<endl;
cin>>Filename;
Outfile.open(Filename,ios::out);
Outfile<<setw(20)<<"ACCOUNT-DATA"<<endl<<endl;
while(Curr!=NIL)
{
List[Curr].Account.Print(Outfile);
Curr=List[Curr].Link;
}//end while
Outfile.close();
cout<<"Write Sucessful.........."<<endl<<endl;
};// end Print
void SaveList(List_t List, Addr_t Start, Addr_t Avail)
{
Addr_t Curr;
Curr=Start;
Filename_t Filename;
Infile_t Infile;
cout<<"Enter the name of the Datafile that you wish to create:"<<endl;
cin>>Filename;
Infile.open(Filename,ios::out);
while(Curr!=NIL)
{
List[Curr].Account.write2list(Infile);
Curr=List[Curr].Link;
}//en while
Infile.close();
}// end SaveList
void Save(cAccount TempAccount, Infile_t &Infile)
{
Filename_t Filename;
cout<<"Enter the name of the data file you whish to create/n";
cout<<"(or QUIT to use an existing data file"<<endl;
cin>>Filename;
if (strcmp(Filename,"QUIT")!=0)
{
Infile.open(Filename,ios::out);
Name_t choice;
cout<<"Do you wish to continue (YES or NO): "<<endl;
cin>>choice;
while(strcmp(choice,"NO")!=0)
{
TempAccount.write2data(Infile);
cout<<"Do you wish to continue (YES or NO):"<<endl;
cin>>choice;
}//end while
}//end if
Infile.close();
}//end Save
//****************************************************************************************
Datafile:
31951
Walker
John
1000.98
69537
Patton
George
876.07
28542
Colt
Samuel
1234.56
28325
Bond
James
6798.00
68128
Hamilton
Alex
9000.00
44206
Lincoln
Abe
00.01
13108
Armstrong
Louis
750.00
41727
Gotti
John
5500.60
21158
Lennon
John
6969.69
52019
McCarthy
Joseph
742.02
37823
Stewart
Jon
29.98
70109
Schrute
Dwight
00.00
74746
Bob
Billy
50.50
18211
Pitt
Brad
2056.89
19724
Blitzer
Wolf
3009.10
49926
Dillinger
John
862.43