1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
const int MAX_CLIENT_SIZE = 100;
const int FIRST_NAME_LEN = 11;
const int LAST_NAME_LEN = 13;
const int ADDRESS_LEN = 25;
const int CITY_NAME_LEN = 16;
const int STATE_LEN = 3;
struct CustomerType
{
char lastName[LAST_NAME_LEN];
char firstName[FIRST_NAME_LEN];
char streetAddress[ADDRESS_LEN];
char city[CITY_NAME_LEN];
char state[STATE_LEN];
int zipCode;
};
void reportHeading(ostream& outfile);
int getInfo(CustomerType struct_array[], istream& infile, int& count);
void printCustomers(ostream& outfile, CustomerType output[], int& count);
void sortByLastName(CustomerType struct_array[], int& count);
int main()
{
int count;
CustomerType clients[MAX_CLIENT_SIZE];
ifstream infile("PJ902_customers.txt");
if (!infile){
cerr << "File loading failed.\n";
return -1;
}
ofstream outfile("PJ902_report.txt");
if (!outfile)
{
cerr << "File output failed.\n";
return -1;
}
reportHeading(outfile);
int success = getInfo(clients, infile, count);
cout << success << endl;
printCustomers(outfile, clients, count);
}
void reportHeading(ostream& outfile)
{
outfile << setw(30) << "Customer Information Report\n";
outfile << "Reported by Josh Panfil\n\n\n";
outfile << setw(10) << "First Name" << setw(10) << "Last Name" << setw(9) << "Address"
<< setw(20) << "City" << setw(8) << "State" << setw(12) << "Zip Code\n";
outfile << setw(10) << "----------" << setw(10) << "---------" << setw(9) << "-------"
<< setw(20) << "----" << setw(8) << "-----" << setw(12) << "--------\n";
}
int getInfo(CustomerType struct_array[], istream& infile, int& count)
{
int index = 0;
infile.get(struct_array[index].firstName, FIRST_NAME_LEN);
infile.get(struct_array[index].lastName, LAST_NAME_LEN);
infile.get(struct_array[index].streetAddress, ADDRESS_LEN);
infile.get(struct_array[index].city, CITY_NAME_LEN);
infile.get(struct_array[index].state, STATE_LEN);
infile >> struct_array[index].zipCode; if (!infile) cerr << "Fail";
while( index < MAX_CLIENT_SIZE && infile.good() )
{
index++;
infile >> ws;
infile.get(struct_array[index].firstName, FIRST_NAME_LEN);
infile.get(struct_array[index].lastName, LAST_NAME_LEN);
infile.get(struct_array[index].streetAddress, ADDRESS_LEN);
infile.get(struct_array[index].city, CITY_NAME_LEN);
infile.get(struct_array[index].state, STATE_LEN);
infile >> struct_array[index].zipCode;
}
count = index;
//Test the file status after reading in the data
if( index == MAX_CLIENT_SIZE && infile >> ws && infile.good() )
return 1; //indicator value for too many data
else if( !infile.eof() && infile.fail() )
return -1; //indicator value for bad data
else
return 0; //everything was successful
}
void printCustomers(ostream& outfile, CustomerType output[], int& count)
{
for (int i = 0; i < count; i++){
outfile << output[i].firstName << output[i].lastName << output[i].streetAddress
<< output[i].city << output[i].state << output[i].zipCode << endl;
}
}
|