Hi all,
I've been instructed to write a program that inputs a dollar amount to be printed on a check, and then prints the amount in a check-protected format with leading asterisks of necessary. Nine spaces are available fore printing an amount (max of $99,999.99). The program looks like this:

#include <iostream>
using namespace std;

int main()
{

char amount[10];
int length = 0;
int i;

cout<< "Please enter the check amount: " << endl;
cin>> amount;
while(amount[length] != '\0')
{ length++;
}

for(i = 0; i < length; i ++)
{ amount[8-i] = amount[length-1-i];
}

for(i=0; i < 9-length; i++)
{ amount[i] = '*';
}

amount[9]='\0';

cout<<"The check amount is: " << amount << endl;


return 0;

}

This part of the code works great... my instructor wasn't clear on whether or not to include commas, I think I might have a little trouble with that part. Anyway, there's a second part to the problem, a continuation of sorts. Those requirements are to print out the amount in words on the second line. I have the code written up to the tens place. However, I'm not sure how to do the thousands and/or hundreds. Would anyone mind helping?? Here's the code I have so far for the second part

#include <iostream>
using namespace std;

int main()
{
char amount[10];
int length = 0;
int dollars = 0;

cout<< "Please enter the check amount: " << endl;
cin>> amount;

while((amount[length] != '.') && (amount[length] !='\0'))
{ 
	dollars = (dollars * 10) + amount[length]-48;
	length++;
}

if (dollars > 19)
	switch(dollars/10)
{
	case 2 : cout<< "TWENTY ";
		break;
	case 3 : cout<<"THIRTY ";
		break;
	case 4 : cout<<"FOURTY ";
		break;
	case 5 : cout<<"FIFTY ";
		break;
	case 6 : cout<<"SIXTY ";
		break;
	case 7 : cout<<"SEVENTY ";
		break;
	case 8 : cout<<"EIGHTY ";
		break; 
	case 9 : cout<<"NINETY ";
		break;
}
else if (dollars > 9)
{ 
	switch(dollars)
	{
	case 10 : cout<<"TEN";
		break;
	case 11 : cout<<"ELEVEN";
		break;
	case 12 : cout<<"TWELVE";
		break;
	case 13 : cout<<"THIRTEEN";
		break;
	case 14 : cout<<"FOURTEEN";
		break;
	case 15 : cout<<"FIFTEEN";
		break;
	case 16 : cout<<"SIXTEEN";
		break;
	case 17 : cout<<"SEVENTEEN";
		break;
	case 18 : cout<<"EIGHTEEN";
		break;
	case 19 : cout<<"NINETEEN";
		break;
	}
	dollars = 0;
}
dollars = dollars % 10;

if ((dollars < 10) && (dollars > 0))
	switch(dollars)
{
case 1 : cout<<"ONE";
	break;
case 2 : cout<<"TWO";
	break;
case 3 : cout<<"THREE";
	break;
case 4 : cout<<"FOUR";
	break;
case 5 : cout<<"FIVE";
	break;
case 6 : cout<<"SIX";
	break;
case 7 : cout<<"SEVEN";
	break;
case 8 : cout<<"EIGHT";
	break;
case 9 : cout<<"NINE";
	break;
}

dollars = 0;
while (amount[length] != '\0')
{
	if(amount[length] != '.')
	{
		dollars = (dollars * 10 + amount[length]-48);
	}
	length++;
}

if(dollars > 0)
{ 
	cout << " AND " << dollars << "/100" << endl;
}

return 0;

}

Thanks for your time,
iTsweetie

I think you are on the right track. You can add code beforehand to deal with the ten thousands, thousands, and hundreds, and it can be quite similar (practically identical) to what you've done before when dealing with 1 through 9.

// assumes dollars < 1000 by this point
if (dollars > 100)
{
    switch (dollars / 100)
    {
        case 1 : cout<<"ONE";
	        break;
        case 2 : cout<<"TWO";
	        break;
        case 3 : cout<<"THREE";
	        break;
        case 4 : cout<<"FOUR";
	        break;
        case 5 : cout<<"FIVE";
	        break;
        case 6 : cout<<"SIX";
	        break;
        case 7 : cout<<"SEVEN";
	        break;
        case 8 : cout<<"EIGHT";
	        break;
        case 9 : cout<<"NINE";
	        break;
    }

    cout << " HUNDRED ";
    dollars = dollars % 100;
}

Note that lines 6 through 23 are simply a verbatim cut and paste of your earlier code. Thousands can be done the exact same way. So you'll have the exact same code (or close to the exact same code) several times.

As for commas, you can reverse your string, push it into another string, which adds a comma every three digits after the second, and reverse it again, so a theoretical ammount of $123456.78 would first go to

87.654321
87.654,321
123,456.78

alternatively, because you know there can only be one comma, create an if statement fo find whether or not ammount[2] is used or not; if it is insert a comma through any method of your choosing, if not, then there is no comma at all!

The code required to do this is well within your grasp so I'll leave that to you :) (its amazing how much effort the little things take isn't it?)

It is quite amazing and I thank you guys SO Much for all of your help.. It's nice to know that there are people in the world willing to help!

ITsweetie

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