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

Need help for the code Billion number to string conversion

Hello Daniweb IT Community, this is the somehow my code for this problem of mine.

My only problem would be in the portion of getting the cents and the Billion it just because of the limitation of the long datatype?

I really need some opinions on how I can solve this problem. Thank you.!!!

#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

void pw(long,char[]);
char *one[]={" "," One"," Two"," Three"," Four"," Five"," Six",
" Seven","Eight"," Nine"," Ten"," Eleven"," Twelve"," Thirteen"," Fourteen",
"Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
char *ten[]={" "," "," twenty"," Thirty"," Forty"," Fifty"," Sixty",
"Seventy"," Eighty"," Ninety"};

void *centavos(int,char[]);
char *cents[]={" "," One"," Two"," Three"," Four"," Five"," Six",
" Seven","Eight"," Nine"," Ten"," Eleven"," Twelve"," Thirteen"," Fourteen",
"Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
char *tencents[]={" "," "," twenty"," Thirty"," Forty"," Fifty"," Sixty",
"Seventy"," Eighty"," Ninety"};


long n;
//int cp;
char *num, *cp;
char* c,d;


void main()
	{
				clrscr();

				printf("Enter any 9 digit no: ");
				gets(num);

			while( (c=strchr(num,',')) > 0)
				{
					memmove(c,c+1,strlen(c-1));
				}

				n=atol(num);
				if(n<=0)
					printf("Enter numbers greater than 0");

				else
					{
						//pw((n/1000000000),"Billion");
						pw((n/100000000),"Hundred");
						pw(((n/1000000)%100),"Million");
						pw(((n/100000)%100),"Hundred");
						pw(((n/1000)%100),"Thousand");
						pw(((n/100)%10),"Hundred");
						pw((n%100)," ");
					}

				if((strchr(num,'.')!=0))
					{
						int cpr,com,hab,con;
						cp = strchr(num,'.');
						com = cp-num;
						hab = strlen(num);
						for (cpr=com+1; cpr<=hab;cpr++)

						printf("%c",num[cpr]);
						//x=num[cpr];
						//printf("%c",x);
						//con=atoi(num[cpr]);
						//centavos((num[cpr]%100),"	");
						printf("Print if my decimal");

					}


getch();
}

void pw(long n,char ch[])
{
(n>19)?printf("%s %s ",ten[n/10],one[n%10]):printf("%s ",one[n]);
if(n)printf("%s ",ch);
}

/*void centavos(int cents,char hc[])
{
(n>19)?printf("%d %d ",tencents[n/10],cents[n%10]):printf("%f ",cents[n]);
if(n)printf("%s ",hc);
} */
Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

It is because you are reading input to random memory.
Line 24's num variable should be declared as:

char num[ 100 ];

(Where '100' is just the maximum number of character's I'd accept from the user.)

Also, please avoid use of thegets() function. Your instructor should not be teaching people to use it. (It is a very poorly designed function that should never be used! No kidding!)

Use fgets() instead.

You can also roll your own getline-type function. See mine here
http://www.cplusplus.com/forum/beginner/4174/page1.html#msg18271

[edit]
Also, avoid using the ternary operator ( ?: ) for top-level constructs. Use anif..else instead.

And main() is <strong>int</strong> main()

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Thank you very much for the reply.

Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

Hi Daniweb People, I am having this problem with my program.

http://www.daniweb.com/forums/thread172189.html this is the link for whole set of codes. My problems were:

1. How can I get the billion value for example 99,999,999,999.99 because the long datatype could handle this large value?

2. How could I solve this problem of places like the hundred places because it repeats like this 220,000 the value two hundred thousand twenty thousand ???

Really need your so badly. Thanks in advance.

Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

>How can I get the billion value for example 99,999,999,999.99

If this is an assignment it would make sense to expect the number 99,999,999,999.99 is stored as a char[] array as opposed to a long long double etc.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

		char *MyInput;
		char *c;

					char threedigits[4];

					int startPos;
					int numdigits = 3;

		int mxlen,i,x, cnt;
		int hun,hunc, hunrm, thou,mil,bil;
main()
{

	clrscr();

	printf("Enter Number to be converted: ");
	gets(MyInput);

				while( (c=strchr(MyInput,',')) > 0)
				{
					memmove(c,c+1,strlen(c-1));
				}


			startPos = strlen(MyInput);

			do {
				startPos = startPos - 3;
					if (startPos < 0) {
					numdigits = numdigits + startPos;
					startPos = 0;
						}
			strncpy(threedigits, MyInput + startPos, numdigits);
						threedigits[numdigits] = '\0';
						printf("%s ",threedigits);
	switch(threedigits[0]){
			case '1': printf("One Hundred");break;
			case '2': printf("Two Hundred");break;
			case '3': printf("Three Hundred");break;
		case '4': printf("Four Hundred"); break;
			case '5': printf("Five Hundred");break;
			case '6': printf("Six Hundred");break;
			case '7': printf("Seven Hundred"); break;
			case '8': printf("Eight Hundred"); break;
			case '9': printf("Nine Hundred"); break;
					}

			switch(threedigits[1]){
			//case '1': printf("One Hundred");
			case '2': printf(" Twenty"); break;
			case '3': printf(" Thirty"); break;
			case '4': printf(" Fourty"); break;
			case '5': printf(" Fifty");  break;
			case '6': printf(" Sixty");  break;
		case '7': printf(" Seventy");break;
			case '8': printf(" Eighty"); break;
			case '9': printf(" Ninety"); break;
					}
			switch(threedigits[2]){
		case '1': printf(" One"); break;
		case '2': printf(" Two"); break;
		case '3': printf(" Three");break;
		case '4': printf(" Four");break;
		case '5': printf(" Five");break;
		case '6': printf(" Six");break;
		case '7': printf(" Seven");break;
		case '8': printf(" Eight");break;
		case '9': printf(" Nine");break;
		case '10':printf(" Ten"); break;
			}  }

		} while (startPos > 0);


						//getting the centavos
					/*if (strchr(input,'.')!=0){
						printf("a ");
						}
						else{
						centavos((cnt%100)," "); }*/

getch();
return 0;
}

This is my new edited program. For some reasons, I would ask help. Thanks.

Problem:
1. Problem in placing the value of every places that supposed to have like for example of thousand, million and billions?
Any help will be appreciated. Thanks.

Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

> main()

That should be int main(void) or int main (args...) etc

> clrscr();
Not needed, and isn't standard so my compiler will throw up an error.

>gets
Still using gets(). You were told not to, ignore this at your peril.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

char *MyInput;
char *c;

char threedigits[4];

int startPos;
int numdigits = 3;

int mxlen, i, x, cnt;
int hun, hunc, hunrm, thou, mil, bil;
main()
{

   clrscr();

   printf ( "Enter Number to be converted: " );
   gets ( MyInput );

   while ( ( c = strchr ( MyInput, ',' ) ) > 0 )
   {
      memmove ( c, c + 1, strlen ( c - 1 ) );
   } 
   startPos = strlen ( MyInput );

   do
   {
      startPos = startPos - 3;
      if ( startPos < 0 )
      {
         numdigits = numdigits + startPos;
         startPos = 0;
      }
      strncpy ( threedigits, MyInput + startPos, numdigits );
      threedigits[numdigits] = '\0';
      printf ( "%s ", threedigits );
      switch ( threedigits[0] )
      {
      case '1':
         printf ( "One Hundred" );
         break;
      case '2':
         printf ( "Two Hundred" );
         break;
      case '3':
         printf ( "Three Hundred" );
         break;
      case '4':
         printf ( "Four Hundred" );
         break;
      case '5':
         printf ( "Five Hundred" );
         break;
      case '6':
         printf ( "Six Hundred" );
         break;
      case '7':
         printf ( "Seven Hundred" );
         break;
      case '8':
         printf ( "Eight Hundred" );
         break;
      case '9':
         printf ( "Nine Hundred" );
         break;
      }

      switch ( threedigits[1] )
      {
         //case '1': printf("One Hundred");
      case '2':
         printf ( " Twenty" );
         break;
      case '3':
         printf ( " Thirty" );
         break;
      case '4':
         printf ( " Fourty" );
         break;
      case '5':
         printf ( " Fifty" );
         break;
      case '6':
         printf ( " Sixty" );
         break;
      case '7':
         printf ( " Seventy" );
         break;
      case '8':
         printf ( " Eighty" );
         break;
      case '9':
         printf ( " Ninety" );
         break;
      }
      switch ( threedigits[2] )
      {
      case '1':
         printf ( " One" );
         break;
      case '2':
         printf ( " Two" );
         break;
      case '3':
         printf ( " Three" );
         break;
      case '4':
         printf ( " Four" );
         break;
      case '5':
         printf ( " Five" );
         break;
      case '6':
         printf ( " Six" );
         break;
      case '7':
         printf ( " Seven" );
         break;
      case '8':
         printf ( " Eight" );
         break;
      case '9':
         printf ( " Nine" );
         break;
      case '10':
         printf ( " Ten" );
         break;
      }
   }

}
while ( startPos > 0 ); 
//getting the centavos
/*if (strchr(input,'.')!=0){
	printf("a ");
	}
	else{
	centavos((cnt%100)," "); }*/

getch();
return 0;
}


If you indented your code properly you will see that you have too many braces etc.


Also why are you complicating matters by allowing the users to input commas and decimal values. It just makes your job harder. Deal with one part at a time, just consider converting big integers first. Once you getTHAT working then move on to the next part.

>case '10':
That is broken.

[search]convert numbers to words[/search]

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Thank you very much for the reply.
This is the requirements for this particular program.
A program could convert a user input number with or without the comma, thus I have problem in converting such large values. Any suggestion? Thanks.

Rein Valdez
Newbie Poster
21 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You