0

Hi guys! I have a program that makes you choose what kind of number conversion you would like to do between Binary to Octal and Binary to Hexadecimal . Here's the function that converts binary to hexadecimal:

int bintohex(void){
fract=0; bits=0;
 printf("Enter any binary number: ");

    while ((ch=getchar()) != EOF && ch != '.' && ch != '\n') {
    switch(ch) {
            case '0':
                whole = whole * 2;
                break;
            case '1':
                whole = whole * 2 + 1;
                break;
            default:
                printf("Bad input\n");
                return 1;
        }
    }
    printf("Hexadecimal value: %lX", whole);

Before all that, I made these conversions seperately (as in individual programs) and they worked perfectly fine. But now that I put them in one program (the one with the switch statement), it wouldn't even let the user to input a binary and just prints the answer as "0". And when I tried to put scanf("%d", &ch);,it would still answer "0".

Can someone tell me what I did wrong? Or the things that needed to be changed? Thank you very much in advance!

2
Contributors
1
Reply
12
Views
2 Years
Discussion Span
Last Post by David W
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.