2 Replies - 2226 Views - Last Post: 23 July 2009 - 08:17 PM Rate Topic: -----

#1 agiles2303  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-February 09

Help with Numbers to Words using classes

Posted 25 March 2009 - 09:54 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: Help with Numbers to Words using classes

#2 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: Help with Numbers to Words using classes

Posted 25 March 2009 - 10:18 AM

hmm, a couple of things
you need to create the functions normally and put them under the public section of the class
e.g.
public: 

void print(int num);


Was This Post Helpful? 0
  • +
  • -

#3 rahul89  Icon User is offline

  • New D.I.C Head

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

Re: Help with Numbers to Words using classes

Posted 23 July 2009 - 08:17 PM

View Postagiles2303, on 25 Mar, 2009 - 08:54 AM, said:

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;
}


hey dude do u have d working program.......i need d same thing if u can help me out i would appreciate it.....thanks a lot
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1