digits to english

This is a discussion on digits to english within the C Programming forums, part of the General Programming Boards category; This program is supposed to output the digits of a number in English. so if the user inputs 100, the ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    digits to english

    This program is supposed to output the digits of a number in English. so if the user inputs 100, the output should be "one, zero, zero".

    i cant figure out whats wrong with my program...
    Code:
    #include <stdio.h>
    
    void get_string( char buffer[], int size ) ;
    char english_number( char string[] ) ;
    
    void get_string( char buffer[], int size )
    {
     	char character;
     	int j = 0;		
    
     	do									/* Get a character until newline or 		*/
     	{									/* we run out of characters.				*/
    		character = getchar() ;
    		buffer[j] = character ;
    		++j;
    	}
    	while ( character != '\n' && j < size ) ;
    	
    	while ( character != '\n' )			/* Get rid of extra characters.				*/
    		character = getchar() ;
    
    	buffer[j - 1] = '\0' ;				/* Replace newline with the null-byte.		*/
    }
    
    void main( void )
    {
    	char line[81] ;
    	int count ;
    	char result, one, two, three, four, five, six, seven, eight, nine ;
    
    	printf( "Enter a number  :  " ) ;
    	get_string( line, 81 ) ;
    
    	for( count = 0 ; line[count] != '\0' ; count++ )
    	{
    		english_number( line ) ;
    		printf( "%s\n", line ) ;
    	}
    
    }
    
    char english_number( char string[] )
    {
    	int count ;
    	char one, two, three, four, five, six, seven, eight, nine ;
    
    	for( count = 0 ; string[count] != '\0' ; count++ )
    	{
    		if( string[count] == '1' )
    			return( 'one' ) ;
    		else if ( string[count] == '2' )
    			return( 'two' ) ;
    		else if ( string[count] == '3' )
    			return( 'three' ) ;
    		else if ( string[count] == '4' )
    			return( 'four' ) ;
    		else if ( string[count] == '5' )
    			return( 'five' ) ;
    		else if ( string[count] == '6' )
    			return( 'six' ) ;
    		else if ( string[count] == '7' )
    			return( 'seven' ) ;
    		else if ( string[count] == '8' )
    			return( 'eight' ) ;
    		else if ( string[count] == '9' )
    			return( 'nine' ) ;
    	}
    
    }
    is there something wrong with the else if statements? or is there another way to do it? or am i completely lost?
    Watshamacalit

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char english_number( char string[] )

    This should return a character pointer.

    return( 'three' ) ;

    Use double quotes for strings. ' ' is used for SINGLE CHARACTERS ONLY.

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

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void main( void )
    is a big no no. It's int main(void) and return something at the end.

    And please use code tags.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    digits to english

    ok...

    i tried yer suggestions but it still doesnt work...

    i keep getting this error

    C:\Program Files\Microsoft Visual Studio\MyProjects\11\1101p04kt.cpp(54) : error C2440: 'return' : cannot convert from 'char [5]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast

    what does that mean?
    Watshamacalit

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Change a couple of things:

    >english_number( line ) ;
    >printf( "%s\n", line ) ;
    Replace these with
    >printf ("%s\n", english_number( line ) );

    And change the return type of english_number from char to char*.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    digits to english

    ok...that solved that error...

    but now im getting this warning...

    C:\Program Files\Microsoft Visual Studio\MyProjects\11\1101p04kt.cpp(16) : warning C4101: 'result' : unreferenced local variable

    and it gives it for all my char variables in english_number
    Watshamacalit

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It means you haven't actually used them, so there's no point in them being there.

    Oh, I forgot, string is a reserved name (as is anything starting str followed by a letter), so don't use it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Whether it's proper coding or not is open to debate, but how about something like.

    Code:
    #include <stdio.h>
    
    void printString(char []);
    
    int main(void)
    {
      char buffer[20];
    	
      printf("Enter a number: ");
      fgets(buffer, 20, stdin);
      printString(buffer);
      return 0;
    }
    
    void printString(char source[])
    {
      char *alphaValue[10] = { "Zero", "One", "Two", "Three",
                          "Four", "Five", "Six", "Seven",
    		      "Eight", "Nine" };
      char digit;
      int i;
      
      for(i = 0; source[i] != '\0'; i++)
      {
        digit = source[i];
        switch(digit)
        {
          case '0': printf("%s ", alphaValue[0]);
    	        break;
          case '1': printf("%s ", alphaValue[1]);
    	        break;
          case '2': printf("%s ", alphaValue[2]);
    	        break;
          case '3': printf("%s ", alphaValue[3]);
    	        break;
          case '4': printf("%s ", alphaValue[4]);
    	        break;
          case '5': printf("%s ", alphaValue[5]);
    	        break;
          case '6': printf("%s ", alphaValue[6]);
    	        break;
          case '7': printf("%s ", alphaValue[7]);
    	        break;
          case '8': printf("%s ", alphaValue[8]);
    	        break;
          case '9': printf("%s ", alphaValue[9]);
    	        break;
          default:  break;
        };
      }
    }
    Your code above seems a little busy just to print out one, two, three....
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    x=isdigit(something);
    printf("%s", x < 10 && x > -1 ? alphaValue[x] : "invalid" );

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

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Doh... quzah got me again, but In my defense I'm merely a pseudo coder.... remember?.... :P

    Hmm... I rather like that shortcut, yet another one added to my notebook. Thanks
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by quzah
    x=isdigit(something);
    printf("%s", x < 10 && x > -1 ? alphaValue[x] : "invalid" );

    Quzah.
    eh? Are you talking about the C standard function isdigit() ? I presume not, as your example wouldn't work too well.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    const char *alphaValue[] = 
    {
        "Zero", "One", "Two", "Three", "Four",
        "Five", "Six", "Seven", "Eight", "Nine" 
    };
    
    void PrintString(char *source)
    {
        for ( ; *source != '\0'; source++)
            if (isdigit(*source))
                printf ("%s ", alphaValue[*source - '0']);
            else printf("Invalid ");
        putchar('\n');
    }
    
    int main(void)
    {
        PrintString("123Z45");
        return(0);
    }
    /*
    Output
    One Two Three Invalid Four Five
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    eh? Are you talking about the C standard function isdigit() ? I presume not, as your example wouldn't work too well.
    No, that was me being side tracked while posting. I was looking at the post preceeding mine, and saw the reference to "digit" in there. For my excuse, I was actually on the phone at the time. I simply meant to validate the single character to ensure it was a number.

    Convert x to an integer, x being a single character, and then apply it to that line.



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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. Compute without Limit - support for fractions and 2G Digits!
    By etlam in forum Projects and Job Recruitment
    Replies: 14
    Last Post: 02-07-2008, 11:46 AM
  3. english, english, english, ...
    By ElastoManiac in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-05-2005, 05:46 AM
  4. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 03:05 PM
  5. The witch who stole english!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-08-2003, 04:53 AM

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