Thread: Doing a Printing out Integers in String Format . Like 55 to Fifty five

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Doing a Printing out Integers in String Format . Like 55 to Fifty five

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*
     * 
     */
    int main(void ) {
        
        char stringEnteredVar[8]; 
        char stringUnitsVar[][10] = { "Zero","One", "Two", "three", "four", "five", "six", "seven", "Eight", "nine" };
        char stringTensVar[][10] = { "Tens","eleven", "twell", "thirteen", "fourteen", "fiofteen", "sixteen", "seventeen", "eiughtteen", "ninteen" };
        char stringAbove20[][10]= { "TWenty", "Thirty", "forty", "fifty", "sixty", "sevety", "eighty", "ninghty" };
        char stringHundred[] = {"Hundred"};
        char stringThousand[] = {"Thousand"};
        
        
        printf( "Enter a Number that is less than 1000:" );
        scanf ( "%s", stringEnteredVar );
    
        
        printf ( "%d , %d\n", strlen(stringEnteredVar) , atoi( stringEnteredVar) ); 
        
         if ( strlen(stringEnteredVar) == 1 && atoi( stringEnteredVar) <= 9 )
        {
            printf ( "UNITS:%s\n" , stringUnitsVar[atoi(stringEnteredVar)]);
    	printf ( "Unit\n" );
        }  
     
        else if ( strlen(stringEnteredVar) == 2 &&  atoi( stringEnteredVar ) <= 19)
        {
    
            
       	printf ( "TENS:%s\n" , stringTensVar[atoi(stringEnteredVar)-10]);
        }
        else if ( strlen(stringEnteredVar) == 2 && (atoi( stringEnteredVar ) >= 20 && atoi( stringEnteredVar) <= 99) )
        {
    
    
       
          char charFirst, charSecound ;
          charFirst = charSecound = '\0';
          
          charFirst = stringEnteredVar[0];
          charSecound = stringEnteredVar[1];
    
          printf ( "\n%d  %d\n",atoi(&charFirst) , atoi(&charSecound) );
          printf ( "%s", stringAbove20[atoi(&charFirst)-2],  );
          printf ( "%s", stringUnitsVar[atoi(&charSecound)] ) ;
    
         
        
          
        }
         
        else if ( strlen( stringEnteredVar ) == 3 && atoi( stringEnteredVar) >=100 && atoi ( stringEnteredVar) <= 999 ) 
        {
          char charFirst, charSecound , charThird ; 
    
          charFirst = stringEnteredVar[0];
          charSecound = stringEnteredVar[1];
          charThird = stringEnteredVar[2];
          // printf ( "%s%s and %s%s\n", stringUnitsVar[] 
    
          printf ( "hundreds : %c , %c , %c \n", charFirst, charSecound , charThird );
           
        }
            
        
         return (0);
    }
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    sorry Forgot to Add what the rror was

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*
     * 
     */
    int main(void ) {
        
        char stringEnteredVar[8]; 
        char stringUnitsVar[][10] = { "Zero","One", "Two", "three", "four", "five", "six", "seven", "Eight", "nine" };
        char stringTensVar[][10] = { "Tens","eleven", "twell", "thirteen", "fourteen", "fiofteen", "sixteen", "seventeen", "eiughtteen", "ninteen" };
        char stringAbove20[][10]= { "TWenty", "Thirty", "forty", "fifty", "sixty", "sevety", "eighty", "ninghty" };
        char stringHundred[] = {"Hundred"};
        char stringThousand[] = {"Thousand"};
        
        
        printf( "Enter a Number that is less than 1000:" );
        scanf ( "%s", stringEnteredVar );
    
        
        printf ( "%d , %d\n", strlen(stringEnteredVar) , atoi( stringEnteredVar) ); 
        
         if ( strlen(stringEnteredVar) == 1 && atoi( stringEnteredVar) <= 9 )
        {
            printf ( "UNITS:%s\n" , stringUnitsVar[atoi(stringEnteredVar)]);
        printf ( "Unit\n" );
        }  
     
        else if ( strlen(stringEnteredVar) == 2 &&  atoi( stringEnteredVar ) <= 19)
        {
    
            
           printf ( "TENS:%s\n" , stringTensVar[atoi(stringEnteredVar)-10]);
        }
        else if ( strlen(stringEnteredVar) == 2 && (atoi( stringEnteredVar ) >= 20 && atoi( stringEnteredVar) <= 99) )
        {
    
    
       
          char charFirst, charSecound ;
          charFirst = charSecound = '\0';
          
          charFirst = stringEnteredVar[0];
          charSecound = stringEnteredVar[1];
    
          printf ( "\n%d  %d\n",atoi(&charFirst) , atoi(&charSecound) );
          printf ( "%s", stringAbove20[atoi(&charFirst)-2]  );
          printf ( "%s", stringUnitsVar[atoi(&charSecound)]) ;
    
         
        
          
        }
         
        else if ( strlen( stringEnteredVar ) == 3 && atoi( stringEnteredVar) >=100 && atoi ( stringEnteredVar) <= 999 ) 
        {
          char charFirst, charSecound , charThird ; 
    
          charFirst = stringEnteredVar[0];
          charSecound = stringEnteredVar[1];
          charThird = stringEnteredVar[2];
          // printf ( "%s%s and %s%s\n", stringUnitsVar[] 
    
          printf ( "hundreds : %c , %c , %c \n", charFirst, charSecound , charThird );
           
        }
            
        
         return (0);
    }

    The Third Else if is sappose to print out the 2 Strings ..
    For example if I put in 55 .. it should print out fifty Five .
    Thou this dosnt seem to hapen i have looked around the ocde and havmnt spotted a problem .

    This isnt a University question . i got this question from the C beginners to Novice book by APRESS .

    Thanks
    Karan
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps some consistency would be good:
    Code:
        char stringUnitsVar[][10] = { "Zero","One", "Two", "three", "four", "five", "six", "seven", "Eight", "nine" };
    // Mixing upper and lower case - use lower case only
        char stringTensVar[][10] = { "Tens","eleven", "twell", "thirteen", "fourteen", "fiofteen", "sixteen", "seventeen", "eiughtteen", "ninteen" };
    // Several typos in red (green letters should be removed), and use lower case only 
        char stringAbove20[][10]= { "TWenty", "Thirty", "forty", "fifty", "sixty", "sevety", "eighty", "ninghty" };
    // As above
        char stringHundred[] = {"Hundred"};
        char stringThousand[] = {"Thousand"};
    Code:
          char charFirst, charSecound ;
          charFirst = charSecound = '\0';
          
          charFirst = stringEnteredVar[0];
          charSecound = stringEnteredVar[1];
    
          printf ( "\n%d  %d\n",atoi(&charFirst) , atoi(&charSecound) );
          printf ( "%s", stringAbove20[atoi(&charFirst)-2]  );
          printf ( "%s", stringUnitsVar[atoi(&charSecound)]) ;
    You shouldn't use atoi() on a single char - it's by far not guaranteed to be followed by a zero [to mark the end of the string], so you get some undefined value back from atoi, and that will give you undefined behaviour in general - it may work sometimes, but most of the time it won't.

    Just subtract '0' from the respective digit.

    [Further, I would suggest you only do ONE atoi() at the very beginning of your code, and then use this value combined with divide and modulo].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM