1

I'm trying to ask a user to enter two binary sequences and check if they are valid. here is my code.

char seq1[8], seq2[8];
printf("Enter 1st 8-bit sequence: ");
scanf("%8s", seq1);
getchar();
printf("\nEnter 2nd 8-bit sequence: ");
scanf("%8s", seq2);
getchar();
printf("\n");
printf("%s\n", seq1);
printf("%s\n", seq2);

if(strlen(seq1) < 8 || strlen(seq2) < 8){
    printf("Error: must enter 8-bits\n");
    exit(1);
}
for(int i = 0; seq1[i]!='\0'; i++){
    if(seq1[i]>48||seq1[i]<49){
        printf("Error: non-binary detected\n");
        exit(1);
    }

}
for(int i = 0; seq2[i]!='\0'; i++){
    if(seq2[i]>48||seq2[i]<49){
        printf("Error: non-binary detected\n");
        exit(1);
    }

}

When printing the strings I get the second string (seq2) added onto the end of seq1, and it always comes back as "Non-binary detected" even if it's all 1's and 0's.

example

2
  • thank you that works. I wonder why it wouldn't work with the ascii values Commented Feb 4, 2019 at 16:02
  • 1
    @IanClark Your condition was wrong. You were checking for values greater than 48 OR less than 49, which is all values. Commented Feb 4, 2019 at 16:04

2 Answers 2

1

There are a few things wrong with your code.

Let's try adding some extra information to your diagnostic messages:

if (strlen(seq1) < 8 || strlen(seq2) < 8) 
{
    printf("Error: must enter 8-bits; strlen(seq1) = %zu strlen(seq2) = %zu\n", strlen(seq1), strlen(seq2));
    exit(1);
}
for (int i = 0; seq1[i] != '\0'; i++)
{
    if (seq1[i] > 48 || seq1[i] < 49) 
    {
        printf("Error: non-binary detected (seq1[%d] = 0x%hhX)\n", i, seq1[i]);
        exit(1);
    }

}
for (int i = 0; seq2[i] != '\0'; i++) 
{
    if (seq2[i] > 48 || seq2[i] < 49) 
    {
        printf("Error: non-binary detected (seq2[%d] = 0x%hhX)\n", i, seq2[i]);
        exit(1);
    }
}

Right away we observe the error message: Error: non-binary detected (seq1[0] = 0x31). That's odd, because 0x31 is the ASCII code for the digit 1, which should be an acceptable binary digit. Reconsider your logic here:

seq1[i] > 48 || seq1[i] < 49 // In other words seq1[i] > '0' || seq1[i] < '1'

This statement will be true if seq1[i] contains a value that is greater than '0' or if it contains a value that is less than '1'. In other words, this statement will always be true and invoke the error handler. The correct conditional for what you want to test for is:

if(seq1[i] != '0' && seq1[i] != '1')

Also, if you're reading a string that's 8 characters long, you need a minimum of 9 characters in your buffer to hold the null-terminator.

Also, explicitly including != '\0' in a conditional is redundant.

Sign up to request clarification or add additional context in comments.

Comments

1

Your check for wrong characters is wrong.

if(seq1[i]>48||seq1[i]<49)

This check is always true because every number is >48 or <49. But you want to check if the value is <48 or >49.

Correct would be

if(seq1[i]<48||seq1[i]>49)

or better readable as

if(seq1[i]<'0'||seq1[i]>'1')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.