My intergars to strings doesn't work now >.<

This is a discussion on My intergars to strings doesn't work now >.< within the C Programming forums, part of the General Programming Boards category; i was making a more simpilify way of converting numbers to words, and also handling zeros. My program stops at ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    121

    My intergars to strings doesn't work now >.<

    i was making a more simpilify way of converting numbers to words, and also handling zeros. My program stops at the first while!!!
    Code:
    #include <stdio.h>
       int  main(void) {
         	int temp,  num, count, R, sum[8],i, n[6], num1, timer = 0,w=0,x=0,  reg[11],  remainder,  tempCount=0;
              int value,value2, temp1,temp2,zero[6],temp_timer=0,p,zero_timer=0;
    	 int digit[10] = {10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
    
    	char *word[11] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
    
                   		printf("Enter Number\n");
    
                   		scanf("%d",&num); // input number
    
                       		temp = num;
                       		count = 0;
    
       while (temp != 0)
    	{
    		temp = temp /10;
    		++tempCount;
    		printf("COUNT IS %d\n", tempCount);
    	}
    
    
    		temp = num;
    
    		  temp1= num;
    
    		  for(  ; temp1 != 0;     ) {
                                           temp1 = temp1 + 1;
               		          temp2  =  temp1%10;
    
    		     zero[temp_timer] =  temp2 - 1;
    
    		            temp1 = temp/10;
                                                ++zero_timer;
    
                                              ++temp_timer;}
    
    	                   printf("works here");
    	 while(temp != 0)
    		{
    
    			R= temp%10;
    
    			temp = temp / 10;
    
    			n[w] = R;
    
    		 	sum[x] = n[w] * digit[tempCount];
    				++ w;
    
    	               	   	 --tempCount;
    
    	       			++ timer;
    
      		                ++ x;
    
      		for(     ; timer != 0; --timer) {
                                         x = 0;
    
                                         value = 0;
    
                                   value2 = value + sum[x];
    
                                       value = value2;
    
    			          num1 = value2;
    
                                          ++ x; }
                                           }
    
            while(num1 !=0 && count < 10) 		 //divides number and puts it in a special container
                {
                		 remainder =num1 % 10;	//which prints the numbers that were broken
    
    		num1 = num1 / 10;      	//up into single digits to be printed into words
    
                 		reg[count] = remainder; 	// base on reg
    
             		 printf("reg[ %d ] is %d\n",  count, reg[count]);
    
    			 ++count;
    
    		 	}
    
    
    
    		if(temp_timer > count ) {
    
                      for(   p=zero_timer; p != 0; -- p) {
    
    			printf("%s", word[zero[p]]);   }   }
    
                         else
                                for(i=0; i<count; ++i)  {
    				printf("%s",word[reg[i]]);	}
                       }

    This program writes the number for the first time backwards then forwards the second time. >.> basically so it can print forward.....and if there are any zero it is handle by for and that number is printed otu if the count doesn't catch zero.
    Attached Files Attached Files
    Last edited by Darkinyuasha1; 10-08-2006 at 04:17 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    121
    ok soo i change my program a bit soo it won't be so confusing
    Code:
    /* This program will take the integer that is keyed in and display it in english words such as 123 to one two three*/
    #include <stdio.h>
      void main() {
    int temp,   num,   count,   i;
     int reg[5], remainder,  a=0,  x,  num1=0,  R, digit_count=0;
         int  tempCount=0,  digit[5] = {1,10,100,1000,10000},  n[5];
       char *word[11] = {"one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};
                 printf("Enter Number\n");
                   scanf("%d",&num);
    			   /*asks the operator to enter a value and gains the value and makes it into num*/
    
                       temp = num;
                       count =0;
                   while(temp !=0){
                                      temp = temp/10;
                                    ++tempCount;
    								   ++digit_count;}
    
                        temp = num;
    
    
    
    
                         digit_count = digit_count - 1;
    
      		for( x = tempCount ; x != 0; -- x) {
    
    		  		R = temp % 10;
    
    				 temp = temp/ 10;
    
    				n[a] = R * digit[digit_count];
    
    
    				num1 = num1 + n[a];
                         --digit_count;
    					++ a; }
    
    
                        printf("%d\n",num1);
    
    	while(num1 !=0)
                   		{
    			   	/*about to declare the values of remainder, temp, and reg[count]*/
                      	 remainder = num1 % 10;
                       	num1 = num1 / 10;
                       	reg[count] = remainder;
                       	++count;
    				             printf("%d\n is %d\n",count,reg[count]);
    				}
    
    
                                for(i=0; i<count; ++i)  {
    				printf("%s",word[  reg[i]  ] );}
    
                        }

  3. #3
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    darn, it's still confusing becoz of the wacky indentation algorithm you employ. im sure the mods and experts will agree.
    It is not who I am inside but what I do that defines me.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Darkinyuasha1
    ok soo i change my program a bit soo it won't be so confusing
    How about I help you make it look like you want someone to read the code.
    Code:
    /* This program will take the integer that is keyed in and display it in english
       words such as 123 to one two three*/
    #include <stdio.h>
    
    int main()
    {
       int temp,   num,   count,   i;
       int reg[5], remainder,  a = 0,  x,  num1 = 0,  R, digit_count = 0;
       int tempCount=0,  digit[5] = {1,10,100,1000,10000},  n[5];
       char *word[11] = 
       {
          "one ","two ","three ","four ","five ","six ","seven ","eight ","nine "
       };
       printf("Enter Number\n");
       scanf("%d",&num);
       /*asks the operator to enter a value and gains the value and makes it into num*/
    
       temp  = num;
       count = 0;
    
       while ( temp != 0 )
       {
          temp = temp / 10;
          ++tempCount;
          ++digit_count;
       }
    
       temp = num;
       digit_count = digit_count - 1;
    
       for ( x = tempCount ; x != 0; -- x )
       {
          R = temp % 10;
          temp = temp / 10;
          n[a] = R * digit[digit_count];
          num1 = num1 + n[a];
          --digit_count;
          ++ a;
       }
    
       printf("%d\n", num1);
    
       while ( num1 !=0 )
       {
          /*about to declare the values of remainder, temp, and reg[count]*/
          remainder = num1 % 10;
          num1 = num1 / 10;
          reg[count] = remainder;
          ++count;
          printf("%d\n is %d\n",count,reg[count]);
       }
    
       for ( i = 0; i < count; ++i )
       {
          printf("%s",word[  reg[i]] );
       }
    
       return 0;
    }
    [edit]http://cboard.cprogramming.com/showthread.php?p=588534
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    121

    thanks

    The only weird thign i see is the reg[count] adds an extra 1 to all the digits >.<
    Last edited by Darkinyuasha1; 10-09-2006 at 09:07 AM.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    What happen if you type in 100? You don't have a "zero"

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    @Darkinyuasha1
    Learn how to indent your code properly - like Dave_Sinkula's for example.

    It's a lot easier for you to spot problems and a lot easier for us as well if your code is half-way presentable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Dave_Sinkula, I think it would be a good idea if this forum would automatically convert tabs into spaces.
    I like tabs but I don't like how web browsers display them.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > would automatically convert tabs into spaces.
    How many?
    2, 3, 4, 8 are all pretty common tab stops, how would you know which one to choose in order to not make the result an even bigger mess.
    Hell, I regularly come across files which change half-way through for some bizarre reason.

    Every forum handles tabs differently.
    Every editor handles tabs differently.
    Every diff/merge tool handles tabs differently.
    Every printer handles tabs differently.

    Tabs are just a mess. There's no way to guarantee that any user anywhere will see the same as what your IDE manages to present to you.

    All good IDEs have a "use spaces for tabs" option, and those with "smart" indent continue to do the expected thing whether you're using tabs or spaces.

    Arguments such as "but 4 spaces uses SO much more disk space than a tab" are pointless.

    If you just use spaces, then the whole world sees what you see, without any extra effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Web page designers/creators always use tabs, because the code is only for them to see.
    For developing, tabs are comfortable to use.
    Noone is going to use spaces because they maybe have to post it to a forum.

    I think 4 or 3 spaces are nice. I don't really care, which of them.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    121
    Quote Originally Posted by SKeane
    What happen if you type in 100? You don't have a "zero"
    yeaa that too but i can simply add that right now i got the issue with my reg...

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by maxorator
    Noone is going to use spaces because they maybe have to post it to a forum.
    I always use spaces. Most IDEs will let you convert tabs to spaces. It's simple to do. Obviously you're new here.

    You appear to be one of those people who think handouts should be given, and there should be no "effort" should be on the part of the recipient. God damnit people, is it too much to expect you try to help out the people who are trying to help you?

    "Hey I know! I'll make my code as unreadable as possible, and provide as little information as I can, so I never get any help!"

    Go find another forum some place. I'm tired of you.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    well quzah, some editors have this automated tab system. even if i do use space on the first indentation, the editor automatically indents the next line after {, which happens to be NOT in spaces but rather, tabs the length of your first indentation.

    it would be quite truoblesome if i backspace'd, then space'd a couple of times rather than let the editor handle all the indentation automatically. hope you get my drift
    It is not who I am inside but what I do that defines me.

  14. #14
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I am willing to bet a large sum of money that your editor in question has an option for use spaces for tabs if it has auto indention.

    Personally, I use 4space tabs... and before I post, I do a quick convert tabs to spaces.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    121
    here my revised program >.>
    Code:
     /* This program will take the integer that is keyed in and display it in english
       words such as 123 to one two three*/
    #include <stdio.h>
    
    int main(void)
    {
       int temp, num, count,   i;
       int reg[6], remainder,  a = 0,  x,  num1 = 0,c=0,b,A,reg2[6],  R, digit_count = 0;
       int tempCount=0,  digit[5] = {1,10,100,1000,10000},  n[5];
       char *word[11] =
       {  "zero", "one ","two ","three ","four ","five ","six ","seven ","eight ","nine "
       };
       printf("Enter Number\n");
       scanf("%d",&num);
       /*asks the operator to enter a value and gains the value and makes it into num*/
    
       temp  = num;
       count = 0;
    
       while ( temp != 0 )
       {
          temp = temp / 10;
          ++tempCount;
          ++digit_count;
       }
    
       while(A != 0)
       {
    	  A = num;
    	  A = A + 1;
    	  b = A%10;
    	  b=b-1;
    	  A=A/10;
    	  reg2[c] = b;
    	  ++c;
       }
    
       temp = num;
       digit_count = digit_count - 1;
    
       for ( x = tempCount ; x != 0; -- x )
       {
          R = temp % 10;
          temp = temp / 10;
          n[a] = R * digit[digit_count];
          num1 = num1 + n[a];
          --digit_count;
          ++ a;
       }
    
       printf("%d\n", num1);
    
       while ( num1 !=0 )
       {
          /*about to declare the values of remainder, temp, and reg[count]*/
          remainder = num1 % 10;
          num1 = num1 / 10;
          reg[count] = remainder;
          ++count;
          printf("%d\n is %d\n",count,remainder);
       }
    	if(c >count)
    		for(i=0; i <count; ++i)
    		 {
    			 printf("%s",word[	reg2[i]]);
    		}
    	else
       		for ( i = 0; i < count; ++i )
       		{
          		printf("%s",word[  reg[i]] );
       		}
    
       return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  3. Replies: 9
    Last Post: 01-21-2004, 03:53 AM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 10:05 AM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM

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