Thread: Help for debugging a program

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    14

    Help for debugging a program

    Dear friends,

    I tried to do my homework and came to some ideas and I wrote a program. but it is not working properly. would be nice if you can check it and give some comments.

    the orginal problem is:
    Write a program, that enters an integer decimal number and a base number (<10), using a void function Input().
    A funtion void To_Base() then converts this number to a number according to the entered base, after which a function Output() prints the results neatly
    Use pointers! Array’s are not allowed!

    and here is my codes:

    Code:
    /***********************************************************************\
    |this program converts decimal number to base number			               |
    |                                                                                                                                              |
    |Programmer		: Saeid   Yazdani                                                                     |
    |Class			: ELI1A                                                                                    |
    |Date			: 27 may 2009                                                                        | 
    \***********************************************************************/
    
    /*INCLUDE: */
    #include <stdio.h>
    #include <conio.h>
    
    /*DEFINE FUNCTION: */
    void Input(int*, int*);
    void To_Base(int, int);
    void Output(int);
    
    int main (void)
    {
    	int nDec, nBase;
    	char chAnswer='y';
    
    	while(chAnswer=='y')
    	{
    		printf("this program converts a decimal number into a base number");
    
    		Input(&nDec, &nBase);
    		printf("\n%d in %d base is:", nDec,nBase);
    
    		To_Base(nDec,nBase);
    
    		printf("\n Do you want to use this program again?(y/n)");
    		chAnswer=getche();
    	}
    	printf("\n Thank you for using my program!");
    	return 0;
    }
    
    void Input(int *pDec, int *pBase)
    /****************************************************\
    |Description   : Input funtion for nDecimal and nBase                       |
    |Arguments	   : int *pDec, points to nDecimal                      |
    |	                  int *pBase , points to nBase        |
    |Return value  : no return value                     |
    \****************************************************/
    {
    	int nRepeat = 1;
    
    	while(nRepeat == 1)
    	{
    		printf("\nplease enter the positive decimal number(from 0 to 32767) : ");
    		scanf("%d",pDec);
    		if(*pDec>=0)
    			nRepeat = 0;
    		else
    		{
    			printf("the entered decimal number is invalid\n");
    	      nRepeat = 1;
    		}
    	}
    
    	/*starting value*/
    	*pBase = -1;
    
    	while(*pBase<=1 || *pBase>9)
       {
    		printf("\nplease enter the base(<10) : ");
    		scanf("%d",pBase);
    		if(*pBase<1 || *pBase>9 )
         	printf("the entered base is invalid\n");
    	}
    }
    void To_Base(int nDec,int nBase)
    /****************************************************************\
    |Description 	: Converts the nDecimal to a number in nBase base |
    |				  	  Calls Output funtion                            |
    |Arguments   	: int nDecimal, the entered decimal number        |
    |				  	  int nBase , the entered base                    |
    |Return value	: no return value		   								  |             
    \****************************************************************/
    {                                   
    	int nRest, nTimes=1, nI, nPrint;
    
    	nRest = nDec/nBase;
    
    	while(nRest>=nBase)
    	{
    	  nTimes = nTimes+1;
    	  nRest = nRest/nBase;
    	}
    
    	while(nTimes>=1)
    	{
    	  nRest = nDec;
    	  for(nI=1; nI<=nTimes; nI = nI+1)
    	  {
    		 nRest = nRest/nBase;
    	  }
    	  nPrint = nRest%nBase;
    	  Output(nPrint);
    	  nTimes = nTimes-1;
    	}
    	
    	Output(nDec%nBase);
    }
    
    void Output(int nPrint)
    /***********************************************************\
    |Description     : Print a number                          |
    |Argument		  : int nPrint,received from To_Base funtion |
    |Return value    : no return value                          |
    \***********************************************************/
    {
    	printf("%d",nPrint);
    }
    Last edited by Saeid87; 05-27-2009 at 06:46 AM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    How does it not work properly? Does it not compile? (What compiler errors?) Does it not run? Does it not give the expected output? (What IS the expected output? What did you get?)

    More details.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    14
    oh Sorry I found the problem already. sorry for bothering.

    I also edited the code in my first post aswell so it maybe useful for somone.
    Last edited by Saeid87; 05-27-2009 at 06:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM