void Introduction() // void statement what program does
{
cout << "This program is used to read a text file with various zip codes .\n"
<< "The first output will be a corrected tab showing the 4 digit zip .\n"
<< "codes as 5 digit zip coes with leading zeros.\n"
<< "The second output will show the Pennsylvania zip codes only " << endl << endl;
if (!openfile) // if file doesnt open gives error
{
return false;
}
else
{
return true;
}
}
int main()
{
string Zipcode;
bool works;
string line;
int numberoflines=0;
int numberofPA=0;
int numberofNJ=0;
int numberofNY=0;
int numberofPhiladelphia=0;
//double zipcode;
int numberofzipcodeswith19104=0;
int numberofBerlinMD=0;
int numberofBerlinNJ=0;
Introduction (); // introduction subprogram
Zipcode=promptForFileName("What is the name of the file?"); // prompt string subprogram
works= validForm(Zipcode); // shows if program works right
while (works == false)
{
cout << "Error opening file" << endl;
Zipcode=promptForFileName("Please enter a valid file name or QUIT to close the program."); // if program doesn work person can enter a different
if (Zipcode=="QUIT") // file or type quit to close program
return 0;
works= validForm(Zipcode);
}
ifstream openfile;
openfile.open(Zipcode.c_str()); //opens text file
ofstream outfile("PA.txt"); // outputs text file for all of PA information
while (!openfile.eof())
{
int index;
getline(openfile,line); //reads lines in text file
index=line.find("\t"); // goes through line finds first tab
if (line.substr(index+1,2)=="PA" ) // once the first 2 letters after tab is PA it counts
{
numberofPA++; // counts number of PA
outfile << line << endl; //outputs PA information in text file
}
index=line.find("\t"); // goes through line finds first tab
if (line.substr(index+1,2)=="NJ" ) // once the first 2 letters after tab is NJ counts
{
numberofNJ++; // counts number of NJ
}
index=line.find("\t"); // Finds first tab
if (line.substr(index+1,2)=="NY" ) // if find tab and next two spaces of are NY counts
{
numberofNY++; // counts number of NY
}
index=line.find(""); // Finds Phil
if (line.substr(index+1)== "Philadelphia PA") //find Philadelphia if next space is tab then counts Philadephia PA
{
numberofPhiladelphia++; // counts number of Philadelphia PA
}
index=line.find("19104");
if (line.substr(index+1)=="-")
{
numberofzipcodeswith19104++;
}
index=line.find("Berlin");
if (line.substr(index+1,2)=="\tM" )
{
numberofBerlinMD++;
}
index=line.find("Berlin");
if (line.substr(index+1,2)=="\tN" )
{
numberofBerlinMD++;
}
}
// All outputs
cout <<"Number of entries (lines of data) in the file, " << numberoflines <<endl;
cout <<"Number of state codes PA in the file, " << numberofPA <<endl;
cout <<"Number of state codes NJ in the file, " << numberofNJ <<endl;
cout <<"Number of state codes NY in the file, " << numberofNY <<endl;
cout <<"Number of Philadelphia PA in the file, " <<numberofPhiladelphia << endl;
//cout <<"Number of hyphen zipcodes in the file, " <<zipcode << endl;
cout <<"Number of 19104 in the file, "<< numberofzipcodeswith19104<< endl;
cout <<"Number of Berlin MD in the file, " <<numberofBerlinMD << endl;