views:

146

answers:

2

Hey guys,

I'm working on a c++ program and I need to take in a binary number from 0-255, inclusive, as a string(it has to be a string). What can I write in a while(input invalid) loop to check that the string is between 00000000 to 11111111, inclusive. Thanks so much

+5  A: 

Assuming you use std::string:

while( str.length()==8 && str.find_first_not_of("01")==std::string::npos )

that is, if you really want it to be always 8 characters. Adjust to suit your taste (for what I can tell by your comment, you want str.length()<=8 or (!str.empty()) && str.length()<=8.

Michael Krelin - hacker
This works for rejecting something like 1111111111but not for 11111100000000.
danny z
danny z, I'm not sure what you're trying to say. Your both examples will be rejected because they're longer than 8 characters.
Michael Krelin - hacker
What I meant was I just wrote the loop and checked it and it didn't reject 1111110000000
danny z
I've just tried it, and (not surprisingly) it works for me.
Andy
danny z, obviously the code above could not possibly accept `1111110000000`, so the bug is not in the condition. Feel free to show your code if you can't find the error.
Michael Krelin - hacker
yeah the error has to be here: while(!input.empty() input is the string which contains the binary number.Thanks again for the help guys
danny z
danny_z Have you noticed parentheses around `!input.empty()` in my example? Also, why `8.` - is it float?
Michael Krelin - hacker
I added the parentheses just now, and the . was a typo. sorry this is taking forever I've been coding all morning im starting to slip up with small mistakes. Here's a question: should this be a while or do while loop. I had this in a do while and I'm thinking I really shouldn't have
danny z
Well, it's a question for you, not for me ;-) It is `while()` if you have input before you enter the loop and want to handle the situation where the first line is already invalid. It is a `do-while()` if you only know exit condition by the time you're done with iteration. It does look more like `while()` or even `for()` (depending on your input machinery) to me.
Michael Krelin - hacker
My original thought was:do{take input} while (input isnt right)yet with the condition i just posted, this is still not working ;[
danny z
`do { take input } while (input isn't right)` meaning you want to skip invalid lines first? In this case you, of course, need to revert condition: `input.empty() || input.length()>8 || input.find_first_not_of("01")!=std::string::npos`
Michael Krelin - hacker
Thanks so much. After taking a nice break after coding all day this mistake was so clear it wouldn't have warranted a stackoverflow post, but thanks again.
danny z
;-) you're welcome.
Michael Krelin - hacker
A: 

I would suggest you to use std::bitset

if you string is always contain bits i.e. only **0 and 1 you u can use**

bitset<8> bitOfStr(string("01011")); // initialize from string bitset<8> bitOfStr(YourStringhere);

Note: If string contain anything else then 0/1 before length of bitset it will through an exception.

on bitset You can perform your rest of operations more efficiently .

sat