/*
	**************************
	Patrick Schmid
	pds2
	13 February 2006
	Lab 8 Solution
	Patrick Schmid
	Section 10 & 11
	**************************
	Purpose: Print entered check and checking account information
		     with formatted output to a file
	Algorithm: Ask user for checking account balance, then ask repeatedly
	           for check information until user enters 0 as check number
*/

#include <fstream.h>
#include <iomanip.h>

void main() {
	double balance; //balance of checking account
	double amount; //check amount
	int checkNumber, month, day; //check number, month and day

	//open output file
	ofstream out("checkbook.txt", ios::out);

	//set output to be with fixed number of digits
	out<<setiosflags(ios::fixed);

	//get balance information
	cout<<"Please enter your beginning checking account balance: ";
	cin>>balance;

	//print balance information and table header to file
	out<<"Your balance at the beginning of this session was $"<<setprecision(2)<<balance<<endl;
	out<<"Your transactions were: \n\n";
	out<<"Check number       Date        Amount         Balance\n";

	//we are using a do-while loop, because we need to execute this loop at
	//least once, so that user can enter 0 as first check number to terminate loop
	do {

		//get check number
		cout<<"Please enter a check number (0 if there are no more checks): ";
		cin>>checkNumber;

		//only process check information, if check number != 0
		if (checkNumber != 0) {

			//get check data
			cout<<"Please enter the amount of the check: ";
			cin>>amount;
			cout<<"Please enter the month this check was written: ";
			cin>>month;
			cout<<"Please enter the day this check was written: ";
			cin>>day;
			
			//update balance
			balance-=amount;
			
			//output check information and new balance to table in a formatted
			//manner
			out<<setw(7)<<checkNumber<<"           "
				<<setfill('0')<<setw(2)<<month<<"/"<<setw(2)<<day
				<<"     $"<<setfill(' ')<<setw(8)<<setprecision(2)<<amount
				<<"       $"<<setw(8)<<setprecision(2)<<balance<<endl;

		}

	} while (checkNumber != 0);

	//output ending balance
	out<<"\nYour balance at the end of this session is $"<<setprecision(2)<<balance<<endl;

	//close file
	out.close();
}
