my program and when i try to convert from decimal to octal the first it give me the right answer

This is my program and when i try to convert from decimal to octal the first it give me the right answer, the second time it give me the new answer plus the first answer. for example when i type 10 it give me 12 then the second time i type in 10 it give me 1212. could you please help me.

#include

void decimaltobinary (void);
void decimaltooctal (void);
void decimaltohexa (void);

char binary[33]=""000000000000000000000000"";
char octal[12]=""00000000000"";
int remainder;
unsigned long decimal;
int n=0;
int i;
int choice;
unsigned long dec;

void main (void)
{

int choice;

do{
do{
printf(""1. decimal to binary\n2. decimal to octal\n3. decimal to hexa\n4. Quit\n"");
printf(""please enter a number: "");
scanf(""%d"", &choice);
printf(""\n"");
}while (choice<1||choice>4);

switch (choice)
{
case 1: decimaltobinary ();
break;
case 2: decimaltooctal ();
break;
case 3: decimaltohexa ();
break;
}
}while(choice!=4);
printf(""\n"");
}

void decimaltobinary (void)
{
printf(""enter a decimal number to be converted to binary: "");
scanf(""%d"", &decimal);

dec=decimal;
printf(""decimal is %ld: "", dec);

while(dec!=0)
{
remainder=dec%2;
dec=dec/2;
if (remainder==1)
{
binary[32-n]='1';
}
else
{
binary[32-n]='0';
}
++n;
}

for (i=0; binary[i]=='0'; ++i)
;// nothing here
for (; i<33; i++)
{
if ((i-1)%4==0)
{
printf("" "");
}
printf(""%c"", binary[i]);
}
printf("" in binary\n\n"");
}

void decimaltooctal (void)
{
char octal[12]=""00000000000"";
printf(""enter a decimal number to be converted to octal: "");
scanf(""%d"", &decimal);

dec=decimal;
printf(""decimal is %ld: "", dec);

while(dec!=0)
{

remainder=dec%8;
dec=dec/8;
switch (remainder)
{
case 0: octal[11-n]='0';
break;
case 1: octal[11-n]='1';
break;
case 2: octal[11-n]='2';
break;
case 3: octal[11-n]='3';
break;
case 4: octal[11-n]='4';
break;
case 5: octal[11-n]='5';
break;
case 6: octal[11-n]='6';
break;
case 7: octal[11-n]='7';
break;
}
++n;
}

for (i=0; octal[i]=='0'; ++i)
;// nothing here
for (; i<12; i++)
{
printf(""%c"", octal[i]);
}
printf("" in octal\n\n"");
/* octal[0]=' ';
octal[1]=' ';
octal[2]=' ';
octal[3]=' ';
octal[4]=' ';
octal[5]=' ';
octal[6]=' ';
octal[8]=' ';
octal[9]=' ';
octal[10]=' ';
octal[11]=' ';
octal[12]=' ';
octal[13]=' ';
octal[14]=' ';
octal[15]=' ';*/
}

void decimaltohexa (void)
{
printf(""enter a decimal number to be converted to hex: "");
scanf(""%d"", &decimal);

dec=decimal;
printf(""decimal is %ld: "", dec);

while(dec!=0)
{
remainder=dec%16;
dec=dec/16;
switch (remainder)
{
case 0: octal[11-n]='0';
break;
case 1: octal[11-n]='1';
break;
case 2: octal[11-n]='2';
break;
case 3: octal[11-n]='3';
break;
case 4: octal[11-n]='4';
break;
case 5: octal[11-n]='5';
break;
case 6: octal[11-n]='6';
break;
case 7: octal[11-n]='7';
break;
case 8: octal[11-n]='8';
break;
case 9: octal[11-n]='9';
break;
case 10: octal[11-n]='A';
break;
case 11: octal[11-n]='B';
break;
case 12: octal[11-n]='C';
break;
case 13: octal[11-n]='D';
break;
case 14: octal[11-n]='E';
break;
case 15: octal[11-n]='F';
break;
}
++n;
}

for (i=0; octal[i]=='0'; ++i)
;// nothing here
for (; i<12; i++)
{
printf(""%c"", octal[i]);
}
printf("" in octal\n\n"");
}

[4413 byte] By [developer-network] at [2007-11-16 5:42:13]
# 1 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
The previous respondent is correct on the changes needed to correct the symptoms that you describe. Since you traverse from right to left in filling the octal string, when you start another number, you start filling the new octal string from the last place of the previous number. This does not deserve a negative evaluation ...

- Mike
Michael Owen at 2007-11-8 0:43:27 >
# 2 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
Thanks a lot.
at 2007-11-8 0:44:35 >
# 3 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
Thanks alot
at 2007-11-8 0:45:31 >
# 4 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
The same program but by using recursive function named DecimalToBinary that accepts an integer number n as parameter :)

C++
Korn at 2007-11-8 0:46:31 >
# 5 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
[ Moved thread ]

Just an aside note for OP:

When posting code it's not necessary to replace '<' and '>' with '<' and '>' respectively.
However, to be easier to read, place it between [CODE] tags.
Pease take a look here (http://www.dev-archive.com/forum/misc.php?do=bbcode) to see the usage.
ovidiucucu at 2007-11-8 0:47:34 >
# 6 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
Talk about bringing an ancient thread back to life :D
Marc G at 2007-11-8 0:48:35 >
# 7 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
recursive function named DecimalToBinary that accepts an integer number n as parameter

C++

It's just like a practice
Korn at 2007-11-8 0:49:31 >
# 8 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
maybe a better exercise would be to transform from any base to any base (including those different from 2, 8, 10 or 16, for example from base 22 to base 13). these bases and the initial number should be read from input, and the input should be checked for errors (for example inputing a base bigger than all the letters and digits could express, or inputing the number FFA when the initial base is 14, etc). the input number should be case insensitive (ffA and FFA should lead to the same result).
a_c at 2007-11-8 0:50:36 >
# 9 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
#include<iostream.h>
void decimalToBinary(int);

void main(){
int num;

cout<<"Enter decimal number: ";
cin>>num;
cout<<endl;
cout<<"The binary is: ";

decimalToBinary(num);
cout<<endl;
}

void decimalToBinary(int n)
{
if (n==0)
return;
else{
decimalToBinary(n/2);
cout<<n%2;
}

}

That's it :)
Korn at 2007-11-8 0:51:42 >
# 10 Re: my program and when i try to convert from decimal to octal the first it give me the right answer
well, now do it the way I proposed.
a_c at 2007-11-8 0:52:35 >