1

i am trying to convert a string containing a 16-bit number in binary to a integer value. It is a homework assignment and i have to use scanf("%1d.... ). The problem i am having is that the loop wont end, i have no clue how to fix it.

for example: input: 0000000000001111
output: 15

int read_binary_value()
{ 
int value = 0;

while( scanf("%1d", &value) == 1) 
{
    printf("%d ", value);
    if (value == 1)
    {
        value += 1;
        value << 1;
    }

 }
 printf("yoyoyoyoyoyoyo");
 printf("%d",value);

 return value;
}
6
  • your while expression probably returns when you are ending it. Check if there is another way to express it. Commented Sep 20, 2015 at 22:44
  • it doesnt get to the "yoyoyoyoy" part when i compile it, i dont understand that Commented Sep 20, 2015 at 22:47
  • a while loop loops until the condition is false, otherwise it will run forever. In your case, it will scan forever. Commented Sep 20, 2015 at 22:48
  • You need to decide on what condition to use to indicate that the input has completed. For example, you could use the assumption that all 16 bits need to be entered. Then just use a for loop with 16 iterations instead of the current while loop. Then the loop completes when all 16 digits have been read or an invalid character is encountered. Commented Sep 20, 2015 at 22:53
  • Note that, if this is not a programming exercise / homework assignment, a call to strtol() would be preferable. Commented Sep 20, 2015 at 23:25

2 Answers 2

1

I modified your code and it works

int read_binary_value()
{
    int total=0;
    int value;
    while( scanf("%1d", &value) == 1)
    {
        printf("%d", value);
        if(!(value==0||value==1))
        {
            //Generate Error Message and Exit program
        }
        total = total << 1;
        if (value == 1)
        {
            total += 1;
        }

    }
    printf("yoyoyoyoyoyoyo\n");
    printf("%d",total);
    return total;
}

Your loop stop when get end of file. Press CTRL+Z for windows and Ctrl+D for linux. Those are End of File character.

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

7 Comments

thanks a lot! are there options to avoid pressing CTRL+Z ?
With scanf() in this condition, I don't know. You can read as string then convert it to integer.
It is insane to require the use of scanf("%1d") in the first place. Using getchar() would make so much more sense. scanf and friends are almost never the correct solution to any parsing problem.
The problem with scanf is that you cannot detect spaces to separate binary numbers. 1 1 will be converted to 3 just like 11 or even 1\n1, hardly expected behaviour!
Also, you should check if value is different from 0 and 1: parsing 121 as 5 would be incorrect.
|
0

If you want to get 16 inputs you can use for(i=0;i<16;i++) instead of the while to just run the loop 16 times and end. Or else within the while loop you can give if(i==16) break; incrementing i at the end of the loop. If you need to manually stop the loop, EOF char is the option.

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.