GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Mar-2006, 15:11
Godzilla Godzilla is offline
New Member
 
Join Date: Mar 2006
Posts: 1
Godzilla is on a distinguished road

Converting a number amount to text


I am doing a checkwriting program using C strings for my intermediate programming class. We have covered loops, streams, if statements, arrays, pointers, and strings.

The person is supposed to enter a number like $1000. 50, and it is supposed to display it like "one thousand dollars and 50 cents" as a string. The highest value that can be entered is $10,000.

My book doesn't show any good examples that would help on this assignment and I am stumped. Help would be appreciated. Thanks

Note: I am using Microsoft Visual Studio.
  #2  
Old 30-Mar-2006, 06:16
Brenton S. Brenton S. is offline
New Member
 
Join Date: Mar 2006
Location: Piscataway, NJ
Posts: 21
Brenton S. is on a distinguished road
Smile

Re: Converting a number amount to text


Hi Godzilla,

This is the only way I can think of.

It's nothing fancy or anything.

It should work though.

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
	double number = 1000.50;


	if(number >= 10000)
	{
		printf("ten thousand ");

		number -= 10000;
	}
	if(number >= 9000)
	{
		printf("nine thousand ");

		number -= 9000;
	}
	if(number >= 8000)
	{
		printf("eight thousand ");

		number -= 8000;
	}
	if(number >= 7000)
	{
		printf("seven thousand ");

		number -= 7000;
	}
	if(number >= 6000)
	{
		printf("six thousand ");

		number -= 6000;
	}
	if(number >= 5000)
	{
		printf("five thousand ");

		number -= 5000;
	}
	if(number >= 4000)
	{
		printf("four thousand ");

		number -= 4000;
	}
	if(number >= 3000)
	{
		printf("three thousand ");

		number -= 3000;
	}
	if(number >= 2000)
	{
		printf("two thousand ");

		number -= 2000;
	}
	if(number >= 1000)
	{
		printf("one thousand ");

		number -= 1000;
	}
	if(number >= 900)
	{
		printf("nine hundred ");

		number -= 900;
	}
	if(number >= 800)
	{
		printf("eight hundred ");

		number -= 800;
	}
	if(number >= 700)
	{
		printf("seven hundred ");

		number -=700;
	}
	if(number >= 600)
	{
		printf("six hundred ");

		number -= 600;
	}
	if(number >= 500)
	{
		printf("five hundred ");

		number -= 500;
	}
	if(number >=400)
	{
		printf("four hundred ");

		number -= 400;
	}
	if(number >=300)
	{
		printf("three hundred ");

		number -= 300;
	}
	if(number >= 200)
	{
		printf("two hundred ");

		number -= 200;
	}
	if(number >= 100)
	{
		printf("one hundred ");

		number -= 100;
	}
	if(number >= 90)
	{
		printf("ninety ");

		number -= 90;
	}
	if(number >= 80)
	{
		printf("eighty ");

		number -= 80;
	}
	if(number >= 70)
	{
		printf("seventy ");

		number -= 70;
	}
	if(number >= 60)
	{
		printf("sixty ");

		number -= 60;
	}
	if(number >= 50)
	{
		printf("fifty ");

		number -= 50;
	}
	if(number >= 40)
	{
		printf("fourty ");

		number -= 40;
	}
	if(number >= 30)
	{
		printf("thirty ");

		number -= 30;
	}
	if(number >= 20)
	{
		printf("twenty ");

		number -= 20;
	}
	if(number >= 19)
	{
		printf("nineteen ");

		number -= 19;
	}
	if(number >= 18)
	{
		printf("eighteen ");

		number -= 18;
	}
	if(number >= 17)
	{
		printf("seventeen ");

		number -= 17;
	}
	if(number >= 16)
	{
		printf("sixteen ");

		number -= 16;
	}
	if(number >= 15)
	{
		printf("fifteen ");

		number -= 15;
	}
	if(number >= 14)
	{
		printf("fourteen ");

		number -= 14;
	}
	if(number >= 13)
	{
		printf("thirteen ");

		number -= 13;
	}
	if(number >= 12)
	{
		printf("twelve ");

		number -= 12;
	}
	if(number >= 11)
	{
		printf("eleven ");

		number -= 11;
	}
	if(number >= 10)
	{
		printf("ten ");

		number -= 10;
	}
	if(number >= 9)
	{
		printf("nine ");

		number -= 9;
	}
	if(number >= 8)
	{
		printf("eight ");

		number -= 8;
	}
	if(number >= 7)
	{
		printf("seven ");

		number -= 7;
	}
	if(number >= 6)
	{
		printf("six ");

		number -= 6;
	}
	if(number >= 5)
	{
		printf("five ");

		number -= 5;
	}
	if(number >= 4)
	{
		printf("four ");

		number -= 4;
	}
	if(number >=3)
	{
		printf("three ");

		number -= 3;
	}
	if(number >= 2)
	{
		printf("two ");

		number -= 2;
	}
	if(number >= 1)
	{
		printf("one ");

		number -= 1;
	}



	printf("dollars ");


	if(number >= 0)
	{
		printf("and ");
	}



	if(number >= .90)
	{
		printf("ninety ");

		number -= .90;
	}
	if(number >= .80)
	{
		printf("eighty ");

		number -= .80;
	}
	if(number >= .70)
	{
		printf("seventy ");

		number -= .70;
	}
	if(number >= .60)
	{
		printf("sixty ");

		number -= .60;
	}
	if(number >= .50)
	{
		printf("fifty ");

		number -= .50;
	}
	if(number >= .40)
	{
		printf("fourty ");

		number -= .40;
	}
	if(number >= .30)
	{
		printf("thirty ");

		number -= .30;
	}
	if(number >= .20)
	{
		printf("twenty ");

		number -= .20;
	}
	if(number >= .19)
	{
		printf("nineteen ");

		number -= .19;
	}
	if(number >= .18)
	{
		printf("eighteen ");

		number -= .18;
	}
	if(number >= .17)
	{
		printf("seventeen ");

		number -= .17;
	}
	if(number >= .16)
	{
		printf("sixteen ");

		number -= .16;
	}
	if(number >= .15)
	{
		printf("fifteen ");

		number -= .15;
	}
	if(number >= .14)
	{
		printf("fourteen ");

		number -= .14;
	}
	if(number >= .13)
	{
		printf("thirteen ");

		number -= .13;
	}
	if(number >= .12)
	{
		printf("twelve ");

		number -= .12;
	}
	if(number >= .11)
	{
		printf("eleven ");

		number -= .11;
	}
	if(number >= .10)
	{
		printf("ten ");

		number -= .10;
	}
	if(number >= .09)
	{
		printf("nine ");

		number -= .09;
	}
	if(number >= .08)
	{
		printf("eight ");

		number -= .08;
	}
	if(number >= .07)
	{
		printf("seven ");

		number -= .07;
	}
	if(number >= .06)
	{
		printf("six ");

		number -= .06;
	}
	if(number >= .05)
	{
		printf("five ");

		number -= .05;
	}
	if(number >= .04)
	{
		printf("four ");

		number -= .04;
	}
	if(number >= .03)
	{
		printf("three ");

		number -= .03;
	}
	if(number >= .02)
	{
		printf("two ");

		number -= .02;
	}
	if(number >= .01)
	{
		printf("one ");

		number -= .01;
	}

	printf("cents");


	return 0;
}

