I have this program for computing an individual's cable bill, then summing each column of values and calculating the average of each column. I got a 100 on the assignment, the program works fine, but this time we have to use functions. I created my functions and the program, again, works fine. Only problem is I have to convert money values into integers for dollars and cents and print them explicitly.

My problem is I don't know where to keep money values in doubles and when to actually convert them to integers. Ugh, this is so frustrating.

int main()	{

	// Constants
	double UTILITYFEE	= 3.00;		// convert into cents
	double TAX		= 0.0012;		// convert into cents

	// Input Variables
	string CustomerName;		// Customer's first name
	string AccountNo;			// Customer's last name
	string Date;				// Date
	double Outlets;				// Number of Outlets
	char Cable;					// Type of cable service
	ifstream In;				// input stream
	ofstream Out;				// output stream

	// Local Variables
	int NumCustomers;					// number of customers
	double ServiceCharge;				// charge for N, B, or P
	double RegTax;						// tax on services
	double OutletCharge;				// number of outlets over one
	int TotalOutletCharge;				// total charge of outlets
	int TotalCustomerCharge;			// total charge of services for customer
	int TotalServiceCharge;				// total charge of all services
	int TotalUtilityCharge;				// total charge of utility tax
	int TotalTax;						// total regulatory tax
	int TotalFee;						// total charged to customer
	double AverageOutlet;				// average number of outlets
	double AverageService;				// average service charge
	double AverageUtility;				// average utility fee
	double AverageTax;					// average regulatory tax
	double AverageFee;					// average total fee
	
	// Initialize Variables
	NumCustomers				= 0;
	TotalOutletCharge			= 0;
	TotalCustomerCharge			= 0;		
	TotalServiceCharge			= 0;
	TotalUtilityCharge			= 0;
	TotalTax					= 0;
	TotalFee					= 0;
	AverageOutlet				= 0;
	AverageService				= 0;
	AverageUtility				= 0;
	AverageTax					= 0;
	AverageFee					= 0;

	// Open input and output file streams
	In.open("CableRecs.txt");
	Out.open("CableBills.txt");

	Out << fixed << showpoint;		// enables setprecision for output file

	// Print error message if input file not found
		if (In.fail())	{
			cout << "Input file not found" << endl;
			cout << "Exiting..." << endl;
			
				return EXIT_FAILURE;
		}

	// Print output header
	PrintHeader( Out );

	// Priming Read
	In.ignore(35, '\n');
	getline(In, CustomerName, '\t');
	getline(In, AccountNo, '\t');
	getline(In, Date, '\t');
	In >> Outlets >> Cable;

	while ( In )	{
		NumCustomers++;										// count number of customers
		CalcOutletCharge( Outlets, OutletCharge );			// calculate outlet charge
		SumTotal( OutletCharge, TotalOutletCharge );			// sum total outlet charge
		SumTotal( UTILITYFEE, TotalUtilityCharge );			// sum total utility fee

		PickService( In, Cable, ServiceCharge );			// pick service plan

		CalcRegTax( ServiceCharge, OutletCharge, TAX, RegTax );												// calculate regulatory tax
		CalcTotalCustomerCharge( ServiceCharge, OutletCharge, UTILITYFEE, RegTax, TotalCustomerCharge );	// calculate total customer charges
		SumTotal( RegTax, TotalTaxDollars, TotalTaxCents );																		// sum total tax
		SumTotal( TotalCustomerCharge, TotalFee );															// sum total customer charges
		SumTotal( ServiceCharge, TotalServiceCharge );														// sum total service charges

		PrintResults( Out, CustomerName, AccountNo, Date, OutletCharge, ServiceCharge, UTILITYFEE, RegTax, TotalCustomerCharge );	// print results to output

		// get new line of input
		getline(In, CustomerName, '\t');
		getline(In, AccountNo, '\t');
		getline(In, Date, '\t');
		In >> Outlets >> Cable;

	}

	CalcAverage( TotalOutletCharge, NumCustomers, AverageOutlet );		// average outlet charge
	CalcAverage( TotalServiceCharge, NumCustomers, AverageService );		// average service charge
	CalcAverage( TotalUtilityCharge, NumCustomers, AverageUtility );	// average utility charge
	CalcAverage( TotalTax, NumCustomers, AverageTax );					// average tax charge
	CalcAverage( TotalFee, NumCustomers, AverageFee );					// average fee charge

	// print rest of output file
	PrintCloser( Out, TotalOutletCharge, TotalServiceCharge, TotalUtilityCharge,
				 TotalTax, TotalFee, AverageOutlet, AverageService, AverageUtility,
				 AverageTax, AverageFee );

return EXIT_SUCCESS;

}


