# Help with some binary issues

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-12-2008
MoonKami
Help with some binary issues
My instructor assigned my group to make a simple program to convert any binary into its one's complement by using arrays. I know how to do the one's complement; however, I do not have any idea as to what to do or what arrays are. One issue is that the instructor doesn't make sense or when he does, he mumbles so I can't hear anyways.
He gave a piece of starting code but I haven't gotten far. I tried using char instead of strings but that didn't get me anywhere. Any help would greatly be appreciated.

Code:

```/* Assignment        Develop a function that will perform one's complement of a given binary number stored in an array bin. */ #include <iostream> #include <string> using namespace std; void OnesComp(string & BIN) {         BIN[0];         return; } int main () {         string BIN;         cout << "Type in a binary number" << endl;         cin >> BIN; //Code for conversion         cout << "The binary number is: " << BIN << endl;         OnesComp(BIN);         cout << "The one's complement is: " << BIN << endl;         cin >> BIN;         return 0; }```
• 10-12-2008
cyberfish
I suggest you read up on arrays first. The code you posted doesn't make any sense.
• 10-12-2008
MoonKami
I'll read up on them more. I kind of know what they are and I also know that the code doesn't make sense. This is what my instructor gave to my class and, like I said, he doesn't make sense most of the time
• 10-12-2008
Salem
> One issue is that the instructor doesn't make sense or when he does, he mumbles so I can't hear anyways.
Well you need to fix that, otherwise you're not going to get very far.

First question is, can you do ones-complement on paper?
If not, then do some reading and make sure you understand the process of calculating the ones-complement of a number.
• 10-12-2008
MoonKami
That is true, but he is the only one of two instructors teaching it. I can do one's complement easily enough. 11111111 >> 00000000 or 10101010 >> 01010101. Just replace one's with zero's and vice versa. But the issue I'm having is being able to get elements to be individually replaced by a replace or replace_if.
• 10-12-2008
Salem
Well can you loop through the characters of the string, and test each character in the string?
• 10-12-2008
MoonKami
Like using bool with if statements? I'm trying to do that right now with some code I got from a book but it's hard since I'm just a noob at all this and the code has vectors and stuff... Mainly things I haven't covered yet or don't understand at all...
• 10-12-2008
Raigne
if your using std::string then you should use an iterator
Code:

```std::string::iterator strIter; for ( strIter = yourstr.begin(); strIter != yourstr.end(); ++strIter ) {   //(*strIter) will access the character at the iterators position }```
I didnt say to much did I?
• 10-13-2008
MoonKami
I do have std namespace but I don't know what an iterator is or how to use for statements. But this is what I've gotten so far which isn't that far...

Code:

```#include <iostream> #include <string> #include <algorithm> using namespace std; bool ZeroOrOne (const int & nNum) {         return ((nNum &#37;2) ==0); } int main () {         string IMSOCONFUSED;         char BINARY [8];         cout << "Input a binary number" << endl;         cin >> BINARY[8];          cout << "The initial binary number was: " << endl;         std::string::iterator strIter;         for ( strIter = BINARY.begin(); strIter != BINARY.end(); ++strIter )                 /* Have no clue how to use these also getting "must have class/ structure/union" error messages */         {                   replace_if (BINARY[8], BINARY[8], ZeroOrOne, 1);                 /* Have no clue how to use these also getting "must have class/ structure/union" error messages */         }         cout << endl << "The one's complement is now:" << BINARY[8] << endl;         cin >> IMSOCONFUSED;         return 0; }```
• 10-13-2008
Raigne
I am gonna get shunned for this...
Code:

```#include <iostream> #include <string> int main () {         std::string binary;         cout << "Input a binary number" << endl;         std::getline(std::cin, binary);         cout << "The initial binary number is: " << binary << endl;         std::string::iterator strIter;         for ( strIter = binary.begin(); strIter != binary.end(); ++strIter )         {                 if ( (*strIter) == '0' )                         (*strIter) = '1';                 else if ( (*strIter) == '1' )                         (*strIter) = '0';         }         cout << endl << "The one's complement is:" << binary << endl;         std::cin.get();         return 0; }```
• 10-13-2008
MoonKami
Wow. Thanks a lot Raigne. You could have tried to explain it to me instead. It works but I'm supposed to use arrays unless for statements are arrays. Now I feel like I cheated... I guess I'll use this as a template while I try to figure out how to use arrays for it instead. But thanks alot Raigne
• 10-13-2008
Raigne
Well in that case it isn't an array. It iterates (loops through) the string, and modifies each char. If you are suppose to use arrays that will not work.

I personally haven't used arrays in a long time, but I will try to come up with something for you, so I can explain how your "suppose" to do it with arrays.
• 10-13-2008
MoonKami
Thanks again. But are arrays used that often? I wouldn't be surprised especially with how confusing my instructor is...
• 10-13-2008
Raigne
Okay, here is the same thing using an array. Commented.

Code:

```#include <iostream> int main() {         //char array 0-255         char buf[256];         std::cout << "Enter a binary number: ";         std::cin >> buf;         std::cin.ignore();         //Loop through each character in the buffer         for ( int i = 0; i != 255; ++i )         {                 //If character at index 'i' is '\0' then we know it is the end of the string                 if ( buf[i] == '\0' )                         break;                 //self explanatory                 if ( buf[i] == '1' )                         buf[i] = '0';                 else if ( buf[i] == '0' )                         buf[i] = '1';                 else                 {                         //Display a message and exit if any character besides '1' or '0' are found                         std::cout << "Error: Buffer contains invalid data" << std::endl;                         std::cin.get();                         return 1;                 }         }         //Output the result         std::cout << "One's Complement: " << buf << std::endl;         std::cin.get(); }```
Generally with C++ you can avoid using arrays most of the time, but it is still good to know about them.

Feel free to ask if there is anything there that you dont understand.

Edit: This is the exact same program just using std::string, and std::string::iterator instead of a char array
Code:

```int main() {         std::string buf;         std::string::iterator iter;         std::cout << "Enter a binary number: ";         std::getline(std::cin, buf);         //Loop through each character in the buffer         for ( iter = buf.begin(); iter != buf.end(); ++iter )         {                 //self explanatory                 if ( (*iter) == '1' )                         (*iter) = '0';                 else if ( (*iter) == '0' )                         (*iter) = '1';                 else                 {                         //Display a message and exit if any character besides '1' or '0' are found                         std::cout << "Error: Buffer contains invalid data" << std::endl;                         std::cin.get();                         return 1;                 }         }         //Output the result         std::cout << "One's Compliment: " << buf << std::endl;         std::cin.get(); }```
• 10-13-2008
MoonKami
Well, using the code you gave me, I modified it a little. But I keep on getting an error message saying that I'm corrupting the binary string, or in my case the char. Is there a way for me to get around that issue? Also I don't think I've covered for statements yet. So I tried to avoid using it.

Code:

```#include <iostream> #include <string> using namespace std; int main () {         char binary[7];         cout << "Input a binary number" << endl;         cin >> binary[7];         cout << "The initial binary number is: " << binary << endl;         if ( binary[0] == '0' )                 (binary[0]) = '1';         else if ( (binary[0]) == '1' )                 (binary[0]) = '0'; // Copy and paste the thing 7 more times for 8 digits         cout << endl << "The one's complement is:" << binary << endl;         cin.get();         return 0; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last