I received an assignment to take input from the user (a binary string representing a number) and to print the received number both in decimal and in hexadecimal.
I created an additional function in order to determine whether the input of the binary string is correct (includes only 1's and 0's) or not.
The code is working perfectly fine, except when I input the string in a certain fashion:
Let's say I input 11110000
The function runs smoothly.
But when I try to input 1111000 as the first string I enter, the function tells me that the string is invalid.
If I input the same string(1111000) after I input 11110000, it works just fine!
Attached is my function:
int CheckIfDigits1or0(char *NumString[MAX_SIZE_INPUT+2])
{
char digit;
for (digit = *NumString; digit != '\0'; digit = *NumString++)
{
if (digit != '0' && digit != '1' && digit != '\n')
{
return 1;
}
}
return 0;
}
Help would be much appreciated!
if (strspn(str, "01") == strlen(str))but remove any newline first.char *NumString[MAX_SIZE_INPUT+2]is the problem. Wrong type. Post a minimal reproducible example.