Help with some binary issues

This is a discussion on Help with some binary issues within the C++ Programming forums, part of the General Programming Boards category; My instructor assigned my group to make a simple program to convert any binary into its one's complement by using ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    10

    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;
    }
    Last edited by MoonKami; 10-12-2008 at 09:06 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I suggest you read up on arrays first. The code you posted doesn't make any sense.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    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.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Well can you loop through the characters of the string, and test each character in the string?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    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...

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    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. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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;
    }

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    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
    Last edited by MoonKami; 10-13-2008 at 12:36 AM.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    Thanks again. But are arrays used that often? I wouldn't be surprised especially with how confusing my instructor is...

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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();
    }
    Last edited by Raigne; 10-13-2008 at 12:52 AM.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    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;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-30-2008, 12:10 AM
  2. Binary Converter - Various stupid string issues :p
    By polarpete in forum C++ Programming
    Replies: 7
    Last Post: 08-21-2005, 02:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21