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;


}
}
}
}

Recommended Answers

All 23 Replies

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;
}
#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();
}
commented: his code is broken. don't use it. use the snippet provided previously, instead. -2

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.

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.

>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

>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?

>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 problem on 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? ;)

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

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

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?

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:

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

>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".

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? who does 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??

.

>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.

@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.

>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.

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.

.

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? :(

Split the number into individual digits and place the digits into an array. Then loop thru the array from the last value.

>But now I'm starting to wonder how the hell did it work on my computer?
Welcome to the world of C. There are many things that are tagged as unspecified, implementation-defined, and undefined. All of these can result in programs that work fine under your setup but fail miserably everywhere else. That's why it's so terribly important to follow the standards as closely as possible. It's also a good practice to compile and run your code using more than one compiler.

>There must be something wrong with the configuration..huh? eh?
No, there's something wrong with your code, as I've explained already: you rely on your specific implementation to do the right thing when you introduce undefined behavior.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.