numbers into words

converting the number 2 into the word two for a check writing program

Page 1 of 1

8 Replies - 3521 Views - Last Post: 14 February 2010 - 02:50 PM Rate Topic: -----

#1 jessicalegner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

numbers into words

Posted 14 February 2010 - 08:47 AM

I have a file with a persons name, date, and check amount. My job is to simulate a check writing program. I have most of it down, the only problem is getting $596.17 to read Five Hundred Ninety Six and 17/100 dollars. Can anyone help?

I am thinking that I should preform this in the for loop before I write each check. Or should I do it inside the write() function?


/*
CST 280 Program Assignment 3
Purpose---
INPUT:		The input will come from file checkdata.txt that contains the starting check number, the employees name, the date, and the amount.  There is no user interaction.
PROCESS:	This program will read and process the data from the file, generate a english version of the check amount, and write the result to the console.
OUTPUT:		A list of simulated checks will be output to the console.
On: 		02/12/2010
*/

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

#include "check.h"

// Global Variable
const int MAX_EMPLOYEES = 500;

int main()
{
	Check ckList[MAX_EMPLOYEES];					// Array of objects
	
	int checkNum;
	double amount;
	string firstName, lastName, date;

	int ptr, size;

	// read check info from file and build array values
	ifstream check_File("checkdata.txt");

	ptr = 0;	
	check_File >> checkNum >> firstName >> lastName >> date >> amount;
	//ckList[ptr].setCheckNumber(checkNum);// capture check number only one time
	while(!check_File.eof())
	{
		// Store information from file in current array object
		ckList[ptr].setCheckNumber(checkNum);
		ckList[ptr].setCheckFName(firstName);
		ckList[ptr].setCheckLName(lastName);
		ckList[ptr].setCheckDate(date);
		ckList[ptr].setCheckAmount(amount);

		check_File >> firstName >> lastName >> date >> amount;
		ptr++;
		checkNum++; // add 1 to the check number to be stored in each object

	}
	size = ptr;

	// Check writing loop
	for (int i=0; i < size; i++)
	{
		// Get long format of number
		ckList[i].write();
	}

	return 0;
}


// This file implements functions for the check class
#include <iostream>
#include<iomanip>
#include <string>
using namespace std;

#include "check.h"

const string CHECK_SIGNER = "DELTA COLLEGE";

// Defualt constructor - initialize to "dummy" values
Check::Check()
{
	checkNum	= 0000;
	firstName	= "XXX";
	lastName	= "ZZZ";
	date		= "00/00/00";
	amount 	 	= 0.00;
}

// Method to set check number
void Check::setCheckNumber(int ckNum)
{
	checkNum = ckNum;
}

// Method to set first name
void Check::setCheckFName(string ckFName)
{
	firstName = ckFName;
}

// Method to set last name
void Check::setCheckLName(string ckLName)
{
	lastName = ckLName;
}

// Method to set date
void Check::setCheckDate(string ckDate)
{
	date = ckDate;
}

// Method to set check amount
void Check::setCheckAmount(double ckAmt)
{
	amount = ckAmt;
}

// Method to wrtie checks to console output
// 
void Check::write()
{
	cout << "--------------------------------------------------------" << endl;
	cout << checkNum ;
	cout << setw(44) << "DATE: " << date << endl;
	cout << "PAY TO THE ORDER OF: " << firstName << " " << lastName;
	cout << setw(4) << "$ "  << amount << endl;
	cout << setw(56) << CHECK_SIGNER << endl;
}



I am having trouble uploading the file, but here is the content:

1678
Jane Doe 04/28/09 567.12
George Obama 05/01/09 811.47
Barrak Bush 10/19/10 98.14
Condi Clinton 12/31/09 1.84
Hillary Rice 07/03/10 0.56
Jen Fuller 08/25/09 112.13
Abe Washington 03/02/11 117.00
George Lincoln 09/09/09 345.99
Nick Lidstrom 11/23/09 10.00
Steve Yyzerman 11/23/09 8.87

This post has been edited by jessicalegner: 14 February 2010 - 09:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: numbers into words

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: numbers into words

Posted 14 February 2010 - 08:55 AM

Could you please post the contents of "checkdata.txt"? Also, the .eof() flag should not be used when reading from a file. Read here: http://www.dreaminco...topic145699.htm
Was This Post Helpful? 2
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: numbers into words

Posted 14 February 2010 - 09:07 AM

You shouldn't do the conversion in main. It should be done inside the Check class. But I wouldn't do it in write() either. I'd do it in a separate function which is called by the write() function.

And I think you should give it some thought and take a stab at it yourself before people start writing it for you.

How you write it will depend on the maximum amount of the checks you want to be able to handle, so you'll have to decide that at some point.
Was This Post Helpful? 0
  • +
  • -

#4 jessicalegner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

Re: numbers into words

Posted 14 February 2010 - 09:49 AM

View Postr.stiltskin, on 14 February 2010 - 08:07 AM, said:

You shouldn't do the conversion in main. It should be done inside the Check class. But I wouldn't do it in write() either. I'd do it in a separate function which is called by the write() function.

And I think you should give it some thought and take a stab at it yourself before people start writing it for you.

How you write it will depend on the maximum amount of the checks you want to be able to handle, so you'll have to decide that at some point.


Thank you. I think you are correct about the placement. As far as starting it myself: I have been tryig different things just on paper...I can't even wrap my head around it. I thought I had a good way of doing it, having the amount be a string and manipulate it that way, but that was a dead end, just like all of my other ideas. I just cant seem to figure out an efficient way of determining whether it should be hundred or not....and then the case of no dollars comes up.

Just a hint to get me started is all I am asking for. I WANT to write it myself. I just have no idea where to begin. :(
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: numbers into words

Posted 14 February 2010 - 09:56 AM

View Postjessicalegner, on 14 February 2010 - 10:49 AM, said:

I just cant seem to figure out an efficient way of determining whether it should be hundred or not....


Assume for the moment that the amount will always be less than 1000 (you can improve the function later to include >=1000, >=1000000 etc).

So ...
if( amount/100 >= 1 ) {
// ...
}

This post has been edited by r.stiltskin: 14 February 2010 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 jessicalegner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

Re: numbers into words

Posted 14 February 2010 - 10:56 AM

I don't know if this is "EFFICIENT" as I would like, but here is the development so far lol.
// This function converts a number into the english word version
void Check::numberIntoWords()
{
	if(amount / 100 >= 1) // greater than 100
	{
		// Determine the value of the hundred place
		int hundredPlace = 0;
		hundredPlace = amount / 100;
		switch(hundredPlace)
		{
			case 1: cout << "ONE HUNDRED"; 		break;
			case 2: cout << "TWO HUNDRED";  	break;
			case 3: cout << "THREE HUNDRED"; 	break;
			case 4: cout << "FOUR HUNDRED";		break;
			case 5: cout << "FIVE HUNDRED";		break;
			case 6: cout << "SIX HUNDRED";		break;
			case 7: cout << "SEVEN HUNDRED";	break;
			case 8: cout << "EIGHT HUNDRED";	break;
			case 9: cout << "NINE HUNDRED";		break;
		};
	}

}



more to come. :-)
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: numbers into words

Posted 14 February 2010 - 11:09 AM

Not bad. You're getting it.
Was This Post Helpful? 0
  • +
  • -

#8 jessicalegner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

Re: numbers into words

Posted 14 February 2010 - 12:26 PM

Okay 3 problems, I think they may be related. When the check amount is 117.00, when I write the amount, it writes it as $117. I tried set precision(2) but that gave me exponential format.

The second problem is that when I want it to write 00/100 it only places one 0 (0/100).

Problem #3..
It some of the "cents" are getting written as one less than they actually are.

I know this more than likely has something to do with int and double problems...however, I CANNOT figure out where it is for the life of me.

Any suggestions?

