like i said in the title, having some problems with the check on the bubls problem, when I run the code on the terminal, the code run without a problem, but when it goes to the check it gets a bulb extra on the check, even if I copy and paste the input, on the terminal it has no problem, does anyone have any ideia on how to fix it?
#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("What is the text?\n");
int size = strlen(text);
for (int i = 0; i < size; i++)
{
int binary[BITS_IN_BYTE];
for (int j = 0; j < BITS_IN_BYTE; j++)
{
binary[j] = text[i] % 2;
text[i] /= 2;
}
for (int k = BITS_IN_BYTE; k >= 0; k--)
{
print_bulb(binary[k]);
}
printf("\n");
}
}
void print_bulb(int bit)
{
if (bit == 0)
{
// Dark emoji
printf("\U000026AB");
}
else if (bit == 1)
{
// Light emoji
printf("\U0001F7E1");
}
}
I already tried to change the signals on the for loops in a lot of manners and tried to hard code the variables, and tried to follow a tutorial in youtube, but in general it shouldn't have any problem in the code as far as i can see(which is not very far), thank you for your time!
sorry for any english typos and stuff like that, it's not my fisrt languague.
This for loop
for (int k = BITS_IN_BYTE; k >= 0; k--)
{
print_bulb(binary[k]);
}
accesses memory outside the array binary
when k
is equal initially to BITS_IN_BYTE
.
The loop can be rewritten like for example
for (int k = BITS_IN_BYTE; k > 0; )
{
print_bulb(binary[--k]);
}