/////////////////////////////////////////////////////////////////////////// PrintHeader
//	Prints header of output file
//
//	Parameters:
//		Out		file stream to be printed to
//
//	Returns:	header of output file
//
//	Pre:		ofstream opened
//	Post:		header of output file printed
//
void PrintHeader( ofstream& Out)
{

	Out << "Programmer: " << "Ryan Dougherty" << endl;
	Out << "CS 1044 Project 4 Fall 2007" << endl << endl;
	
	Out << "Adufia Customer Billing Report:" << endl << endl;
	
	Out << left;
	Out << setw(24) << "Customer"
		<< setw(18)	<< "Account"
		<< setw(14) << "Billing"
		<< setw(8)	<< "Outlets"
		<< setw(8)	<< "Service"
		<< setw(8)	<< "Utility"
		<< setw(11)	<< "Regulatory"
		<< setw(6)	<< "Total" << endl;
	
	Out << setw(24)	<< "Name"
		<< setw(18)	<< "Number"
		<< setw(14)	<< "Date"
		<< setw(8)	<< "Charge"
		<< setw(8)	<< "Plan"
		<< setw(8)	<< "Tax"
		<< setw(11)	<< "Tax"
		<< setw(6)	<< "Fee" << endl;

	Out << "________________________________________________________________________________________________"
		<< endl;
}

/////////////////////////////////////////////////////////////////////////// CalcOutletCharge
//	Calculates charge for outlets used over 1
//
//	Parameters:
//		int 	number of outlets used
//
//	Returns:	charge for outlets in cents
//
//	Pre:		outlets read from input file
//	Post:		outlet charge stored into OutletCharge
//
void CalcOutletCharge( double Outlets, double& OutletCharge )
{
	OutletCharge = (Outlets - 1.0);		// will calculate outlet charge in cents
}

/////////////////////////////////////////////////////////////////////////// SumTotal
//	keeps a running total of a sum of numbers
//
//	Parameters:
//		int		number to be summed
//		int&	sum of the numbers
//
//	Returns:	sum stored into variable
//
//	Pre:		sum is initialized, number to be summed has value
//	Post:		sum is updated
//
void SumTotal( double Number, double& Sum )
{
	int Sum;
	int Temp;

	Temp = Number * 100;
	Sum = Sum + Number;
	
	Dollars = Sum / 100;
	Cents	= Sum % 100;
}

/////////////////////////////////////////////////////////////////////////// PickService
//	picks the service plan/charge for each customer based on a char value
//
//	Parameters:
//		ifstream&	for if statement
//		char		determines service plan type
//		int&		service plan charge, in cents
//
//	Returns:		service plan charge in cents
//
//	Pre:			char value is read from input file
//	Post:			service plan is determined for customer
//
void PickService( ifstream& In, char Cable, double& ServiceCharge )
{
	int temp;

	if (Cable == 'N')
			ServiceCharge = 14.95;		// converts service charge to cents
		else if (Cable == 'B')
			ServiceCharge = 34.95;		// converts service charge to cents
		else if (Cable == 'P')
			ServiceCharge = 69.95;		// converts service charge to cents
}

/////////////////////////////////////////////////////////////////////////// CalcRegTax
//	calculates regulatory tax for customer
//
//	Parameters:
//		int			charge for service			
//		int			charge for outlets
//		int			tax applied
//		int&		regulatory tax calculated
//
//	Returns:		regulatory tax to be added to customer's bill
//
//	Pre:			all variables have values stored, regtax intialized
//	Post:			regtax given new value for customer
//
void CalcRegTax(double ServiceCharge, double OutletCharge, double TAX, double& RegTax)
{
	RegTax = (ServiceCharge + OutletCharge) * TAX;	// tax is converted to cents
		
}

