Description: Reads a record from the file, then decides if the person
lives in Missouri (MO) or Kansas (KS). If they live in
Kansas, then it prints the record to a file named kansas.txt.
Otherwise, the program prints it to the file named missouri.txt.
Inputs: fin
Outputs: moOut, ksOut
Algorithm: open & check files
print headers
while we can read data
if state is MO
print to moFile
else
print to ksFile
end while loop
close files
*********************************************/
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct addressType//declare struct
{
string fname, lname, city, state, street;
int zip, house;
};
//protype for file headers
void printHeader (ofstream &out, string state);
//protype for reading the input file
bool readData(ifstream &in, addressType persons);
//protype for printing data to output files
void printData(ofstream &out, addressType persons);
int main()
{
ifstream fin("program5.txt");//in file stream for fin
ofstream moOut("kansas.txt");//declaring output file for missouri residents
ofstream ksOut("missouri.txt");//declaring output file for kansas residents
if(fin.fail())//if to check that the input file opens
{
cerr << "Unable to open input file\n";
exit(3);
}
addressType person;//delcaring variable for struct
printHeader(moOut, "Missouri");//printing file headers
printHeader(ksOut, "Kansas");
int i=0;
while(readData(fin, person))
{
if (person.state == "MO")
{
printData(moOut, person);
}
else
{
printData(ksOut, person);
}
i++;
}
fin.close();//close files
moOut.close();
ksOut.close();
return 0;
}
void printHeader (ofstream &out, string state)//function for output file header
{
out << "First Name" << setw (15) << "Last Name" << setw (10) << "House #"
<< setw (13) << "Street" << setw (13) << "City" << setw (10)
<< "State" << setw (10) << "Zip Code" << endl;
}
//function to read data from output file
bool readData(ifstream &in, addressType persons)
{
in >> persons.fname >> persons.lname >> persons.house >> persons.street
>> persons.state >> persons.zip;
return in.good();//return true statement
}
//function to print data to output file
void printData(ofstream &out, addressType persons)
{
out << persons.fname << setw (15) << persons.lname << setw (10) << persons.house
<< setw (13) << persons.street << setw (13) << persons.city << setw (10)
<< persons.state << setw (10) << persons.zip << endl;
}
** Edit **
Attached File(s)
-
program5.txt (21.29K)
Number of downloads: 108

New Topic/Question
Reply




MultiQuote




|