Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-20-2004, 05:15 AM   #1 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
numbers to letters??

I'm workin on a program now that outputs checks for a company, the use inputs the payee and the price (numeric value). Once I get the info its my job to format it on the check and also take the price given by the user and also spell it out, is there a way to do this because I sure as heck can't figure it out!
keystoneman is offline   Reply With Quote
Old 10-20-2004, 09:17 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
int theInt;
string theStdString("23");
char* theCStyleString = "34";
   
//First with the C-style string.
theInt=atoi(theCStyleString);
cout<<theInt<<endl;
   
//Now with std::string.
theInt=atoi(theStdString.c_str());
cout<<theInt<<endl;
__________________
Valmont is offline   Reply With Quote
Old 10-20-2004, 11:12 AM   #3 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
I'm not sure I follow the code here and what it is doing, it appears to me that what we are doing is assigning an integer 'theInt' the value of 'theCStyleString once it is used with the function atoi. The function atoi converts a C-string to an integer and returns the value, so the output of theInt is an integer. What I was looking for was to be able to have the user input lets say '12.50' then for the cpu to convert it to tweleve fifty. Is this possible? Did I miss interpert your code?
keystoneman is offline   Reply With Quote
Old 10-20-2004, 01:19 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
What? The computer needs actually to say "twelve fifty"? Oh yes, I see now. For the words on official documents and such. Sure I can do that.
__________________
Valmont is offline   Reply With Quote
Old 10-20-2004, 01:44 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Run this and enter:
1.02

Can you finish it? :)
Code:
#include <iostream>
#include <string>
#include <climits>
#include <cmath>


using namespace std;

//Optional function: depends on IDE.
void wait_for_enter();

//Additional helper.
void reset_istream();

//The model.
string Words[]={"null" ,"one", "two"};

int main(int argc, char *argv[])
{
   double theAmount;
   
   cout<<"Enter the amount please:"<<endl<<"-> ";
   cin>>theAmount;
   
   double theDollars;
   double theCents;
   theCents=modf(theAmount, &theDollars)*100;
   
   cout<<theDollars<<" "<<theCents<<endl;
   
   cout<<Words[(unsigned)theDollars]<<" : "<<Words[(unsigned)theCents]<<endl;

   reset_istream();
   wait_for_enter();
   return 0;
}

//---------------------------------------------------

void reset_istream()
{
   if(cin.eof())
   {
      cin.clear();
   }
   else
   {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
   }
}

//---------------------------------------------------

void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  string line;
  getline( cin, line);
}
The cool thing about this is that you can do it in a million ways. See if you like to study std::map and std::pair.
__________________
Valmont is offline   Reply With Quote
Old 10-20-2004, 02:15 PM   #6 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
Hmmm I ran your code and it works for 1.02 but thats it, if I try 9.6 or 2.34 it doesn't work, and further more im a little lost with this section of code
Code:
cout<<Words[(unsigned)theDollars]<<" : "<<Words[(unsigned)theCents]<<endl;
I know it has something to do with
Code:
string Words[]={"null" ,"one", "two"};
but I can't put my finger on just what it is ... if it is a one or a two it displays but if its not one of them does it return a null character??
keystoneman is offline   Reply With Quote
Old 10-20-2004, 05:11 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Hmm, I thought you'd liked to figure out how it works on your own.

The concept
Lets simulate what your program should do.
Code:
User enters:   output:
0              null
1              one
1.01           one one
1.02           one two
...            ...
50.76          fifty seventysix
30.19          thirty nineteen
21.29          twenty twentynine
And so forth.
The first 4 entries gives us a hint already on how to solve our little problem. For various reasons.
1) I approach it in terms of models. So I stop thinking and implement the model already. A "model" is the world we are talking about basically (nevermind high tech analisys).
2) There is a mathematical relationship between the numbers and my model already visible. We talk about that later.

So off I go. I build the model first:
Code:
string Words[]={"null" ,"one", "two"};
I am not going to add more words for practical reasons. But you get the drift. Later more on this.

Now the mathematical relationship:
- Every number is a (human-like) word.
- The numbers can be used as an index for my model as it is right now. Using such an index on my model will access it's corresponding word!
Let's see the concept of that:
Code:
Number   Index   Model-usage                            Yields
0       0       MyModel[0]                             "null"
1       1       MyModel[1]                             "one"
1.01    1,1     Mymodel[1] MyModel[1]                  "one" "one"
2.15    2,1,5   MyModel[2] MyModel[10] MyModel[5]      "two" "ten" "five"
And so forth.
And this is the basis for the whole thing going on.
So if you add the word "three" to the end of the array, you'll be able to enter the number 1.03. Just try it out.

Basically I am just accessing my array by using two indexes. However, array subscripts can only be of some integer type. But my indexes are of type double. So I need to convert them first. That why it looks like that.

Why did I need the doubles, not integers straight away?
Well, because of the function modf().
Modf() splits a double into a whole part and a fractional part. But modf() requires doubles as parameters. So basically I had no choice but to make my dollars/cents variables doubles as well.

Trick
If you would add all the words for all the existing numbers, then you'll die young I'm affraid.
So we need to think twice:
What if I split my model into smaller models like this...
Code:
string sSingles[]={"one","two","three","four","five","six","seven","eight","nine"};
string sTens[]={"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
string sHundreds[]={"hundred", "twohundred", "threehundred"};
And so forth.
But the sHundreds model can even be split up further, since we already defined the words "one", "two", "three" etcetera.
So this will suffice:
Code:
string sHundreds[]={"hundred"};
string sThousands[]={"thousand"};
string sMillions[]={"million"};
See?
A cool problem indeed. But quite possible to solve. Quit possible to solve it in a million ways .
See how far you can get. Such a program can end up quite complex. So start programming neat already. Make sure a stranger can actually read and understand your code. Saves me time .
__________________
Valmont is offline   Reply With Quote
Old 10-21-2004, 12:08 PM   #8 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
Val don't think i'm ignoring you, I just have alot to do but the program is almost complete I'll submit it once i'm done and you can take a look, thanks for the help again! Should be done later tonight.
keystoneman is offline   Reply With Quote
Old 10-21-2004, 03:11 PM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Ah then, we can compare our versions eyh?

__________________
Valmont is offline   Reply With Quote
Old 10-21-2004, 03:37 PM   #10 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
Go ahead and compare, I hope mine works on your machiene like it does on mine

Code:
#include <iostream>
#include <string>
#include <math.h>

using namespace std;



//The model.
string single[]={"zero", "one", "two", "three", "four", "five", "six", "seven", 
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", 
"seventeen", "eighteen", "nineteen"};
string tens[]={"null", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", 
"seventy", "eighty", "ninty"};
string hundreds[]={"null", "one hundred", "two hundred", 
"three hundred", "four hundred", "five hundred", "six hundred", "seven hundred", 
"eight hundred", "nine hundred"};
 

int main()
{
   double amount;
   int subdollars;
   int subcents;
   
   
   cout<<"Amount: ";
   cin>>amount;
   cout<<endl<<endl;

   double dollars;
   double cents;
   cents=modf(amount, &dollars)*100;
   
   cout<<dollars<<" "<<cents<<endl;
   cout<<endl<<endl;


	   if (dollars > 99)
	   {
		   subdollars=dollars/100;
		   cout<<hundreds[subdollars];
		   cout<<" ";
		   dollars=dollars-(subdollars*100);
	   }

	   if (dollars > 20)
	   {
		   subdollars=dollars/10;
		   cout<<tens[subdollars];
		   cout<<" ";
		   dollars=dollars-(subdollars*10);
	   }

	   if (dollars > 0 && dollars < 20)
	   {
		   subdollars=dollars;
		   cout<<single[subdollars];
		   cout<<" ";
		   dollars=dollars-(subdollars);
	   }

cout<<"dollars & ";

   
if (cents > 20)
	   {
		   subcents=cents/10;
		   cout<<tens[subcents];
		   cout<<" ";
		   cents=cents-(subcents*10);
	   }

	   if (cents > 0 && cents < 20)
	   {
		   subcents=cents;
		   cout<<single[subcents];
		   cout<<" ";
		   cents=cents-(subcents);
	   }

cout<<"cents"<<endl;
  
   return 0;
}

Works on mine, let me know how it goes for you.

Last edited by Valmont; 10-21-2004 at 05:39 PM.
keystoneman is offline   Reply With Quote
Old 10-21-2004, 04:47 PM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Nononono.
Look at "three hunderd" etcetera.
You already defined three and hundred individually. So don't define it again in the "hundreds" array. Mine is muuuch better
See how you can solve that huh, before I show you mine
By the way, mine goes up to "long" limit.This includes negative numbers as well.
__________________
Valmont is offline   Reply With Quote
Old 10-21-2004, 05:03 PM   #12 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
I just used three hundred instead of threehundred, they are defined right I believe.
keystoneman is offline   Reply With Quote
Old 10-21-2004, 05:37 PM   #13 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
No. I made a spelling error. This is what I mean:

"Three Hundred"
You have the word "Three" and "Hundred" defined already. Use what you got already to define "Three Hundred".

My native language is not the english language, but if I analyze the language, then I see the following distinctions:
UNITS: one, two, three, four, five, six, seven, eight, nine ... nineteen.
TENS: ten, twenty ... ninety.
HUNDREDS: hundred.
THOUSANDS: thousand.
MILLIONS: million.
SIGN: minus
ADDITIONAL: and

With this, you can form a truly neat sentence like:
"Minus Four Hundred and Fifty Seven".
Without cheating.

This is the essence of language analisys. Next step is to formalize it into some math, wich in turn is part of a sophisticated algorithm. It's harder then you think. I needed myself to throw in the best I can. Now you try to pump out what you can. You have the C++ know-how. Now it's time for some thinking. You do that on paper. Not in the IDE.

I go to bed now. I'll see you later.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Variable Numbers falsepride HTML, XML, Javascript, AJAX 1 10-28-2004 01:23 PM
adding numbers doesn't add up !!! help arwilliams HTML, XML, Javascript, AJAX 3 09-30-2004 10:17 AM
random numbers Rotkiv Standard C, C++ 1 07-19-2004 06:26 PM
how do you view line numbers in vc++.net? sde Standard C, C++ 1 02-20-2003 10:57 AM
format numbers sde PHP 3 10-03-2002 11:10 AM


All times are GMT -8. The time now is 08:44 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting