0

Im making a program called Bulbs for the cs50x course. The program is supposed to output an inputted string into binary, using either yellow or black color for on and off in the terminal. I've tried with a few numbers as inputs, like 1 and 4, and it displays it correctly besides the third and fourth bulb that are ALWAYS on, no matter the input. Can you see why? Thanks in advance!

EDIT: I tried inputs "1" and "4", the terminal output are the yellow and black colors respective to digits 1 and 2. So for example if I inputted "1" id get: black, black, yellow, yellow, black, black, black, yellow (00110001) instead of (00000001).

EDIT 2: Everything works well now. It seems I just didnt uderstand ASCII. Thank you very much!! I almost cried when I saw every condition for sumbitting the code in green. Have a great day

#include <cs50.h>
#include <stdio.h>
#include <string.h>

const int BITS_IN_BYTE = 8;

void print_bulb(int bit);

int main(void) {
    string text = get_string("Text to encrypt!: ");
    int bit;
    int n = strlen(text);
    int array[BITS_IN_BYTE] = {0, 0, 0, 0, 0, 0, 0, 0};
    int count = 0;
    for ( int i = 0; i < n ; i++) {
        for (int j = 0, x = 7; j < 8 && x >= 0; j++, x--) {
                if (text[i] == 0) {
                    array[x] = 0;
                }
                else if (text[i] == 1) {
                    array[x] = 1;
                    text[i] = 0;
                }
                else if (text[i] != 1 && text[i] != 0) {
                    array[x] = text[i] % 2;
                    text[i] = text[i]/2; //jak dojdzie do 1 to?
                }
            }
        for (int y = 0; y <= 7; y++) {
            print_bulb(array[y]);
        }
        printf("\n");
    }
}

void print_bulb(int bit) {
    if (bit == 0) {
        // Dark emoji
        printf("\U000026AB");
    }
    else if (bit == 1) {
        // Light emoji
        printf("\U0001F7E1");
    }
}
Rae
  • 1
  • 2
  • There's no need for both `j` and `x` variables, since `x == 7 - j` always – Barmar Jul 25 '23 at 20:00
  • `text[i]` is the ASCII code for the digit, not the digit itself. The digit is `text[i] - '0'` – Barmar Jul 25 '23 at 20:02
  • You can remove most of the loop content anyway. If `"4"` is a valid input which is to be converted to binary, then `array[x] = text[i] % 2; text[i] = text[i]/2;` should work, except that it is going right to left instead of the conventional left to right. – Weather Vane Jul 25 '23 at 20:08
  • @Barmar I don't think that's the right dup. OP isn't converting *from* binary text. – Weather Vane Jul 25 '23 at 20:10
  • @Barmar it examines 8 bits of each character. – Weather Vane Jul 25 '23 at 20:13
  • @WeatherVane But first they have to convert from a character code to a number before they can examine the bits. That quesiton shows how to convert FROM a character TO the corresponding number. – Barmar Jul 25 '23 at 20:14
  • @Barmar "The program is supposed to output an inputted string into binary". OP then examines 8 bits of each byte, to convert to 8 bits of binary. – Weather Vane Jul 25 '23 at 20:15
  • I misunderstood. I thought it was inputting a number like `123`, then showing the binary of each digit successively. – Barmar Jul 25 '23 at 20:16
  • @Barmar agreed the problem description isn't all that clear. OP should please post an example input, expected output and actual output, in the question. So if the string is `"14"` should the output be `00000001 00000100` or should it be `00110001 00110100` (and the left-to-right or right-to-left sequence made clear)? – Weather Vane Jul 25 '23 at 20:17
  • You need to move `array` inside the loop, so it gets reset to all 0 before you process each character. – Barmar Jul 25 '23 at 20:19
  • Actually that's not needed, since you reassign all the elements of `array` every time. I tried your code, it seems to work as expected for me. – Barmar Jul 25 '23 at 20:31
  • `00110001` is the correct result for `1`. The ASCII code for `1` is 49. – Barmar Jul 25 '23 at 20:32
  • @WeatherVane It seems like I was in the right ballpark with my dup. The OP *was* confused about what the character value of digits is. – Barmar Jul 25 '23 at 21:37

0 Answers0