I hope I've been of some help.

If I can think of a better way, I'll post it here.
Last edited by cable_guy_67 : 30-Mar-2006 at 06:38. Reason: Please enclose c code in [c] ... [/c] tags
  #3  
Old 30-Mar-2006, 08:29
davis
 
Posts: n/a

Re: Converting a number amount to text


Quote:
Originally Posted by Brenton S.
It should work though.

Here is a slight improvement on your code, but it still doesn't meet the requirements as written.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char buffer[16] = {0};
    printf( "enter a dollar.cents amount: " );
    fgets( buffer, sizeof( buffer ), stdin );
    buffer[ -1 + strlen( buffer ) ] = '\0';

    float number = (float)atof( buffer );


    if( number >= 10000 )
    {
        printf("ten thousand ");

        number -= 10000;
    }
    if( number >= 9000 )
    {
        printf("nine thousand ");

        number -= 9000;
    }
    if( number >= 8000 )
    {
        printf("eight thousand ");

        number -= 8000;
    }
    if( number >= 7000 )
    {
        printf("seven thousand ");

        number -= 7000;
    }
    if( number >= 6000 )
    {
        printf("six thousand ");

        number -= 6000;
    }
    if( number >= 5000 )
    {
        printf("five thousand ");

        number -= 5000;
    }
    if( number >= 4000 )
    {
        printf("four thousand ");

        number -= 4000;
    }
    if( number >= 3000 )
    {
        printf("three thousand ");

        number -= 3000;
    }
    if( number >= 2000 )
    {
        printf("two thousand ");

        number -= 2000;
    }
    if( number >= 1000 )
    {
        printf("one thousand ");

        number -= 1000;
    }
    if( number >= 900 )
    {
        printf("nine hundred ");

        number -= 900;
    }
    if( number >= 800 )
    {
        printf("eight hundred ");

        number -= 800;
    }
    if( number >= 700 )
    {
        printf("seven hundred ");

        number -=700;
    }
    if( number >= 600 )
    {
        printf("six hundred ");

        number -= 600;
    }
    if( number >= 500 )
    {
        printf("five hundred ");

        number -= 500;
    }
    if( number >=400 )
    {
        printf("four hundred ");

        number -= 400;
    }
    if( number >=300 )
    {
        printf("three hundred ");

        number -= 300;
    }
    if( number >= 200 )
    {
        printf("two hundred ");

        number -= 200;
    }
    if( number >= 100 )
    {
        printf("one hundred ");

        number -= 100;
    }
    if( number >= 90 )
    {
        printf("ninety ");

        number -= 90;
    }
    if( number >= 80 )
    {
        printf("eighty ");

        number -= 80;
    }
    if( number >= 70 )
    {
        printf("seventy ");

        number -= 70;
    }
    if( number >= 60 )
    {
        printf("sixty ");

        number -= 60;
    }
    if( number >= 50 )
    {
        printf("fifty ");

        number -= 50;
    }
    if( number >= 40 )
    {
        printf("fourty ");

        number -= 40;
    }
    if( number >= 30 )
    {
        printf("thirty ");

        number -= 30;
    }
    if( number >= 20 )
    {
        printf("twenty ");

        number -= 20;
    }
    if( number >= 19 )
    {
        printf("nineteen ");

        number -= 19;
    }
    if( number >= 18 )
    {
        printf("eighteen ");

        number -= 18;
    }
    if( number >= 17 )
    {
        printf("seventeen ");

        number -= 17;
    }
    if( number >= 16 )
    {
        printf("sixteen ");

        number -= 16;
    }
    if( number >= 15 )
    {
        printf("fifteen ");

        number -= 15;
    }
    if( number >= 14 )
    {
        printf("fourteen ");

        number -= 14;
    }
    if( number >= 13 )
    {
        printf("thirteen ");

        number -= 13;
    }
    if( number >= 12 )
    {
        printf("twelve ");

        number -= 12;
    }
    if( number >= 11 )
    {
        printf("eleven ");

        number -= 11;
    }
    if( number >= 10 )
    {
        printf("ten ");

        number -= 10;
    }
    if( number >= 9 )
    {
        printf("nine ");

        number -= 9;
    }
    if( number >= 8 )
    {
        printf("eight ");

        number -= 8;
    }
    if( number >= 7 )
    {
        printf("seven ");

        number -= 7;
    }
    if( number >= 6 )
    {
        printf("six ");

        number -= 6;
    }
    if( number >= 5 )
    {
        printf("five ");

        number -= 5;
    }
    if( number >= 4 )
    {
        printf("four ");

        number -= 4;
    }
    if( number >=3 )
    {
        printf("three ");

        number -= 3;
    }
    if( number >= 2 )
    {
        printf("two ");

        number -= 2;
    }
    if( number >= 1 )
    {
        printf("one ");

        number -= 1;
    }



    printf("dollars ");


    if( number >= 0 )
    {
        printf("and ");
    }



    if( number >= .90 )
    {
        printf("ninety ");

        number -= .90;
    }
    if( number >= .80 )
    {
        printf("eighty ");

        number -= .80;
    }
    if( number >= .70 )
    {
        printf("seventy ");

        number -= .70;
    }
    if( number >= .60 )
    {
        printf("sixty ");

        number -= .60;
    }
    if( number >= .50 )
    {
        printf("fifty ");

        number -= .50;
    }
    if( number >= .40 )
    {
        printf("fourty ");

        number -= .40;
    }
    if( number >= .30 )
    {
        printf("thirty ");

        number -= .30;
    }
    if( number >= .20 )
    {
        printf("twenty ");

        number -= .20;
    }
    if( number >= .19 )
    {
        printf("nineteen ");

        number -= .19;
    }
    if( number >= .18 )
    {
        printf("eighteen ");

        number -= .18;
    }
    if( number >= .17 )
    {
        printf("seventeen ");

        number -= .17;
    }
    if( number >= .16 )
    {
        printf("sixteen ");

        number -= .16;
    }
    if( number >= .15 )
    {
        printf("fifteen ");

        number -= .15;
    }
    if( number >= .14 )
    {
        printf("fourteen ");

        number -= .14;
    }
    if( number >= .13 )
    {
        printf("thirteen ");

        number -= .13;
    }
    if( number >= .12 )
    {
        printf("twelve ");

        number -= .12;
    }
    if( number >= .11 )
    {
        printf("eleven ");

        number -= .11;
    }
    if( number >= .10 )
    {
        printf("ten ");

        number -= .10;
    }
    if( number >= .09 )
    {
        printf("nine ");

        number -= .09;
    }
    if( number >= .08 )
    {
        printf("eight ");

        number -= .08;
    }
    if( number >= .07 )
    {
        printf("seven ");

        number -= .07;
    }
    if( number >= .06 )
    {
        printf("six ");

        number -= .06;
    }
    if( number >= .05 )
    {
        printf("five ");

        number -= .05;
    }
    if( number >= .04 )
    {
        printf("four ");

        number -= .04;
    }
    if( number >= .03 )
    {
        printf("three ");

        number -= .03;
    }
    if( number >= .02 )
    {
        printf("two ");

        number -= .02;
    }
    if( number >= .01 )
    {
        printf("one ");

        number -= .01;
    }

    printf("cents\n");


    return 0;
}

// output:

enter a dollar.cents amount: 4500.00
four thousand five hundred dollars and cents

enter a dollar.cents amount: 1023.88
one thousand twenty three dollars and eighty eight cents


The requirements were:

Quote:
Originally Posted by Godzilla
The person is supposed to enter a number like $1000.50, and it is supposed to display it like "one thousand dollars and 50 cents" as a string.

Notice the "50" cents and not "fifty" cents. Also, your code doesn't handle "no" cents properly.

You've got the right idea, but you're going about it rather bluntly. Try creating an array of strings for your textual numbers and an array of integers for your "value amounts." EG:

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

const static int values[] = { 10000, 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000,
                              900, 800, 700, 600, 500, 400, 300, 200, 100,
                              90, 80, 70, 60, 50, 40, 30, 20,
                              19, 18, 17, 16, 15, 14, 13, 12, 11,
                              10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

const static std::string numbers[] = { "Ten Thousand", "Nine Thousand", "Eight Thousand", "Seven Thousand",
                                       "Six Thousand", "Five Thousand", "Four Thousand", "Three Thousand",
                                       "Two Thousand", "One Thousand", "Nine Hundred", "Eight Hundred",
                                       "Seven Hundred", "Six Hundred", "Five Hundred", "Four Hundred",
                                       "Three Hundred", "Two Hundred", "One Hundred", "Ninety", "Eighty",
                                       "Seventy", "Sixty", "Fifty", "Forty", "Thirty", "Twenty",
                                       "Nineteen", "Eighteen", "Seventeen", "Sixteen", "Fifteen",
                                       "Fourteen", "Thirteen", "Twelve", "Eleven", "Ten", "Nine", "Eight",
                                       "Seven", "Six", "Five", "Four", "Three", "Two", "One" };


int main( void )
{
    cout << "Enter a dollar.cents amount: ";
    float amount = 0.0F;
    cin >> amount;

    int dollars = (int)amount;
    int cents = (int)((amount - dollars) * 100.101);
    int saved = dollars;

    std::string answer = "";
    int index = 0;
    while( dollars > 0 )
    {
        if( dollars >= values[index] )
        {
            answer += numbers[index];
            answer += " ";
            dollars -= values[index];
        }
        ++index;
    }
    if( saved == 0 && cents > 1 )
    {
        cout << saved << " dollars and " << ((cents > 0)?(cents):(0)) << " cents" << endl;
    }
    if( saved == 0 && cents == 0 )
    {
        cout << "Why are we writing a check for no amount?" << endl;
    }
    if( saved == 0 && cents == 1 )
    {
        cout << saved << " dollars and " << cents << " cent" << endl;
    }
    if( saved > 1 && cents > 1 )
    {
        cout << answer << "dollars and " << ((cents > 0)?(cents):(0)) << " cents" << endl;
    }
    if( saved > 1 && cents == 1 )
    {
        cout << answer << "dollars and " << cents << " cent" << endl;
    }
    if( saved > 1 && cents == 0 )
    {
        cout << answer << "dollars and " << 0 << " cents" << endl;
    }
    if( saved == 1 && cents > 1 )
    {
        cout << answer << "dollar and " << ((cents > 0)?(cents):(0)) << " cents" << endl;
    }
    if( saved == 1 && cents == 1 )
    {
        cout << answer << "dollar and " << cents << " cent" << endl;
    }

    return 0;
}


Output:
Code:
Enter a dollar.cents amount: 9999.99 Nine Thousand Nine Hundred Ninety Nine dollars and 99 cents Enter a dollar.cents amount: 10000.00 Ten Thousand dollars and 0 cents Enter a dollar.cents amount: 0.01 0 dollars and 1 cent Enter a dollar.cents amount: 1234.56 One Thousand Two Hundred Thirty Four dollars and 56 cents Enter a dollar.cents amount: 0 Why are we writing a check for no amount? Enter a dollar.cents amount: 8888.88 Eight Thousand Eight Hundred Eighty Eight dollars and 88 cents Enter a dollar.cents amount: 1111.11 One Thousand One Hundred Eleven dollars and 11 cents

Also, you'll want to convert the incoming dollar.cents value into a pair of integers, one for the dollar amount and another for the cents amount since you need to treat dollars in text and cents as the numerical value in order to properly meet the requirements.

Of course, there is a much more elegant solution using units, but that is for someone with more time to spend on it....


...the same basic thing is possible using char* (C strings) and maintaining a buffer and strcat'ing the char*s into the buffer, but I was too lazy and used "C++ strings."


:davis:
  #4  
Old 30-Mar-2006, 13:10
kmoffett kmoffett is offline
New Member
 
Join Date: Feb 2006
Posts: 9
kmoffett is on a distinguished road

Re: Converting a number amount to text


I had to do the same project. This is what I came up with. It is a lengthy program, but it works.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>

using namespace std;

void PrintCheckProtectedFormat(double amount);
void PrintTextFormat(double amount);

int main()
{
    double amount;

    // read numbers until user elects to quit
    while(true)
    {
        cout << "Enter amount of money (Enter 0 to quit application):" << endl;
        cin >> amount;

        // convert to a string to check length
        char convert[18+1];
        sprintf(convert, "%.2f", amount);

        if(strlen(convert) > 11)
        {
            cout << "The number your entered is too large -- "
                << "10 digits plus a decimal max" << endl << endl;
            
            // throw away the contents of the cin buffer
            cin.bad();
            continue;
        }

        if(amount == 0.0)
            break;

        PrintCheckProtectedFormat(amount);
        PrintTextFormat(amount);
    }

    return 0;
}

void PrintCheckProtectedFormat(double amount)
{
	cout << fixed << setprecision (2);
    cout << setfill('*') << setw(11) << amount << endl;
	
}


void PrintTextFormat(double amount)
{
    char ones[][6] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", 
"Seven", "Eight", "Nine"};
    char tens[][8] = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", 
"Seventy", "Eighty", "Ninety"};
    
    char teens[][10] = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", 
"Fifteen",
                        "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    
    // if the user enters 11 digits but fails to enter a decimal
    // (no cents), we will have a dollar amount in the billions
    
    // Note that we will never print "Ten Million," but we need it for a 
	//place holder
    char place[][17] = {"Zero", "One", "Ten", "Hundred", "Thousand", 
"Thousand"/*Ten Thousand*/,
                        "Hundred"/*Hundred Thousand*/, "Million", 
"Million"/*Ten Million*/,
                        "Hundred"/*Hundred Million*/, "Billion"};
    
    char buf[200] = {0};
    char conversion[11+1] = {0}; // add one extra than needed for the null
    char wholeDollarAmount[11+1] = {0};
    char cents[2+1] = {0};
    char decimal = '.';
    char charDigit[11+1] = {0};
    int intDigit;
    char* pdecimal = '\0'; // once initialized, this pointer will point to 
	//decimal, if one exists
    int position = 0;

    // convert double to a string
    sprintf(conversion, "%.2f", amount);

    // obtain the length of the string
	// int lengthOfConversionString = strlen(conversion);

    // we cannot assume that the user entered a decimal
    pdecimal = strchr(conversion, decimal);

    // extract the whole dollar amount of the converted string
    memcpy(wholeDollarAmount, conversion, strlen(conversion) - 
strlen(pdecimal));

    if(pdecimal != '\0') // if pointer is not equal to null
    {
        pdecimal++; // increment pointer to point to the first digit past the 
					//decimal
        memcpy(cents, pdecimal, strlen(pdecimal));
    }

    // format our working buffer (buf) starting at the left most digit of 
	// wholeDollarAmount
    position = strlen(wholeDollarAmount);

    for(int i = 0; i < strlen(wholeDollarAmount); i++)
    {
        memset(charDigit, '\0', sizeof(charDigit)); // reinitialize to null
        charDigit[0] = wholeDollarAmount[i];
        intDigit = atoi(charDigit);

        // what place (or position) is this digit?  ones?  tens? hundreds? 
		//thousands?

        switch(position)
        {
        case 11:
        // case represents tens of billions (user entered no decimal 
		//point)
            break;
        case 10:
            // case represents billions
            break;
        case 9:
            // case represents hundreds of millions
            if(intDigit == 0)
            {
                // if intDigit == 0, represents tens of thousands
                charDigit[0] = wholeDollarAmount[i];
                charDigit[1] = wholeDollarAmount[++i];
                intDigit = atoi(charDigit);
                strcat(buf, tens[intDigit-1]);
                strncat(buf, " ", 1);
                position -= 2;
                break;
            }
            strcat(buf, ones[intDigit]);
            strncat(buf, " ", 1);
            strcat(buf, place[position]);
            strncat(buf, " ", 1);
            position--;
            break;
        case 8:
            // case represents tens of millions
            if(intDigit == 0)
            {
                // less than 10 million
                position--;
                break;
            }
            else
                if(intDigit == 1)
                {
                    charDigit[0] = wholeDollarAmount[i];
                    charDigit[1] = wholeDollarAmount[++i];
                    intDigit = atoi(charDigit);
                    strcat(buf, teens[intDigit-10]);
                    strncat(buf, " ", 1);
                    strcat(buf, place[position]);
                    position--;
                }
                else
                    strcat(buf, tens[intDigit-1]);
            strncat(buf, " ", 1);
            position--;
            break;
        case 7:
            // case represents millions
            strcat(buf, ones[intDigit]);
            strncat(buf, " ", 1);
            strcat(buf, place[position]);
            strncat(buf, " ", 1);
            position--;
            break;
        case 6:
            // case represents hundreds of thousands
            if(intDigit == 0)
            {
                // if intDigit == 0, represents tens of thousands
                charDigit[0] = wholeDollarAmount[i];
                charDigit[1] = wholeDollarAmount[++i];
                intDigit = atoi(charDigit);
                strcat(buf, tens[intDigit-1]);
                strncat(buf, " ", 1);
                position -= 2;
                break;
            }
            strcat(buf, ones[intDigit]);
            strncat(buf, " ", 1);
            strcat(buf, place[position]);
            strncat(buf, " ", 1);
            position--;
            break;
        case 5:
            // case represents tens of thousands
            if(intDigit == 1)
            {
                charDigit[0] = wholeDollarAmount[i];
                charDigit[1] = wholeDollarAmount[++i];
                intDigit = atoi(charDigit);
                strcat(buf, teens[intDigit-10]);
                strncat(buf, " ", 1);
                strcat(buf, place[position]);
                position--;
            }
            else
                strcat(buf, tens[intDigit-1]);
            strncat(buf, " ", 1);
            position--;
            break;
        case 4:
            // case represents thousands
            if(intDigit == 0)
            {
                strcat(buf, place[position]);
                strncat(buf, " ", 1);
                position--;
                break;
            }
            strcat(buf, ones[intDigit]);
            strncat(buf, " ", 1);
            strcat(buf, place[position]);
            strncat(buf, " ", 1);
            position--;
            break;
        case 3:
            // case represents hundreds
            if(intDigit != 0)
            {
                strcat(buf, ones[intDigit]);
                strncat(buf, " ", 1);
                strcat(buf, place[position]);
                strncat(buf, " ", 1);
            }
            position--;
            break;
        case 2:
            // case represents tens
            if(intDigit == 0)
            {
                // less than 10
                position--;
                break;
            }
            else
                if(intDigit == 1)
                {
                    // in the teens, but less than 20
                    charDigit[0] = wholeDollarAmount[i];
                    charDigit[1] = wholeDollarAmount[++i];
                    intDigit = atoi(charDigit);
                    strcat(buf, teens[intDigit-10]);
                    strncat(buf, " ", 1);
                    position--;
                }
                else
                    strcat(buf, tens[intDigit-1]);

            strncat(buf, " ", 1);
            position--;
            break;
        case 1:
            // case represents ones
            // ensure this digit is not a zero
            if(intDigit != 0)
            {
                strcat(buf, ones[intDigit]);
                strncat(buf, " ", 1); // add a space between each word
                position--;
                break;
            }
        };
    }

    // user may have only entered cents (ex: .35) and no whole dollars
    // if user entered whole dollars, add the string "Dollars and "
    if((strlen(wholeDollarAmount) > 0) && (strlen(buf) > 0))
        strncat(buf, "Dollars and ", strlen("Dollars and "));

    // add the cents, if any
    if(strlen(cents) > 0)
    {
        strncat(buf, cents, strlen(cents));
        strncat(buf, "/100", strlen("/100"));
    }

    cout << buf << endl << endl;
}
  #5  
Old 30-Mar-2006, 14:27
davis
 
Posts: n/a

Re: Converting a number amount to text


Quote:
Originally Posted by kmoffett
I had to do the same project. This is what I came up with. It is a lengthy program, but it works.

Code:
Enter amount of money (Enter 0 to quit application): 29.01 ******29.01 Twenty Nine Dollars and 01/100

Your output should probably be:

Twenty Nine and 01/100 Dollars.

Look at what happens in this case:

Code:
Enter amount of money (Enter 0 to quit application): .01 *******0.01 01/100

...there are no "units" being disclosed. We've got 1/100 of something unspecified.


:davis:
  #6  
Old 31-Mar-2006, 11:38
cjavac#c++_vbP cjavac#c++_vbP is offline
Junior Member
 
Join Date: Mar 2006
Location: Miami, FL
Posts: 42
cjavac#c++_vbP is on a distinguished road

Re: Converting a number amount to text


nice solution david
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My program can run,but warning were display on Vc++ fwongmc C Programming Language 5 08-Dec-2004 10:15
Revised--C/Pointers problem fwongmc C Programming Language 9 06-Dec-2004 11:25
C Pointer problem fwongmc C Programming Language 8 04-Dec-2004 12:34
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.