//this program lets the user enter a number. the
//dollarFormat function formats the number as
//a dollar amount.
#include <iostream>
#include <string>
using namespace std;

//function prototype
void dollarFormat(string &);

int main()
{
	string input;
	
	//get the dollar amount from the user.
	cout << "Enter a dollar amount in the form nnnnn.nn : ";
	cin >> input;
	dollarFormat (input);
	cout << "Here is the amount formatted:\n";
	cout << input << endl;
	return 0;
}

//***********************************************************
// definition of the dollarFormat function. this function 	*
// accepts a string reference object, which is assumed to	*
// to hold a number with a decimal point. the function		*
// formats the number as a  dollar amount with commas and	*
// a $ symbol.												*
//***********************************************************

void dollarFormat(string &currency)
{
	int dp;
	
	dp = currency.find('.');	//find decimal point
	if (dp > 3)					//insert commas
	{
		for (int x = dp - 3; x > 0; x -= 3)
			currency.insert(x, ",");
	}
	currency.insert(0, "$");	//insert dollar sign
}
