// fields for file data
int recordCode;
int salesRep;
string dateOfSale;
string productCode;
string productDescription;
int quantitySold;
double unitPrice;
int amount = 0; // initialize numeric variables
// control break field
string previousLastName; //
// program total fields
// groupTotal is the accumulation of all amounts of one person.
// -each unique person is considered a group
// grandTotal is the accumulation of all groupTotals
int groupTotal = 0; // group subtotal by last name
int grandTotal = 0; // total of all groups
// program identifiers
string programLoop;
int returnStatus;
/* statements *************************************************
declare program instructions
*/
cout << "*** start of program ***" << endl;
// Step #2: do while program loop is Y or y
while(programLoop == "y" || programLoop == "Y")
{
// Step #3: get filename for user
cout << "Enter filename(\"SALES_01.txt\"): ";
cin >> filename;
cout << endl;
// Step #4: open the file
inData.open( filename.c_str(), ios::in );
// Step #5: if file open failure
if(!inData)
{
// Step #6 display file open failure message
cout << "<< Unable of open the file: " << filename << endl;
cin.get();
// Step #7 set return code to 1 (unsuccessful operation)
returnStatus = 1;
}
else
{
// Step #8 set return code to 0 (successful operation)
returnStatus = 0;
// Step #9 read the fields of first row from the file
inData >> recordCode;
inData >> salesRep;
inData >> dateOfSale;
inData >> productCode;
inData >> productDescription;
inData >> quantitySold;
inData >> unitPrice;
// Step #10 while not end of file
while(!inData.eof() )
{
// Step #11 save current value to previous value
previousLastName = lastName;
// Step #12 re-initialize group subtotal for new group
groupTotal = 0;
// Step #13 while not eof and current equal previous
while( !inData.eof() && previousLastName == lastName)
{
// Step #14 format & display the file fields
cout << fixed << showpoint << setprecision(2);
cout << left << setw(3) << recordCode;
cout << left << setw(6) << salesRep;
dateOfSale.insert(4, "-");
dateOfSale.insert(2, "-");
cout << right << setw(9) << dateOfSale;
cout << right << setw(6) << productCode;
cout << right << setw(16) << productDescription;
cout << right << setw(9) << quantitySold;
cout << fixed << setprecision(2) << right << setw(8) << unitPrice;
cout << endl;
// Step #15 accumulate group total
groupTotal += amount;
// Step #16 read fields of the next record
inData >> recordCode;
inData >> salesRep;
inData >> dateOfSale;
inData >> productCode;
inData >> productDescription;
inData >> quantitySold;
inData >> unitPrice;
} // end while not eof and current equal previous
cout << endl;
// Step #17 display group subtotals of an individual
cout << setw(30) << "Individual Total:";
cout << setw(10) << groupTotal;
cout << " *" << endl << endl;
// Step #18 accumulate grand total from group total
grandTotal += groupTotal;
} // end while not end of file
// Step #19 close the file
inData.clear();
inData.close();
// Step #20 display grand total
cout << right << setw(30);
cout << "Grand Total:";
cout << setw(10) << grandTotal;
cout<<" **"<<endl;
} // end if file open failure
// Step #21 reset for next file...
grandTotal = 0;
// Step #22 run program again?
cout << "\nRun program again(y/n)? ";
cin >> programLoop;
} // end do while program loop is Y or y
// Step #23 terminate the program
cout<<"*** end of program ***"<<endl;
cin.get();