1 Replies - 10360 Views - Last Post: 24 July 2009 - 11:19 AM Rate Topic: -----

#1 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

C++ program to convert number to words using classes

Posted 24 July 2009 - 09:33 AM

I need help converting numbers to words using a class. The program is meant to only format numbers within the range 0 to 9999.

I have three static arrays of strings that specify how to translate each number value into the desired format. However I am stumped as to how I would complete the rest of the program. When I searched the forums, I wasn't able to find any such posts using classes.

I need to create a constructor that accepts a nonnegative integer and uses it to initialize the Numbers object. I also need a member function for example print() which obviously prints the English Description.

#include <iostream>
using namespace std;

class Numbers
{
  private:
	int number;
  public:
	char lessThan20[20][25] = {"zero", "one", "two", "three", "four", "five",
	"six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
	"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
	char hundred[] = "hundred";
	char thousand[] = "thousand";
};
int main()
{
	int number;
	//Ask user for number input
	cout << "Enter a number between 0 and 9999: ";
	cin >> number;
	
	while (number < 0)
	{
	  cout << "This program does not accept negative numbers." << endl;
	  cout << "Enter a number between 0 and 9999: ";
	  cin >> number;
	}
	
	while (number > 9999)
	{
	  cout << "This program does no accept numbers greater than 9999." << endl;
	  cout << "Enter a number between 0 and 9999: ";
	  cin >> number;
	}
	
	
	system("PAUSE");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ program to convert number to words using classes

#2 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: C++ program to convert number to words using classes

Posted 24 July 2009 - 11:19 AM

Common homework problem.

first of all, you are missing your 20's to 90's
And you only need a string array, not a matrix!

char *numbers[] = {
   "zero", "one", "two", "three", "four", "five",
   "six", "seven", "eight", "nine", "ten", 
   "eleven", "twelve", "thirteen", "fourteen", "fifteen",
	"sixteen", "seventeen", "eighteen", "nineteen",
   "twenty", "thirty", "fourty", "fifty", "sixty",
   "seventy", "eighty", "ninety" };



Why have the burdeon of negative errors!
char szBuf[ 256 ], *p;

p = szBuf;
if (number < 0)
{
  p += sprintf( p, "Negative " );
  number = -number;
}



then design for the future!
Work off the big numbers

int hi = num / 100;	{0...9}
int lo = num % 100;

if (0 != hi)		// Meaning 100 or more
{
   p += sprintf( p, hundred );
}

num = lo;   // {0...99} remain



Now your threshold is >= 20
Use the same hi /lo hi is tens, lo = ones through 19.
don't forget your hyphent
and ignore tens if 0, or 1!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1