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

Numbers to words

hey um i kind of been trying to figure this out for awhile now...well the program is supposed to convert numbers like 123 into words such as one two three the only problem is my program writes the numbers back words such as three two one...does anyone have any ideas of what i can change or add to make these numbers come out foward in my program? im also kind of like a beginer at this so i need all the help i can get

#include <stdio.h>
void main() {
     int temp, num, count, i, reg[5], remainder;
     char *word[11] = {"one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};
             printf("Enter Number\n");
               scanf("%d",&num);

                   temp = num;
                   count = 0;

               while(temp !=0)
               {
                   remainder = temp % 10;
                   temp = temp / 10;
                   reg[count] = remainder;
                   ++count;
				}


                  for(i=1; i<=count; ++i) {

                   switch(i){
                        case 1:
                            switch(reg[0]){

               		     case 1:
                       		     printf("%s",word[0]);
				     break;
               			 case 2:
                       		     printf("%s",word[1]);
                       		     break;
               			 case 3:
                       		     printf("%s",word[2]);
                       		     break;

               			 case 4:
                       		     printf("%s",word[3]);
                       		     break;
               			 case 5:
                       		     printf("%s",word[4]);
                       		     break;
               		     case 6:
                       		     printf("%s",word[5]);
                       		     break;
               			 case 7:
                       		     printf("%s",word[6]);
                       		     break;
               			 case 8:
                       		     printf("%s",word[7]);
                                 break;
				 	  	 case 9:
                       		     printf("%s",word[8]);
                                 break;

				         default:
                       			printf("Zero");
                       			break;
                       }
                       break;
            case 2 :
                     switch(reg[1]){

               		  case 1:
                       	      printf("%s",word[0]);
                              break;
               		  case 2:
                       	      printf("%s",word[1]);
                              break;
               		  case 3:
                              printf("%s",word[2]);
                              break;

               		  case 4:
                              printf("%s",word[3]);
                              break;
               		  case 5:
                       	       printf("%s",word[4]);
                               break;
               		  case 6:
                       	      printf("%s",word[5]);
                              break;
               		  case 7:
                       	      printf("%s",word[6]);
                       	      break;
               		  case 8:
                       	      printf("%s",word[7]);
                              break;
			  		  case 9:
                       	      printf("%s",word[8]);
                              break;

               		  default:
                       		printf("Zero");
                       		break;
						}
						break;
            	case 3:
                    switch(reg[2]){

               		 case 1:
                       		printf("%s",word[0]);
                       		break;
               		 case 2:
                       		printf("%s",word[1]);
                      		break;
               		 case 3:
                       		printf("%s",word[2]);
                       		break;

              		 case 4:
                       		printf("%s",word[3]);
                       		break;
              		 case 5:
                       		printf("%s",word[4]);
                       		break;
             		 case 6:
                       		printf("%s",word[5]);
                       		break;
              		 case 7:
                       		printf("%s",word[6]);
                       		break;
              		 case 8:
                       		printf("%s",word[7]);
                       		break;
	 	 	         case 9:
                       		printf("%s",word[8]);
                                break;

               		default:
                       		printf("Zero");
                       		break;
			}
                        break;
             case 4:
                 switch(reg[3]){

                     case 1:
                       	  printf("%s",word[0]);
                          break;
               	     case 2:
                           printf("%s",word[1]);
                           break;
                     case 3:
                          printf("%s",word[2]);
                          break;

                     case 4:
                          printf("%s",word[3]);
                          break;
                     case 5:
                          printf("%s",word[4]);
                          break;
                     case 6:
                          printf("%s",word[5]);
                          break;
                     case 7:
                          printf("%s",word[6]);
                          break;
                     case 8:
                          printf("%s",word[7]);
                          break;
		             case 9:
                          printf("%s",word[8]);
                          break;

               		default:
                       		printf("Zero");
                       		break;


				}
                           break;

		case 5:
                    switch(reg[4]){

               	      case 1:
                             printf("%s",word[0]);
                             break;
               		 case 2:
                       		printf("%s",word[1]);
                       		break;
               		 case 3:
                       		printf("%s",word[2]);
                       		break;

               		 case 4:
                       		printf("%s",word[3]);
                       		break;
               		 case 5:
                       		printf("%s",word[4]);
                       		break;
               		 case 6:
                       		printf("%s",word[5]);
                       		break;
                	 case 7:
                       		printf("%s",word[6]);
                       		break;
                 	 case 8:
                       		printf("%s",word[7]);
                       		break;
			         case 9:
                       		printf("%s",word[8]);
                                break;
               		default:
                       		printf("Zero");
                       		break;


}
}
}
}
Sesshokotsu
Newbie Poster
1 post since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

You've got my respect for being able to understand those nested switches! :) I would have just used array indexing. Anyway, the problem is that you break the number down in reverse. To get it back you need to loop in reverse, from back to front.

#include <stdio.h>

int main(void)
{
  int i;
  int num;
  int reg[5];
  int remainder;
  int count = 0;
  char *word[11] = {"zero ","one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};

  printf("Enter Number\n");
  scanf("%d",&num);

  while (num != 0) {
    remainder = num % 10;
    num = num / 10;
    reg[count] = remainder;
    ++count;
  }

  for (i = count - 1; i >= 0; --i) {
    int index = reg[i];
    printf("%s", word[index]);
  }

  printf("\n");

  return 0;
}
Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
 

There is something similar to that project on DaniWeb's C code snippets:
http://www.daniweb.com/code/snippet381.html
Sort of the next step to this project

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 
#include <stdio.h>
#include <conio.h>
#include <string.h>

main()
{
	clrscr();

	int num,temp,rem;
	char dig[10][10]={"Zero ","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
	char numdig[50],tmp[50];

	printf("Enter a number : ");
	scanf("%d",&num);

	temp=num;

	while(temp!=0)
	{
		rem=temp%10;

		strcpy(numdig," ");
		strcpy(numdig,dig[rem]);
		strcat(numdig,tmp);
		strcpy(tmp,numdig);
		temp=temp/10;
	}

	printf("%s",numdig);

	getch();
}
dtejasd
Newbie Poster
6 posts since Apr 2009
Reputation Points: 8
Solved Threads: 0
 

That ^ post would be better of deleted. It violates about every rule in the book (homework, bumping, code tags) and furthermore: the solution is wrong. For future viewers of this thread: DO NOT use that code, just follow the link 'Ene Uran' posted.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

hi niek_e,
if you are referring to my post, then i'll appreciate if you can be more specific on your objection. I'll surely read the rules and regulation again for that matter but the solution isnt wrong. Please compile and execute the code. 'Sesshokotsu' says "the program is supposed to convert numbers like 123 into words such as one two three" and not like one hundred and twenty three. Please notice that. The link the other guy (Ene Uran) has given contains a very huge code which is not necessary and it is not what the questioner is asking for. I'll still welcome your comments on this. Thank you.

dtejasd
Newbie Poster
6 posts since Apr 2009
Reputation Points: 8
Solved Threads: 0
 

>It violates about every rule in the book (homework, bumping, code tags)
Homework: It's a clause for the person asking questions, not the person answering them. There's no explicit rule saying that you can't give the complete answer to homework (though it is an unwritten rule and you'll lose face by doing it).

Bumping: Bumping with value is not against the rules (a "me too" or "bump" post could earn a Keep It Organized warning though). It's an unwritten rule just like homework, but not deserving of any kind of official moderation.

Code tags: I already added code tags.

>furthermore: the solution is wrong
If I deleted every post that gave an incorrect solution, there wouldn't be any posts left. :D

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>furthermore: the solution is wrong If I deleted every post that gave an incorrect solution, there wouldn't be any posts left. :D


Are you saying you have been giving incorrect solutions all this time?

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

>Please compile and execute the code.
It fails to compile as written for me (due to clrscr and getch not being supported on my compile), and after removing those parts it crashes at runtime with a segmentation violation. You try to strcat tmp onto numdig but tmp isn't initialized (and thus, isn't a valid string).

I'd guess that you're using a Borland compiler where the memory for local variables is zeroed out, so the net effect is as if you did this:

int num = 0, temp = 0, rem = 0;
char dig[10][10]={
    "Zero ",
    "One ","Two ","Three ",
    "Four ","Five ","Six ",
    "Seven ","Eight ","Nine "};
char numdig[50] = {0}, tmp[50] = {0};

This avoids the problemon your compiler because tmp is now a valid empty string.

>Are you saying you have been giving incorrect solutions all this time?
Yes. Care to try figuring out why? ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Are you saying you have been giving incorrect solutions all this time?


No. She doesn'tgive solutions. She gives help... with a side dish of attitude :)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Honestly i didnt get what is that 'homework' and 'bumping' thing. Sounds irrelevant. If it is something related to "replying to already solved question", then I'm sorry. Yes, I'll keep in mind to add codetags. But still! you havent told me why the solution is WRONG?

dtejasd
Newbie Poster
6 posts since Apr 2009
Reputation Points: 8
Solved Threads: 0
 
No. She doesn't give solutions. She gives help... with a side dish of attitude :)


Another solution scheduled for deletion in this hypothetical scenario. Oh, wait, you don't call it solution... how does the song goes?"You call it potato...I call it pot..."

>Are you saying you have been giving incorrect solutions all this time? Yes. Care to try figuring out why? ;)


