converting numbers to letters

This is a discussion on converting numbers to letters within the C Programming forums, part of the General Programming Boards category; ive made this program, and am getting an error, plz help! im wanting the user to enter in integers, and ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    36

    converting numbers to letters

    ive made this program, and am getting an error, plz help!
    im wanting the user to enter in integers, and the program will convert the integers into words.

    Code:
    #include <stdio.h>
    
    int PrintDigit(int number);
    
    int main(void)
    	{
    
    		int inputNumber;
    		int rc;
    		int remainder;
    		int divisor;
    		int digit;
    
    		printf("Please enter an integer:\n",&inputNumber);
    
    		rc = scanf("&#37;d",&inputNumber);
    
    		if(inputNumber==0)
    
    			printf("ZERO\n");
    			
    		else if(inputNumber < 0)
    			{
    				printf("Minus");
    				inputNumber = inputNumber*-1;
    				printf("%d", inputNumber);
    			
    			
    
    				remainder = inputNumber;
    				divisor = 10000;
    
    				while(divisor>0)
    					{
    						if(inputNumber>=divisor)
    							{
    								digit = (remainder/divisor);
    								remainder = remainder % divisor;
    								PrintDigit(digit); // Passing digit to function PrintDigit
    							}
     						divisor = divisor/10;
    					}
    			}
    
    		
    		return(0);
    }
    
    
    
    int PrintDigit(int number)
    {
    
    	int digit;
    
    
    	switch(digit) 
    	{
    			case 0:	printf("zero");
    						break;
    			case 1:	printf("one");
    						break;
    			case 2:	printf("two");
    						break;
    			case 3:  printf("three");
    						break;
    			case 4:  printf("four");
    						break;
    			case 5:  printf("five");
    						break;
    			case 6:  printf("six");
    						break;
    			case 7:  printf("seven");
    						break;
    			case 8:  printf("eight");
    						break;
    			case 9:  printf("nine");
    						break;
    			default:	printf("unknown digit detected");
    						return(number);
    	}
    }
    Last edited by sh4k3; 06-09-2007 at 10:58 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    And.... the error is?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    sorry.... more of a problem then an error...

    im getting stuck inside of my else if(inputNumber < 0) loop

    i cant really figure out why the program is not working... :S

    help plz?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    if inputNumber > 0, is it meant to not do anything?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well... ok, let's start with a few problems in the code:

    1. Your first printf() is wrong, since it doesn't need the second argument.
    2. Your program only handles numbers less than or equal to 0.
    3. Your switch() statement in PrintDigit() uses a local variable that has not been initialized instead of using the int that is passed to it. To put it mildly, this is silly.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    
    int PrintDigit(int number);
    
    int main(void)
    {
        int inputNumber;
    	int rc;
    	int remainder;
    	int divisor;
    	int digit;
    
       /* You don need to send the address of variable*/
    	printf("Please enter an integer:\n",inputNumber);
    	/* You get the  retrun value but wont do anything with it.*/
    	scanf("&#37;d",&inputNumber);
    
    	if(inputNumber==0)
    		printf("ZERO\n");
    	/* So the program works only for the negative values, but not for positive
    	   which mean it wont print what u wanted */
        else if(inputNumber < 0)
    	{
             /* So u are trying to convert the negative to postive values here.
              it would be more better if you give a error message for a negative values
              and the postive values from the user */
    		printf("Minus");
    		inputNumber = inputNumber*-1;
    		printf("%d", inputNumber);
    
    		remainder = inputNumber;
    		divisor = 10000;
    
    		while(divisor>0)
    		{
    			if(inputNumber>=divisor)
    			{
    				digit = (remainder/divisor);
    				remainder = remainder % divisor;
    				PrintDigit(digit); // Passing digit to function PrintDigit
    			}
    			divisor = divisor/10;
    		}
    	}
    	return(0);
    }
    
    int PrintDigit(int number)
    {
    	int digit;
        /* should you be sending the number as an argument to your switch ?? */
        switch(digit) 
    	{
           case 0: printf("zero");
                break;
           case 1:	printf("one");
    	        break;
           case 2:	printf("two");
    			break;
           case 3:  printf("three");
    			break;
           case 4:  printf("four");
    			break;
    	   case 5:  printf("five");
    			break;
    	   case 6:  printf("six");
    			break;
    	   case 7:  printf("seven");
    			break;
    	   case 8:  printf("eight");
    			break;
    	   case 9:  printf("nine");
    			break;
    	   default:	printf("unknown digit detected");
    			return(number);
       }
    }
    You need a proper code indentation. Apart i dont see any were in your code. where you try to convert the actual int to char. That was the main aim of the problem wasn't it.

    There are some commnets along the code. Go through it.

    NOTE: lol even I tried indenting your code. It dint work. Editor miss match.
    ssharish2005
    Last edited by ssharish2005; 06-10-2007 at 06:20 AM.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    36
    okay... thank you for all of the feedback.

    i modified the code to work properly now.

    Code:
    #include <stdio.h>
    #define neg -1
    
    //Function prototype section
    int PrintDigit(int number);
    
    int main(void)
    	{
    
    		int inputNumber;
    
    		int rc;
    		int remainder;
    		int divisor;
    		int digit;
    
    		printf("Please enter an integer:\n",&inputNumber);
    
    		rc = scanf("%d",&inputNumber);
    
    		if(inputNumber==0)
    
    			printf("ZERO\n");
    			
    		else if(inputNumber < 0)
    			{
    				printf("Minus");
    				inputNumber = inputNumber*neg;   // Making the input number positive
    			
    			
    				remainder = inputNumber;
    				divisor = 10000;
    
    				while(divisor!=0)
    					{
    						if(inputNumber>=divisor)
    							{
    								digit = (remainder/divisor);
    								remainder = remainder % divisor;
    								PrintDigit(digit); // Passing digit to function PrintDigit
    							}
     						divisor = divisor/10;
    					}
    			}
    			
    
    			else if(inputNumber > 0)
    			{
    				
    				remainder = inputNumber;
    				divisor = 10000;
    
    				while(divisor!=0)
    					{
    						if(inputNumber>=divisor)
    							{
    								digit = (remainder/divisor);  // Declaring digit = inputNumber / divisor
    								remainder = remainder % divisor; // Checking for remainders
    								PrintDigit(digit); // Passing digit to function PrintDigit
    							}
     						divisor = divisor/10;
    					}
    			}
    			
    		printf("\n\n\n");	
    		return(0);
    }
    
    /****************************************************************************************
    //
    //  FUNCTION:  Function PrintDigit
    // 
    //  WHAT IT DOES:  This function is passed a number, then prints the corresponding word
    //					based on the number that it receives.  There are 11 cases in total.
    //					If a case is true, the cases before the true case, are bypassed, then
    //					a printf is executed, a break occurs, then the number is returned.
    //
    //***************************************************************************************/ 
    
    int PrintDigit(int number)
    {
    
    	//int digit;
    
    
    	switch(number) 
    	{
    			case 0:	printf(" zero");
    						break;
    			case 1:	printf(" one");
    						break;
    			case 2:	printf(" two");
    						break;
    			case 3:  printf(" three");
    						break;
    			case 4:  printf(" four");
    						break;
    			case 5:  printf(" five");
    						break;
    			case 6:  printf(" six");
    						break;
    			case 7:  printf(" seven");
    						break;
    			case 8:  printf(" eight");
    						break;
    			case 9:  printf(" nine");
    						break;
    			default:	printf(" unknown digit detected");
    						return(number);
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting _getch() inputted numbers
    By bengreenwood in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2008, 11:46 PM
  2. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 10:34 AM
  3. 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
  4. Transforming letters to numbers...
    By N8760 in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2001, 03:26 PM
  5. How do you allocate numbers to letters?
    By face_master in forum C++ Programming
    Replies: 9
    Last Post: 11-14-2001, 06:47 AM

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