Click to See Complete Forum and Search --> : c


November 12th, 1999, 12:18 AM
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 <stdio.h>

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

Michael Owen
November 12th, 1999, 09:39 AM
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

November 12th, 1999, 06:48 PM
Thanks a lot.

November 12th, 1999, 06:50 PM
Thanks alot

Korn
October 4th, 2005, 02:48 AM
The same program but by using recursive function named DecimalToBinary that accepts an integer number n as parameter :)

C++

ovidiucucu
October 4th, 2005, 02:57 AM
[ Moved thread ]

Just an aside note for OP:

When posting code it's not necessary to replace '<' and '>' with '&lt;' and '&gt;' respectively.
However, to be easier to read, place it between [CODE] tags.
Pease take a look here (http://www.codeguru.com/forum/misc.php?do=bbcode) to see the usage.

Marc G
October 4th, 2005, 03:25 AM
Talk about bringing an ancient thread back to life :D

Korn
October 5th, 2005, 04:42 PM
recursive function named DecimalToBinary that accepts an integer number n as parameter

C++

It's just like a practice

a_c
October 6th, 2005, 03:09 AM
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).

Korn
October 7th, 2005, 11:12 AM
#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 :)

a_c
October 7th, 2005, 01:24 PM
well, now do it the way I proposed.