// This function converts a number into the english word version
void Check::numberIntoWords()
{
	// If amount is less than one dollar
	if(amount < 1)
	{
		cout << "ZERO ";
	}


	// Determine the value of the hundred place and write the result to the console
	int hundredPlace = 0;
	hundredPlace = amount / 100;
	switch(hundredPlace)
	{
		case 0: cout << ""; 				break;
		case 1: cout << "ONE HUNDRED "; 	break;
		case 2: cout << "TWO HUNDRED ";  	break;
		case 3: cout << "THREE HUNDRED "; 	break;
		case 4: cout << "FOUR HUNDRED ";	break;
		case 5: cout << "FIVE HUNDRED ";	break;
		case 6: cout << "SIX HUNDRED ";		break;
		case 7: cout << "SEVEN HUNDRED ";	break;
		case 8: cout << "EIGHT HUNDRED ";	break;
		case 9: cout << "NINE HUNDRED ";	break;
	};

	// Determine the value of the ten place and write to console
	int tenPlace = 0;
	int tenValue = (amount - hundredPlace * 100);
	tenPlace = tenValue / 10;
	int onePlace = tenValue - tenPlace * 10;
	if (tenValue >= 10 && tenValue < 20)
	{
		switch(tenValue)
		{
			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;
		};
	}
	else
	{
		switch(tenPlace)
		{
			case 2: cout << "TWENTY-";		break;
			case 3: cout << "THIRTY-";		break;
			case 4: cout << "FORTY-"; 		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;
		};

		// Determine value of one place and write to console
		switch(onePlace)
		{
			case 0: cout << "";			break;
			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;
		};
	}

	int wholeDollars;
	int cents = 00;
	wholeDollars = (hundredPlace * 100) + (tenPlace * 10) + onePlace;
	cents = (amount - wholeDollars) * 100;

	cout << " AND " << cents << "/100 DOLLARS" << endl;
}



--------------------------------------------------------
1678 DATE: 04/28/09
PAY TO THE ORDER OF: Jane Doe $ 567.12
FIVE HUNDRED SIXTY-SEVEN AND 12/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1679 DATE: 05/01/09
PAY TO THE ORDER OF: George Obama $ 811.47
EIGHT HUNDRED ELEVEN AND 47/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1680 DATE: 10/19/10
PAY TO THE ORDER OF: Barrak Bush $ 98.14
NINETY-EIGHT AND 14/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1681 DATE: 12/31/09
PAY TO THE ORDER OF: Condi Clinton $ 1.84
ONE AND 84/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1682 DATE: 07/03/10
PAY TO THE ORDER OF: Hillary Rice $ 0.56
ZERO AND 56/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1683 DATE: 08/25/09
PAY TO THE ORDER OF: Jen Fuller $ 112.13
ONE HUNDRED TWELVE AND 12/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1684 DATE: 03/02/11
PAY TO THE ORDER OF: Abe Washington $ 117
ONE HUNDRED SEVENTEEN AND 0/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1685 DATE: 09/09/09
PAY TO THE ORDER OF: George Lincoln $ 345.99
THREE HUNDRED FORTY-FIVE AND 99/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1686 DATE: 11/23/09
PAY TO THE ORDER OF: Nick Lidstrom $ 10
TEN AND 0/100 DOLLARS
DELTA COLLEGE
--------------------------------------------------------
1687 DATE: 11/23/09
PAY TO THE ORDER OF: Steve Yyzerman $ 8.87
EIGHT AND 86/100 DOLLARS
DELTA COLLEGE
Press any key to continue . . .
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: numbers into words

Posted 14 February 2010 - 02:50 PM

The double value .99 might actually be stored as .989999999994 or something like that, which is not likely to cause any problems except when you use truncation instead of rounding. When you multiply by 100 you get 98.9999999994, and when that gets cast to an int it is truncated to 98.

A simple and efficient way to correctly round numbers is simply add 0.5 and then truncate. So 98.50000000000 + 0.5 = 99.0000000000 which is truncated to 99, 98.9999999994 + 0.5 = 99.4999999994 which is also truncated to 99, while 98.4999999994 + 0.5 = 98.9999999994 which is truncated to 98.

As for the 00: test the cents value; if cents == 0, print the string (or c-string) "00". Alternatively, if your compiler's library includes an "itoa" (integer to c-string conversion) function you could use that and print all the cents as strings, but that isn't guaranteed to work with all compilers since itoa is not a standard function. Of course, you could also write your own...

(Don't be confused by that business about itoa. The simplest solution is just test for 0 and in that case print the string "00" and in all other cases print the integer cents as you are already doing.)

This post has been edited by r.stiltskin: 14 February 2010 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1