Function with an argument without a return value not working properly on turbo c

89 views Asked by At

I have this program, which some of you may have seen from my past questions. This is about converting decimal to binary and octal, binary to decimal and octal, and octal to decimal and binary.

#include<stdio.h>

void MainMenu();
int UserInput();
unsigned long Dec2BinOct(unsigned long n);
void Dec2BinOct2(unsigned long n);
unsigned long Bin2DecOct(unsigned long n);
unsigned long Bin2DecOct2(unsigned long dec);
unsigned long Oct2BinDec(unsigned long n, int base, int base2);

unsigned long main()
{
    char choice;
    unsigned long n;


   clrscr();
   do{
    unsigned long bin, o, dec;

    MainMenu();
    printf("enter your choice: ");
    scanf(" %c", &choice);
    printf("\n");
    switch(choice){

        case 'A':
        case 'a':

            printf("Conversion: Decimal to binary and octal.\n");
            printf("Enter number: ");
        n = UserInput();

            o   = Dec2BinOct(n);

            printf("%lu in Decimal is ", n);
        Dec2BinOct2(n);
        printf(" in Binary Form.\n");
            printf("%lu in Decimal is %lu in Octal Form.\n\n", n, o);

        break;

        case 'B':
        case 'b':
            printf("Conversion: Binary to decimal and octal.\n");
            printf("Enter number: ");
            n = UserInput();


            dec = Bin2DecOct(n);
            o = Bin2DecOct2(dec);

            printf("%lu in Binary is %lu in Decimal Form.\n", n, dec);
            printf("%lu in Binary is %lu in Octal Form.\n\n", n, o);

        break;

        case 'c':
        case 'C':
            printf("Conversion: Octal to decimal and binary.\n");
            printf("Enter number: ");
            n = UserInput();


            bin = Oct2BinDec(n, 2, 10);
            dec = Oct2BinDec(n, 10, 8);

            printf("%lu in Octal is %lu in Binary Form\n", n, bin);
            printf("%lu in Octal is %lu in Decimal Form.\n\n", n, dec);


        case 'd':
        case 'D':
            printf("Exit.\n\n");
        break;

        default:
          break;

    }

   }while(choice != 'd'&& choice != 'D');
    getch();
}

void MainMenu()
{
    printf("Choices:\na. Decimal to binary and octal\nb. Binary to decimal and octal\nc. Octal to decimal and binary\nd. Exit.\n\n");
}

int UserInput(n)
{
    scanf("%lu", &n);
    return n;
}

unsigned long Dec2BinOct(unsigned long n)
{
    unsigned long place=1, bin=0;
    for(; n != 0; place *= 10)
                    {

                        bin   += n % 8 * place;
                        n     = n / 8;
                    }
    return(bin);
}

void  Dec2BinOct2(unsigned long n)
{
    int x=25, y=24;
    unsigned long p=n;
    while(x)
    {
        if(n<pow(2, x))
        {
            if(n>=pow(2, y))
            {
                printf("1");
                n-=pow(2, y);
            }
            else if(n<p)
            {
                printf("0");
            }
        }

        x--;
        y--;
    }

}

unsigned long Bin2DecOct(unsigned long n)
{
    unsigned long ans=1, dec=0;
    for(; n != 0; ans *= 2)
                    {
                        
                        dec = dec + n % 10 * ans; 
                        n   = n / 10;
                    }
    return(dec);
}

unsigned long Bin2DecOct2(unsigned long dec)
{
    unsigned long o=0, place=1;
    while (dec != 0)
                    {
                        o     = o + (dec % 8) * place;
                        dec   = dec / 8;
                        place = place * 10;
                    }
    return(o);
}

unsigned long Oct2BinDec(unsigned long n, int base, int base2)
{
    unsigned long bin=0, place=1;
    for(; n != 0; place *= base2)  
                    {      
                        bin   += n % base * place;
                        n     = n / base;  
                    }
    return(bin);
}

But when I chose option letter a/A, the function Dec2BinOct2(n) won't do its thing. It won't print.

Choices:
a. Decimal to binary and octal
b. Binary to decimal and octal
c. Octal to decimal and binary
d. Exit.

enter your choice: >>>a

Conversion: Decimal to binary and octal.
Enter number: >>>123
123 in Decimal is in Binary Form.
123 in Decimal is 173 in Octal Form.

But in dcoder, it works just fine. I appreciate all of your help.

1

There are 1 answers

1
Dexter1403 On

I know the answer now.

#include<math.h>

That's why the pow function isn't working and nothing is printing. Anyways, thank you guys!