This is just a programming example I am learning from.
I can't get this to output to a text file.
Input file:
1126 Andrew Jackson ILL 5000.00 3245.11
Code:
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<string.h>
const int MaxNameLen = 31;
const int MaxStCodeLen = 3;
const int MaxNum = 500;
struct CREDITREC {
long acctno; //account number = social security number;
char custName[MaxNameLen]; //Customer name upto 30 cahracters.
char stateCode[MaxStCodeLen]; //upper case state code.
double creditlimit; //credit limit of customers
double balance; //customers current balance due.
};
int LoadArray(CREDITREC crec[], int limit, char filename[]);
void SortArray(CREDITREC crec[], int numRecs);
void PrintReport(CREDITREC crec[], int numRecs, char filename[]);
int main(){
CREDITREC creditRec[MaxNum]; //array of customer credit records.
int numRecs; //current number of customers in array.
//Obtain the input and report filenames from the user
char inputFilename[_MAX_PATH];
char reportFilename[_MAX_PATH];
cout<<"Enter the name of the credit master file: ";
cin>>inputFilename;
cout<<"Enter the name of the output filename: ";
cin>>reportFilename;
//load the array of credit records
numRecs = LoadArray (creditRec, MaxNum, inputFilename);
cout<<"\nArray loaded: "<<numRecs<<"records input\n";
//sort the arrray into a state code order
cout<<"Sorting the array\n";
SortArray(creditRec, numRecs);
//Produce the report
cout<<"Printing the report\n";
PrintReport(creditRec, numRecs, reportFilename);
cout<<"Finished\n";
return 0;
}
//////////////////////////////////////////////////////////////////////
//Load Array //
//////////////////////////////////////////////////////////////////////
int LoadArray(CREDITREC crec[], int limit, char filename[]){
ifstream infile;
infile.open(filename, ios::in | ios::nocreate );
if (!infile){
cerr<<"Error: cannot open"<<filename<<endl;
exit (1);
}
int i = 0;
char c;
while(i < limit && infile >>crec[i].acctno >> c){
//input the customer name " delimited
infile.getline(crec[i].custName, MaxNameLen, '\"');
//input the next byte which can be the " if thee name is length.
infile.get (c);
//if it was the " then skip ws
infile>>ws;
infile.get (crec[i].stateCode, MaxStCodeLen);
infile>>crec[i].creditlimit>>crec[i].balance;
i++;
}
//guard against input data
if(infile.bad()){
cerr<<"Error: bad data input file.\nLast good id was";
if((i - 1)== 0)
cerr<<"the first line\n";
else
cerr<<crec[i - 1].acctno<<endl;
infile.close ();
exit (3);
}
infile.close ();
return i;//return the number of records
}
//////////////////////////////////////////////////////////////////////
//SortArray: sorts thee credit records into state code order //
//////////////////////////////////////////////////////////////////////
void SortArray (CREDITREC crec[], int numRecs){
CREDITREC temp;
int i, j;
for(i=0; i<numRecs-1; i++){
for(j=i+1; j<numRecs; j++){
if (strcmp (crec[j].stateCode, crec[i].stateCode) < 0){
temp = crec[i];
crec[i] = crec[j];
crec[j] = temp;
}
}
}
}
//////////////////////////////////////////////////////////////////////
//PrintReport: summarize data wile printing the report //
//////////////////////////////////////////////////////////////////////
void PrintReport(CREDITREC crec[], int numRecs, char filename[]){
char prevState[MaxStCodeLen]; //the previous state code for breaks
int numAcctInState = 0; //number of accounts in this state
double totalLimit = 0; //total credit limit in this state
double totalBalance = 0; //total credit extended in this state
int grandNumStates = 0;
int grandNumAccts = 0; //company total number of accounts.
double grandTotalLimit = 0; //company total credit limit
double grandTotalBalance = 0; //company total credit extended
//open report file and print hedings and column headings
ofstream outfile (filename);
outfile<<"Acme Credit Company - Creedit Extended by State Report\n\n"
<<"State Number of Credit Totals Extended\n"
<<"Code Accounts Limit Balance\n";
outfile.setf(ios::fixed, ios::floatfield);
outfile.setf(ios::showpoint);
outfile<<setprecision(2);
//guard against no records in the array
if(numRecs){
//initialize the previous state code to the first record
//and initialize the initial state totals
strcpy(prevState, crec[0].stateCode);
totalLimit = crec[0].creditlimit;
totalBalance = crec[0].balance;
numAcctInState;
//Loop through all the credit records
for(int i=1; i < numRecs; i++){
//check for a change in state code
if (strcmp (prevState, crec[i].stateCode) != 0){
//this stat is the next state so print totals of the previous state
outfile<<setw(4)<<prevState
<<setw(9)<<numAcctInState
<<setw(7)<< "$" <<setw(9)<<totalLimit
<<setw(6)<< "$" <<setw(9)<<totalBalance<<endl;
//roll previous state totals into grand company totals
grandNumAccts += numAcctInState;
grandNumStates++;
grandTotalLimit += totalLimit;
grandTotalBalance += totalBalance;
//reset previous state code to the new one
strcpy(prevState, crec[i].stateCode);
//reset the state totals to zero.
numAcctInState = 0;
totalLimit = 0;
totalBalance = 0;
}
//accumulate this state values
numAcctInState++;
totalLimit += crec[i].creditlimit;
totalBalance += crec[i].balance;
}
//print last state in the set of data
outfile<<setw(4)<<prevState
<<setw(9)<<numAcctInState
<<setw(7)<< "$" <<setw(9)<<totalLimit
<<setw(6)<< "$" <<setw(9)<<totalBalance<<endl;
grandNumAccts += numAcctInState;
grandNumStates++;
grandTotalLimit += totalLimit;
grandTotalBalance += totalBalance;
}
//print the grand company totals
outfile<<" -- --- -------- -----------\n"
<<setw(4)<<grandNumStates
<<setw(9)<<grandNumAccts
<<setw(7)<< "$" <<setw(9)<<grandTotalLimit
<<setw(6)<< "$" <<setw(9)<<grandTotalBalance<<endl;
outfile.close();
}