#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <climits>
#include <vector>
using namespace std;
struct Person
{
string firstName ;
string lastName;
string phoneNumber;
};
struct Address
{
string streetNum_Name;
string city;
string state;
int zip_code = 70; //always initialize built in type in block/local scope
};
struct Info
{
Person person;
Address address;
};
void printFileHeader(ofstream& outFile);
bool openInputFile(ifstream& inFile);
void openOutputFile(ofstream& outFile);
void readInfo(ifstream& inFile, std::vector<Info>&);
void printInfo(ofstream& outFile, std::vector<Info>&);
int main()
{
ifstream inFile;
ofstream outFile;
bool fileStreamState;
std::vector<Info> infoVec; //this vector will contain all the information
//Info person; //no need for this anymore since we have the vector
//printFileHeader(outFile);//call this after openOutputFile()
fileStreamState = openInputFile(inFile);
openOutputFile(outFile);
printFileHeader(outFile);
cout << "Processing information. Please Wait...." << endl << endl;
readInfo(inFile, infoVec) ; //call readInfo once instead of calling again and again
printInfo(outFile, infoVec);//call printInfo once instead of calling again and again
outFile << setw(7) << "-----" << setw(20) << "---------" << setw(15) << "----------" << setw(20) << "----"
<< setw(12) << "------------" << endl;
cout << "Program has finished execution." << endl;
inFile.close();
outFile.close();
}
void printFileHeader(ofstream& outFile) {
outFile << left << setw(7) << "Entry" << setw(20) << "Last Name" << setw(15) << "First Name" << setw(20) << "City"
<< setw(12) << "Phone Number" << endl;
outFile << setw(7) << "-----" << setw(20) << "---------" << setw(15) << "----------" << setw(20) << "----"
<< setw(12) << "------------" << endl;
}
bool openInputFile(ifstream& inFile) {
string filename;
cout << "Enter the name of the input file: ";
cin >> filename;
cout << filename << endl << endl;
inFile.open(filename.c_str());
while(inFile.fail()) {
cout << string(15,'*') << " File Open Error " << string(15,'*') << endl;
cout << "==> Input file failed to open properly!!\n";
cout << "==> Attempted to open file: " << filename << endl;
cout << "==> Try Again\n";
cout << string(47,'*') << endl<< endl;
inFile.clear();
cout << "Enter in the name of the input file: ";
cin >> filename;
cout << filename << endl << endl;
inFile.open(filename.c_str());
}
return 1;
}
void openOutputFile(ofstream& outFile) {
string filename;
cout << "Enter the name of the output file: ";
cin >> filename;
cout << filename << endl << endl;
outFile.open(filename.c_str());
while(outFile.fail()) {
cout << string(15,'*') << " File Open Error " << string(15,'*') << endl;
cout << "==> Output file failed to open properly!!\n";
cout << "==> Attempted to open file: " << filename << endl;
cout << "==> Try Again\n";
cout << string(47,'*') << endl<< endl;
outFile.clear();
cout << "Enter in the name of the input file: ";
cin >> filename;
cout << filename << endl << endl;
outFile.open(filename.c_str());
}
}
void readInfo(ifstream& inFile, std::vector<Info> &infoVec) {
std::string temporaryZipCode;
Info info;
while(getline(inFile, info.person.phoneNumber,'\n'),
getline(inFile, info.person.lastName,'\n'),
getline(inFile, info.address.streetNum_Name,'\n'),
getline(inFile, info.address.city,'\n'),
getline(inFile, info.address.state,'\n'),
getline(inFile, temporaryZipCode, '\n'),
getline(inFile, info.person.firstName,'\n')
)
{
std::istringstream ss(temporaryZipCode);
ss >> info.address.zip_code;
infoVec.emplace_back(info);
}
}
void printInfo(ofstream& outFile, std::vector<Info> &infoVec) {
int entry = 0; //no need for making it static
for(const Info& record: infoVec )
{
outFile << left << setw(7) << entry << setw(20) << record.person.lastName << setw(15) << record.person.firstName
<< setw(20) << record.address.city << setw(12) << record.person.phoneNumber << endl;
entry++;
}
}
001.000.0000
rana
street1
city1
state1
1001
anoop
002.000.0000
ranaagain
street2
city2
state2
1002
anoopagain
Entry Last Name First Name City Phone Number
----- --------- ---------- ---- ------------
0 rana anoop city1 001.000.0000
1 ranaagain anoopagain city2 002.000.0000
----- --------- ---------- ---- ------------