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);
}