954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting numbers to words

Hello I'm new here and I've been up two night's trying to figure out how to write a program that will convert numbers to word. If anyone can help please respond to the post. here is what I sort of figured out so far. As you can tell I'm still only about 5 months into programming. I need the program to be able to do this up to 20,000.

/** Brandon Brown **/
/** 2/17/05 **/

//*ALGORITHM

#include <iostream> 
#include <fstream>
  using namespace std;
 int main() 
 { 

    int number, one ;
		 
cout << "enter number  "<< endl;
cin >>number;


	 cout << number<< "number :"<< endl;

switch (int)
 case 1:
cout << "one";

switch (int)
 case 2:
cout << "two";
 
switch (int)
 case 3:
cout << "three";
 
switch (int)
 case 4:
cout << "four";
 
switch (int)
 case 5:
cout << "five";
 
switch (int)
 case 6:
cout << "six";

switch (int)
 case 7:
cout << "seven";



return 0;
 }
zerohero
Newbie Poster
1 post since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

You should search the forum before asking a question that's been [thread=11968]asked before[/thread].

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

can u cr8 me program that words converted to numbers.(1-50) using switch. pls help me.

soulcandra44
Newbie Poster
14 posts since Aug 2010
Reputation Points: 8
Solved Threads: 0
 

Sure. Since your language's orthography apparently uses digits for any homonymous syllable, just use

(std::stringstream() << n).str()
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

pls post.

soulcandra44
Newbie Poster
14 posts since Aug 2010
Reputation Points: 8
Solved Threads: 0
 

Sure. Since your language's orthography apparently uses digits for any homonymous syllable, just use

(std::stringstream() << n).str()


This gave me a good laugh.

Allophyl
Light Poster
26 posts since Jul 2010
Reputation Points: 12
Solved Threads: 2
 

plsss. i need it. can u make that program? i need it badly. pls

soulcandra44
Newbie Poster
14 posts since Aug 2010
Reputation Points: 8
Solved Threads: 0
 

How do you convert numbers to words by hand? Take that algorithm and write it in C++.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Make a while loop to make a number (I called this one divisor) an multiply it by 10 till its larger than the number you're calculating.

Then in another loop, you're going to need to divide by divisor, get both the remainder AND the front digit and store them. Don't store them over n.

Then you're going to make a bunch of if statements to calculate the correct words based on the front (the current digit you're working on.) When I wrote it the total I have is 5 switch statements inside 4 if statements + some minor if statements to end the loop if there's errors or nothing else to calculate.

Here's my program:

#include <conio.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char *make_words(char *s, int ncomma);
char *insert_comma(long n, int *ncomma);
char *int2words(int n);
int a;
 
int main(void)
{
 


 printf("Enter number: ");
 scanf("%d",&a);
  printf("%d = %s\n",a,int2words(a));

 
  getch();  
  return 0;
}

char *make_words(char *s, int ncomma)
{
  int i, len, rest = 0;
  char *p = NULL;
  static char zzz[256];
 
  static char *ones[] = {"one ","two ","three ","four ",
    "five ","six ","seven ","eight ","nine "};
  static char *tens[] = {"ten ","eleven ","twelve ","thirteen ",
    "fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
  static char *twenties[] = {"","twenty ","thirty ","forty ",
    "fifty ","sixty ","seventy ","eighty ","ninety "};
  static char *hundreds[] = {
    "hundred ","thousand ","million "};
 
  memset(zzz, '\0', 256);  // fill with nulls
  len = strlen(s);
  for(i = 0; i < len; i++)
  {
 
    if ((p = strchr((s[i] == ',') ? &s[++i] : &s[i], ',')) == NULL)
    {
      p = &s[strlen(s)];
    }
    if (s[i] == '0')
    {
      continue;  // skip one iteration
    }
    if ((rest = (p - &s[i])) != 0)
    {
      if (rest == 3)
      {
        strcat(zzz, ones[s[i] - '0' - 1]);
        strcat(zzz, hundreds[0]);

        if (len == 7 && s[2] == '0')  strcat(zzz, hundreds[1]);
        if (len == 11 && s[2] == '0')  strcat(zzz, hundreds[2]);
      }
      else if (rest == 2) 
      {
        if (s[i] == '1')
        {
          strcat(zzz, tens[s[++i] - '0']);
          rest--;
        }
        else
        {
          strcat(zzz, twenties[s[i] - '0' - 1]);
        }
      }
      else
        strcat(zzz, ones[s[i] - '0' - 1]);
    }
    if (rest == 1 && ncomma != 0)
    {
      strcat(zzz, hundreds[ncomma--]);
    }
  }
  return zzz;
}
 

char *insert_comma(long n, int *ncomma)
{
  static char zzz[30];
  int i = 0;
  char *p = &zzz[sizeof(zzz)-1];
 
  *p = '\0';
  *ncomma = 0;
  do 
  {
    if (i % 3 == 0 && i != 0) 
    {
      *--p = ',';
      ++*ncomma;
    }
     *--p = (char)('0' + n % 10);
    n /= 10;
    i++;
  } while(n != 0);
  return p;
}
 
char *int2words(int n)
{
  int nc;
  char *ps, *zzz, *minus;
  char *buffer;
  buffer = (char *) malloc(256);
 
  // save any - sign
  if (n < 0)
  {
    minus = "minus";
    n = abs(n);
  }
  else
  {
    minus = "";
  }
 
  ps = insert_comma(n, &nc);

  zzz = make_words(ps, nc);
 
  sprintf(buffer,"%s %s", minus, zzz);
 
  return buffer;
  getch();
}
mixmagz
Newbie Poster
6 posts since Dec 2010
Reputation Points: 6
Solved Threads: 0
 

# include
void main () {

Int N;


printf("Input number:");
scanf("%d",&N);
if
(N==1){
printf("One");
}
else if
(N==2){
printf("Two");
}
else {
printf("Invalid")
}


}

why don't you used else if?
its good but super basic...

Kalel Kristoff
Newbie Poster
1 post since Jan 2012
Reputation Points: 6
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You