Why don't you tell me and I'll know for sure. :icon_smile:

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

OK! so there isnt anything wrong with the logic. Right? I'm using a DJGPP compiler by the way.

dtejasd
Newbie Poster
6 posts since Apr 2009
Reputation Points: 8
Solved Threads: 0
 

>OK! so there isnt anything wrong with the logic. Right?
It's hard to get the logic wrong with a program that simple. But there's more to writing good code than solid logic. Your code is non-portable and filled with poor practices, which many people will call "wrong".

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

lemme try this. gcc on windows xp.

>gcc -o num2word num2word.c
num2word.c:(.text+0x34): undefined reference to `clrscr'
collect2: ld returned 1 exit status


clrscr? at the beginning of the program? whodoes such a thing?

ok... so i remove clrscr. it compiles. i run it.

>num2word
Enter a number : 3
Three 2
>


"Three 2" ? what the heck does that mean?

your "logic" may be sound (all two lines of it :icon_rolleyes: ), but your program sucks. did we wait 2-1/2 years for THIS??

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

>clrscr? at the beginning of the program? who does such a thing?
Antisocial programmers who want to remove the output of all previous programs in the console window. I haven't made any secret of my disdain for this practice.

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

@jephthah

thanx for that arrogant reply. i would like to suggest you something. try gpp instead of gcc. if it doesnt work. download latest DJGPP compiler from www.delorie.com and then try again. i havent posted the code for fun. i have tested and posted only after getting results.

there is too much attention on things which can change from compiler to compiler. I'm not posting a code here to sell. The algorithm is right, the syntax is right, so thats it! Why discuss on compiler specific things? Anyways thanks all for your reply. I'm off this discussion.

dtejasd
Newbie Poster
6 posts since Apr 2009
Reputation Points: 8
Solved Threads: 0
 

>The algorithm is right, the syntax is right, so thats it!
Sorry slick, that's not it. If your code is so right, why are experienced programmers complaining that it fails to compile and crashes?

>Why discuss on compiler specific things?
You're the one who introduced compiler-specific things. We're simply pointing out where and how those compiler-specific things break your program on different compilers. Also note that the OP didn't mention a compiler, so the only safe subset of C is the one defined in the ISO standard.

>I'm off this discussion.
So you choose to cover your ears, close your eyes, and remain ignorant? I can't say that I'm surprised, but I am disappointed. It's a shame that there are people who are content with being mediocre.

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

GCC is the standard. yours is the derivative. anyhow the problem is (probably) not the compiler, the problem is that you're using non-portable, implementation-specific libraries to solve a trivial problem combined with poor coding practices.

if you're going to present a pedantic solution to be used by various people around the globe on a variety of platforms and compilers, then YOU'RE the one who needs to conform to the basic standards, and not try to sell us on your flavor-of-the-month.

The algorithm is right, the syntax is right,

actually its not. on the standard GCC compiler, in common and widespread used around the world, even if you get it to compile, the program operation FAILS to work correctly.

Fails. plain and simple. now you can either accept this fact and recheck your assumptions, or you can stand around like a dunce and continue argue your support for a failed implementation.

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

OK people. I'm getting a feeling of being novice talking to you. Your words appear jargon to me. I accept there may be, infact must be a better way to solve this. But believe me I'm not just stubborn to keep insisting that I'm right. For god's sake, please have a look at the original question again. I again accept you are better with this topic or overall programming language indeed. But now I'm starting to wonder how the hell did it work on my computer? There must be something wrong with the configuration..huh? eh? :(

dtejasd
Newbie Poster
6 posts since Apr 2009
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You