0

I'm trying to make a binary to decimal converter and I wanted to use _getch() so the user doesn't have to press enter every number he enters, but…

#include <stdio.h>
#include <conio.h>
int main() {
    int intnum[8], i = 0, ris;
    char charnum;
    for (i = 0; i < 8; i++) {
        charnum = _getch();
        printf("%c", charnum);
        intnum[i] = (charnum - '0');
    }
    ris = intnum[8] * 2^0;
    for (i = 7; i > 0; i--) {
        ris += (intnum[i] * 2 ^ j);
        j++;
    }
    printf("%d", ris);
    getchar();
}

…but the problem is that when they enter in the loop the user can enter only 4 numbers and not 8; can you explain why?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Danilo
  • 23
  • 5
  • 2
    `ris = intnum[8] * 2^0` is undefined behavior since `intnum[8]` would be the _ninth_ element of an eight-element array. Also, `^` does not do what you think it does: that is the bitwise exclusive-or operator, not an exponentiation operator. `^ 0` is a no-op. – TypeIA Dec 03 '18 at 18:10
  • 1
    `j` is not defined, so the code won't compile, but anyway precedence rules that `intnum[i] * 2 ^ j` is performed as `(intnum[i] * 2) ^ j` – Weather Vane Dec 03 '18 at 18:15
  • Note that if you replaced `_getch()` with `scanf("%c", &charnum)`, the user could perfectly well type `11011001` and then hit return, and your code would get 8 binary digits to read, one at a time. Granted, you'd not see the characters as they are typed, but that really doesn't matter for this code. However, that's not an explanation of your 4 vs 8 problem. (I agree with @TypeIA's diagnosis that `intnum[8] * 2 ^ 0` is a bug; but trying to raise the multiplication to the power of zero would be a bug, too, though of course in C it doesn't even do that. I also agree that `j` is undefined.) – Jonathan Leffler Dec 03 '18 at 18:17
  • thanks to all of you but i have another question, if I can't use "^" how can I write 2^0 and so on? – Danilo Dec 03 '18 at 18:23
  • All you need to do is multiply an accumulator by 2 and add the next digit (0 or 1). – Weather Vane Dec 03 '18 at 18:27
  • getch() got broken in a recent release, it generates an extra 0 for each keypress. That's why it looked like you only had to press 4 keys. Painful bug, but you can work around it in this case since you don't validate the key. Just keep calling getch() until it doesn't return 0. – Hans Passant Dec 03 '18 at 19:20

1 Answers1

0

After debugging of code,following changes you should do with your code..

  • declare int j = 0.
  • Also instead of ris = intnum[8] * 2^0 use ris = intnum[7] * 1 since array index start with 0 in c.
  • Also do same for for(i = 6;i >= 0;i--) and ris += intnum[i] * 2<<j with left shift operator(<<).
  • Basically use of getch() is unnecessary and makes your program non-portable,but if you want to use it,then I will suggest you to use below codes..

For linux,use this code..

    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>

    int getch (void){
            int ch;
            struct termios oldt, newt;
            tcgetattr(STDIN_FILENO, &oldt);
            newt = oldt;
            newt.c_lflag &= ~(ICANON|ECHO);
            tcsetattr(STDIN_FILENO, TCSANOW, &newt);
            ch = getchar();
            tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

            return ch;
    }
    int main() {
        int intnum[8], i = 0, ris,j = 0;
        char charnum;
        for (i = 0; i < 8; i++) {
            charnum = getch();
            intnum[i] = (charnum-'0');
        }
        ris = intnum[7] * 1;
        for (i = 6; i >= 0; i--) {
            ris += (intnum[i] * 2<<j);
            j++;
        }
        printf("%d", ris);
        getchar();
    }

For windows,use this code..

    #include <stdio.h>
    #include <conio.h>
    int main() {
            int intnum[8], i = 0, ris,j = 0;
            char charnum;
            for (i = 0; i < 8; i++) {
                charnum = getch();
                intnum[i] = (charnum-'0');
            }
            ris = intnum[7] * 1;
            for (i = 6; i >= 0; i--) {
                ris += (intnum[i] * 2<<j);
                j++;
            }
            printf("%d", ris);
            getchar();
        }
code_cody97
  • 100
  • 9