Numbers To words

This is a discussion on Numbers To words within the C Programming forums, part of the General Programming Boards category; hey um i kind of been trying to figure this out for awhile now...well the program is supposed to convert ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Exclamation 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


    Code:
     
    #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;
    
    
    }
    }
    }
    }
    Last edited by Sesshokotsu; 10-05-2006 at 07:03 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Right now, you make it into a string as you go backwards (starting at 1's place and going up) and convert it immediately. You could convert it first, then go forward through the string. Note that blocks like this

    Code:
                                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;
    Can be reduced to

    Code:
    printf("%s", word[reg[0] - 1]);
    Or just add zero into your array and then

    Code:
    printf("%s", word[reg[0]]);

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  3. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 10:34 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21