/////////////////////////////////////////////////////////////////////////// CalcTotalCustomerCharge
//	calculates total bill for customer
//
//	Parameters:
//		int			service charge in cents		
//		int			outlet charge in cents
//		int			regulatory taxin cents
//		int			utility fee in cents
//		int&		total charge for customer
//
//	Returns:		total charge for individual customer
//
//	Pre:			all variables have values stored, totalcustomercharge initialized
//	Post:			totalcustomercharge stored with new value
//
void CalcTotalCustomerCharge( double ServiceCharge, double OutletCharge, double UTILITYFEE, double RegTax, double& TotalCustomerCharge )
{
	TotalCustomerCharge = ServiceCharge + OutletCharge + UTILITYFEE + RegTax;
}

/////////////////////////////////////////////////////////////////////////// PrintResults
//	prints results for each customer
//
//	Parameters:
//		ofstream&	output file stream
//
//	Returns:		printed values of results
//
//	Pre:			all variables have values
//	Post:			results printed to output file
//
void PrintResults( ofstream& Out, string CustomerName, string AccountNo, string Date, double OutletCharge, double ServiceCharge, double UTILITYFEE, double RegTax, double TotalCustomerCharge )
{
		Out << left;
		Out << setw(24) << CustomerName;
		Out << setw(18) << AccountNo;
		Out << setw(15) << Date;
		Out << setw(8)	<< setprecision(2)	<< OutletCharge;
		Out << setw(8)	<< setprecision(2)	<< ServiceCharge;
		Out << setw(8)	<< setprecision(2)	<< UTILITYFEE;
		Out << setw(11)	<< setprecision(2)	<< RegTax;
		Out << setw(6)	<< setprecision(2)	<< TotalCustomerCharge	<< endl;
}

/////////////////////////////////////////////////////////////////////////// CalcAverage
//	calculates the average of a sum and the number of customers
//
//	Parameters:
//		int			total to be averaged
//		int			number of customers
//		int&		average value in cents
//
//	Returns:		average value in cents
//
//	Pre:			all variables have values
//	Post:			average is calculated and stored into average
//
void CalcAverage( double Total, int Divisor, double& Average )
{
	Average		= Total / Divisor;
}

/////////////////////////////////////////////////////////////////////////// PrintCloser
//	prints the rest of the output file
//
//	Parameters:
//		ofstream&	output file stream
//
//	Returns:		rest of output file
//
//	Pre:			all variables have values
//	Post:			all of output file is printed
//
void PrintCloser( ofstream& Out, double TotalOutletCharge, double TotalServiceCharge, double TotalUtilityCharge, double TotalTax, double TotalFee, double AverageOutlet, double AverageService, double AverageUtility, double AverageTax, double AverageFee )
{

	Out << "________________________________________________________________________________________________"
		<< endl;
	Out << left;
	Out << setw(56) << "Totals:"
		<< setw(8)	<< setprecision(2) << TotalOutletCharge
		<< setw(8)	<< setprecision(2) << TotalServiceCharge
		<< setw(8)	<< setprecision(2) << TotalUtilityCharge
		<< setw(11) << setprecision(2) << TotalTax
		<< setw(6)	<< setprecision(2) << TotalFee	<< endl;
	Out << setw(56) << "Averages:"
		<< setw(8)  << setprecision(2) << AverageOutlet
		<< setw(8)	<< setprecision(2) << AverageService
		<< setw(8)	<< setprecision(2) << AverageUtility
		<< setw(11) << setprecision(2) << AverageTax
		<< setw(6)	<< setprecision(2) << AverageFee << endl;
}

HELP :-/

Write another function that simply prints out the double values by converting to ints. Then at all places you output the values, call the function instead.

This article has been dead for over six months. Start a new discussion instead.