6 Replies - 4865 Views - Last Post: 22 June 2012 - 12:31 AM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 195
  • Joined: 25-March 10

Convert number symbols to words

Posted 19 June 2012 - 04:15 PM

I was able to get the program to compile and run, but unfortunately I can't come up with an algorithm, to deal with numbers greater than 19. I was wondering if my pseudo code is on the right track. Basically I'm wondering how I can get C++ to read/detect, the number at a certain digit position? For example 456, I'd get the program to detect the number at the 3rd position which is six. If the program was 419, I would have the program focus on the last two digits, and have it display 19..


I don't mind someone linking to a long online tutorial in regards to this.

/*
917
1. Numbers Class 
Design a class Numbers that can be used to translate whole dollar amounts 
in the range 0 through 9999 into an English description of the number. 
For example, the number 713 would be translated into the string seven hundred thirteen,
and 8203 would be translated into eight thousand two hundred three. 
*/

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

class Numbers
{
private:
	int number;

	static const string lessThan20[20];
	static const string greaterThan19[8];
	static const string hundred;
	static const string thousand;

public:
	//cosntructor
	Numbers();
	Numbers(int num)
	{
		setNumber(num);
	}
	
	void setNumber(int num) 
		{number = num;}

	void print();
	
};

// Static member variables must be defined outside of the class
const string Numbers::lessThan20[20] =	{"zero", "one" , "two", "three", "four", "five",
										"six", "seven", "eight", "nine", "ten", "eleven",
										"twelve", "thirteen", "fourteen", "fifteen", 
										"sixteen", "seventeen", "eighteen", "nineteen"};

const string Numbers::greaterThan19[8] =	{"twenty", "thirty", "forty", "fifty", "sixty", 
											"seventy", "eighty", "ninety"};

const string Numbers::hundred = "hundred";
const string Numbers::thousand = "thousand";

//print function
void Numbers::print()
{
	if (number >= 0 && number < 20)  //executes pefectly
	{
		cout << lessThan20[number]<< " dollars";
	}
	else if (number >= 20 && number < 100) //2 digit numbers, > 20
	{
		
		//check 1st digit of 2 digit number, and assign it int firstNum
		//check 2nd digit & assign it to, int secondNum
		
		//cout <<  greaterThan19[firstNum] << lessThan20[secondNum]<< " dollar";
	}
	else if (number >= 100 && number < 1000) //3 digit numbers, < 1000
	{
		//if statement checks if last 2 digits are less than 20
			//check 1st digit of 3 digit number, and assign it int firstNum
			//check 2nd & 3rd digit, assign it to, int secondNum
			//cout << greaterThan19[firstNum] << " " << lessThan20[secondNum]<< " dollar";

		//else works with last 2 digits greater than 19
			//check 1st digit of 3 digit number, and assign it int firstNum
			//check 2nd digit, assign it to, int secondNum
			//check 3rd digit & assign it to, int thirdNum
			//cout << greaterThan19[firstNum] << " " << lessThan20[secondNum] << " " <<  lessThan20thirdNum] << dollar";
	}
	else //(number >= 1000 && number < 10000)  //4 digit numbers, > 20
	{
		//cout << lessThan20[number]<< " dollar";
	}
}


int main()
{
	int input;
	//Numbers numObject;

	cout << "Enter a dollar amount from 0 to 9999: ";
	cin >> input;
	
	while (input >= 0 && input <= 9999)
	{
		Numbers numObject(input);	//pass user input to constructor
		numObject.print();
		
		cout << endl << endl;

		cout << "Enter another amount, or a value less than" << endl;
		cout << "0 or greater than 9999 to quit: ";
		cin >> input;
	}

	cout << "You're finished";
	cout << endl;

	system("pause");
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Convert number symbols to words

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1397
  • View blog
  • Posts: 4,872
  • Joined: 19-February 09

Re: Convert number symbols to words

Posted 19 June 2012 - 08:15 PM

419 divided by 100 = 4, using integer division.

419 - (4 * 100) = 19.
Was This Post Helpful? 1
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Convert number symbols to words

Posted 19 June 2012 - 11:23 PM

I don't know how to do this either butI think you are on the right track.
Have you considersd enumerations and tokens?
If you must use a class then read no further.
It should be possible to put the individual digits in an array.
Find the size of the array.If 4 you know you are dealing with thousands,hundreds,tens and units. If size = 3 you are dealing with only
hundreds,tens and units (except where you may want to use and for 0).
One enum might take care of at least thousand and hundreds eg
enum thou_hun{and=0,one,two,three,.....etc.(to 9)};
The second enum would need to take care of eleven ---> nineteen.
Good luck.
Was This Post Helpful? 0
  • +
  • -

#4 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Convert number symbols to words

Posted 20 June 2012 - 07:52 AM

	else if (number >= 20 && number < 100) //2 digit numbers, > 20
	{

    int n =  (number / 10) % 10; // pulls off the tens place
    n -= 2;

		cout <<  greaterThan19[n] << endl;//lessThan20[secondNum]<< " dollar";

    int secNum = (number % 10 ); // saves the ones digit into secNum
    cout << secNum << endl;
        cout << lessThan20[secNum ] << endl;


You need to do what is done with n and secNum for the Hundreds and Thousands as well. I don't think it is a great idea to index into an array with the number you pulled from input but, it will work if you pay attention to how you setup your array. ie... notice I subtracted 2 from n.
Was This Post Helpful? -1
  • +
  • -

#5 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 195
  • Joined: 25-March 10

Re: Convert number symbols to words

Posted 20 June 2012 - 08:21 AM

View Post#define, on 19 June 2012 - 08:15 PM, said:

419 divided by 100 = 4, using integer division.

419 - (4 * 100) = 19.


Thanks. This helped alot. Unfortunately after getting 2 digit values to display, I decided, to implement a for loop, because this what a profressor would most likely require.

Typing a value like 21, the program will refuse to display any text about it. Typing a 2 digit number in the 30s and 70s range, the program displays, the entire array after the corresponding index.

The following tries to use a for loop.
/*
917
1. Numbers Class 
Design a class Numbers that can be used to translate whole dollar amounts 
in the range 0 through 9999 into an English description of the number. 
For example, the number 713 would be translated into the string seven hundred thirteen,
and 8203 would be translated into eight thousand two hundred three. 
*/

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

class Numbers
{
private:
	int number;

	static const string lessThan20[20];
	static const string greaterThan19[8];
	static const string hundred;
	static const string thousand;

public:
	//cosntructor
	Numbers();
	Numbers(int num)
	{
		setNumber(num);
	}
	
	void setNumber(int num) 
		{number = num;}

	void print();
	
};

// Static member variables must be defined outside of the class
const string Numbers::lessThan20[20] =	{"zero", "one" , "two", "three", "four", "five",
										"six", "seven", "eight", "nine", "ten", "eleven",
										"twelve", "thirteen", "fourteen", "fifteen", 
										"sixteen", "seventeen", "eighteen", "nineteen"};

const string Numbers::greaterThan19[8] =	{"twenty", "thirty", "forty", "fifty", "sixty", 
											"seventy", "eighty", "ninety"};

const string Numbers::hundred = "hundred";
const string Numbers::thousand = "thousand";

//print function
void Numbers::print()
{
	int firstNum;
	int secondNum;
	

	if (number >= 0 && number < 20)  //executes pefectly
	{
		cout << lessThan20[number]<< " dollars";
	}
	else if (number >= 20 && number < 100) //2 digit numbers, > 20 & < 100
	{
		firstNum = (number / 10) - 1;
		//firstNum -= 1;	//to match up with the array index

		for (int index = 0; index < 8; index++)
		{
			if(firstNum == index) 
			{
				firstNum += 1;
				secondNum = number - (firstNum * 10);

				if (secondNum == 0)
					cout <<  greaterThan19[index] << " dollars";	
				else 
					cout <<  greaterThan19[index] << " " << lessThan20[secondNum] << " dollars";
			}
			else	
				index++;
		}

		//check 1st digit of 2 digit number, and assign it int firstNum
		//check 2nd digit & assign it to, int secondNum
		
		//cout <<  greaterThan19[firstNum] << lessThan20[secondNum]<< " dollar";
	}
	else if (number >= 100 && number < 1000) //3 digit numbers, < 1000
	{
		//if statement checks if last 2 digits are less than 20
			//check 1st digit of 3 digit number, and assign it int firstNum
			//check 2nd & 3rd digit, assign it to, int secondNum
			//cout << greaterThan19[firstNum] << " " << lessThan20[secondNum]<< " dollar";

		//else works with last 2 digits greater than 19
			//check 1st digit of 3 digit number, and assign it int firstNum
			//check 2nd digit, assign it to, int secondNum
			//check 3rd digit & assign it to, int thirdNum
			//cout << greaterThan19[firstNum] << " " << lessThan20[secondNum] << " " <<  lessThan20thirdNum] << dollar";
	}
	else //(number >= 1000 && number < 10000)  //4 digit numbers, > 20
	{
		//cout << lessThan20[number]<< " dollar";
	}
}


int main()
{
	int input;
	//Numbers numObject;

	cout << "Enter a dollar amount from 0 to 9999: ";
	cin >> input;
	
	while (input >= 0 && input <= 9999)
	{
		Numbers numObject(input);	//pass user input to constructor
		numObject.print();
		
		cout << endl << endl;

		cout << "Enter another amount, or a value less than" << endl;
		cout << "0 or greater than 9999 to quit: ";
		cin >> input;
	}

	cout << "You're finished";
	cout << endl;

	system("pause");
	return 0;
}



The following works, but as you can see, not as efficient.
/*
917
1. Numbers Class 
Design a class Numbers that can be used to translate whole dollar amounts 
in the range 0 through 9999 into an English description of the number. 
For example, the number 713 would be translated into the string seven hundred thirteen,
and 8203 would be translated into eight thousand two hundred three. 
*/

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

class Numbers
{
private:
	int number;

	static const string lessThan20[20];
	static const string greaterThan19[8];
	static const string hundred;
	static const string thousand;

public:
	//cosntructor
	Numbers();
	Numbers(int num)
	{
		setNumber(num);
	}
	
	void setNumber(int num) 
		{number = num;}

	void print();
	
};

// Static member variables must be defined outside of the class
const string Numbers::lessThan20[20] =	{"zero", "one" , "two", "three", "four", "five",
										"six", "seven", "eight", "nine", "ten", "eleven",
										"twelve", "thirteen", "fourteen", "fifteen", 
										"sixteen", "seventeen", "eighteen", "nineteen"};

const string Numbers::greaterThan19[8] =	{"twenty", "thirty", "forty", "fifty", "sixty", 
											"seventy", "eighty", "ninety"};

const string Numbers::hundred = "hundred";
const string Numbers::thousand = "thousand";

//print function
void Numbers::print()
{
	int firstNum;
	int secondNum;
	

	if (number >= 0 && number < 20)  //executes pefectly
	{
		cout << lessThan20[number]<< " dollars";
	}
	else if (number >= 20 && number < 100) //2 digit numbers, > 20
	{
		if (number / 10 == 2)
		{
			secondNum = number - (2 * 10);
			cout <<  greaterThan19[0] << " " << lessThan20[secondNum] << " dollars";
		}
		else if (number / 10 == 3)
		{
			secondNum = number - (3 * 10);
			cout <<  greaterThan19[1] << " " << lessThan20[secondNum] << " dollars";
		}
		else if (number / 10 == 4)
		{
			secondNum = number - (4 * 10);
			cout <<  greaterThan19[2] << " " << lessThan20[secondNum] << " dollars";
		}
		else if (number / 10 == 5)
		{
			secondNum = number - (5 * 10);
			cout <<  greaterThan19[3] << " " << lessThan20[secondNum] << " dollars";
		}
		else if (number / 10 == 6)
		{
			secondNum = number - (6 * 10);
			cout <<  greaterThan19[4] << " " << lessThan20[secondNum] << " dollars";
		}
		else if (number / 10 == 7)
		{
			secondNum = number - (7 * 10);
			cout <<  greaterThan19[5] << " " << lessThan20[secondNum] << " dollars";
		}
		else if (number / 10 == 8)
		{
			secondNum = number - (8 * 10);
			cout <<  greaterThan19[6] << " " << lessThan20[secondNum] << " dollars";
		}
		else
		{
			secondNum = number - (9 * 10);
			cout <<  greaterThan19[7] << " " << lessThan20[secondNum] << " dollars";
		}

		//check 1st digit of 2 digit number, and assign it int firstNum
		//check 2nd digit & assign it to, int secondNum
		
		//cout <<  greaterThan19[firstNum] << lessThan20[secondNum]<< " dollar";
	}
	else if (number >= 100 && number < 1000) //3 digit numbers, < 1000
	{
		//if statement checks if last 2 digits are less than 20
			//check 1st digit of 3 digit number, and assign it int firstNum
			//check 2nd & 3rd digit, assign it to, int secondNum
			//cout << greaterThan19[firstNum] << " " << lessThan20[secondNum]<< " dollar";

		//else works with last 2 digits greater than 19
			//check 1st digit of 3 digit number, and assign it int firstNum
			//check 2nd digit, assign it to, int secondNum
			//check 3rd digit & assign it to, int thirdNum
			//cout << greaterThan19[firstNum] << " " << lessThan20[secondNum] << " " <<  lessThan20thirdNum] << dollar";
	}
	else //(number >= 1000 && number < 10000)  //4 digit numbers, > 20
	{
		//cout << lessThan20[number]<< " dollar";
	}
}


int main()
{
	int input;
	//Numbers numObject;

	cout << "Enter a dollar amount from 0 to 9999: ";
	cin >> input;
	
	while (input >= 0 && input <= 9999)
	{
		Numbers numObject(input);	//pass user input to constructor
		numObject.print();
		
		cout << endl << endl;

		cout << "Enter another amount, or a value less than" << endl;
		cout << "0 or greater than 9999 to quit: ";
		cin >> input;
	}

	cout << "You're finished";
	cout << endl;

	system("pause");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5902
  • View blog
  • Posts: 12,807
  • Joined: 16-October 07

Re: Convert number symbols to words

Posted 20 June 2012 - 08:41 AM

Just looking at this:
} else if (number >= 20 && number < 100) {
	if (number / 10 == 2)
	{
		secondNum = number - (2 * 10);
		cout <<  greaterThan19[0] << " " << lessThan20[secondNum] << " dollars";
	}
	else if (number / 10 == 3)
	{
		secondNum = number - (3 * 10);
		cout <<  greaterThan19[1] << " " << lessThan20[secondNum] << " dollars";
	}
	else if (number / 10 == 4)
	{
		secondNum = number - (4 * 10);
		cout <<  greaterThan19[2] << " " << lessThan20[secondNum] << " dollars";
	}
	else if (number / 10 == 5)
	{
		secondNum = number - (5 * 10);
		cout <<  greaterThan19[3] << " " << lessThan20[secondNum] << " dollars";
	}
	else if (number / 10 == 6)
	{
		secondNum = number - (6 * 10);
		cout <<  greaterThan19[4] << " " << lessThan20[secondNum] << " dollars";
	}
	else if (number / 10 == 7)
	{
		secondNum = number - (7 * 10);
		cout <<  greaterThan19[5] << " " << lessThan20[secondNum] << " dollars";
	}
	else if (number / 10 == 8)
	{
		secondNum = number - (8 * 10);
		cout <<  greaterThan19[6] << " " << lessThan20[secondNum] << " dollars";
	}
	else
	{
		secondNum = number - (9 * 10);
		cout <<  greaterThan19[7] << " " << lessThan20[secondNum] << " dollars";
	}



Can't you just see the massive repetition here?

Perhaps:
} else if (number >= 20 && number < 100) {
	int tens = number / 10;
	int secondNum = number - (tens * 10);
	if (tens==0) { 
		// you need to allow for this
		// you currently don't
	} else {
		cout <<  greaterThan19[tens-2] << " " << lessThan20[secondNum] << " dollars";
	}



Even without a loop. Perhaps if you just figured out the four variables to work from, thousands, hundreds, tens, ones, then you'd be on your way?
Was This Post Helpful? 1
  • +
  • -

#7 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Convert number symbols to words

Posted 22 June 2012 - 12:31 AM

This may give you a means of checking your work. Its not thoroughly tested and certainly not bullet proof but appears to work on the four digit values so far input
#include<iostream>
#include<string>

using namespace std;

int main()
{
string s1[]={" ","one","two","three","four","five","six",
             "seven","eight","nine"};
string s2[]={" ","eleven","twelve","thirteen","fourteen","fifteen",
             "sixteen","seventeen","eighteen","nineteen"};
string s3[]= {"","ten","twenty","thirty","fourty","fifty","sixty","seventy",
              "eighty","ninety"};
int a[4]={0};
int num=0;
std::cout<<"\n\nThis program converts whole numbers to words\n";
std::cout<<"enter four digits with spaces between ";

for(int i=0;i<4;i++){
    num=0;
    std::cin>>num;
    a[i]=num;
   }
    std::cout<<s1[a[0]]<<" thousand";//write thousands value 1-9
    if(a[1]==0 && a[2]==0 && a[3]==0)return 0;
    if(a[1]==0 && a[2]>0 && a[3]>0)std::cout<<" and ";//iff no hundreds write "and"
    if(a[1]>0)std::cout<<' '<<s1[a[1]]<<" hundred";//write hundreds value
    if(a[1]>0)std::cout<<" and ";
    if(a[2]==1)std::cout<<s2[a[3]];
    if(a[2]>1)std::cout<<s3[a[2]]<<s1[a[3] ];//4735
    if(a[1]==0 && a[2]==0)std::cout<<" and "<<s1[a[3] ];//4006
    if(a[1]==0 && a[2]==1)std::cout<<s2[a[3]];//4013



return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1