Issues with Binary conversion program

This is a discussion on Issues with Binary conversion program within the C++ Programming forums, part of the General Programming Boards category; I'm having issues linking the main program to the function program/file. Not sure whats causing the error. This is the ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Hutto, TX
    Posts
    12

    Issues with Binary conversion program

    I'm having issues linking the main program to the function program/file. Not sure whats causing the error. This is the first time I'm linking to programs together. Also I'm having a issue designing a function to convert a "float int" into binary. Not sure if the one I input'ed will work right. The output for the float should look like this:

    ******
    Enter a float: 1.115

    The binary representation for 1.115 is: 0011 1111 1000 1110 1011 1000 0101 0010
    ******


    Any help would be appreciated.



    Thank you,
    Robert Brown

    ---------------------BinaryPrograms.cpp---------------------------------------

    Code:
    // Programmer: Robert Brown
    // Project Number: 1
    // Project Desc: Binary Representation of Data
    // Course: Data Structures
    // Date: Sep. 03, 2011
    //
    // Write a small main program (driver) to test your functions. 
    // It should be menu-driven.
    //
    // 1) A function called printChar that takes a character (char) argument. 
    //    The function should print the binary representation of the character 
    //    argument. 
    // 2) A function called printShort that takes a short integer (short) argument. 
    //    The function should print the binary representation of the short argument. 
    // 3) A function called printFloat that takes a single-precision floating-point 
    //    float) argument. The function should print the binary representation of 
    //    the float argument.
    
    #include <iostream>
    
    using namespace std;
    
    // include the PrintFunctions program file
    #include "PrintFunctions.cpp"
    
    int main()
    {
        // declare objects of PrintFunction
        printChar();
    	printShort();
    	printFloat();
    	
        int choice;
        char charValue;
        int  intValue;
        float floatValue;
        
        // Display menu
        do
    	{
    		cout<< "\nMenu: \n";
        	cout<< "\t1 - Print the binary representation of a character.\n";
    	    cout<< "\t2 - Print the binary representation of a short integer\n";
    	    cout<< "\t3 - Print the binary representation of a float\n";
         	cout<< "\t4) Exit program. \n\n";
           cout << "Enter your choice ( 1, 2, 3, or 4): ";
    	   cin>>choice;
    	//
    	if (choice >=1 && choice <=4)
    	{
    	// WHICH CHOICE DID USER SPECIFY?
    	         switch(choice)
    	         {
        // GET INPUT
        case 1: 
    			cout << "\nEnter a character (-9999 to Stop): " << endl;
    			cin>>charValue;
    			while (charValue != -9999)
    			{
                //   
                // THE FUNCTION RECEIVES VALUE.
                printChar(charValue);
    			cout << "Enter a character (-9999 to Stop): "<<endl;
    			cin>>charValue;
    			}
    			break;
    	//
            //		
    	// GET INPUT
    	case 2:
    			cout<<"\nEnter a short: (-9999 to Stop): " << endl;
    			cin>>intValue;
    			while (intValue != -9999)
    			{
                //   
                // THE FUNCTION RECEIVES VALUE.
                printShort(intValue);
    			cout << "Enter a short: (-9999 to Stop): " << endl;
    			cin>>intValue;
    			}
    			break;
    	// 
    	//
    	// GET INPUT
    	case 3:
    			cout<<"\nEnter a float: (-9999 to Stop): " << endl;
    			cin>>floatValue;
    			while (floatValue != -9999)
    			{
                //   
                // THE FUNCTION RECEIVES VALUE.
                printFloat(floatValue);
    			cout << "Enter a float: (-9999 to Stop): " << endl;
    			cin>>floatValue;
    			}
    			break;
    	//
            //
    	// EXIT
    	case 4:
    		cout<<"\n\n\nProgram terminated successfully!\n\n\n";
    		system("pause");
    		break;
            } // end case
        }
    	
    	else if (choice != 4)
    	    {
                  cout << "\nThe valid choices are 1 through 4.\n"; 
                  cout << "\nSelect one of the valid choices.\n";
                  }
            
       } while (choice != 4); // loop for more data
            //
    	//
    	//
    	// DISPLAY TERMINATION MESSAGE
    	cout<<"\n\n\nProgram terminated successfully!\n\n\n";
    
        system ("pause");
        return 0;
    }	// END MAIN
    ---------------------2nd File : PrintFunctions.cpp------------------------------

    Code:
    // Programmer: Robert Brown
    // Project Number: 1
    // Project Desc: Binary Representation of Data
    // Course: Data Structures
    // Date: Sep. 03, 2011
    //
    
    
    // Function to convert a character into binary 
    char *printChar (unsigned char c)
    {
    	static char bin[CHAR_BIT + 1] = {0};
    	int i;
    	for(i=CHAR_BIT - 1;i >= 0;i--)
    	{
    		bin[i]=(c%2) +'0';		
    		c /= 2;		
    	}
    	return bin;
    
    }     
    
    // Function to convert a short integer into binary 
    int *printShort (int number) 
    {
    	int remainder;
    
    	if(number <= 1) 
        {
    		cout << number;
    		return;
    	}
    
    	remainder = number%2;
    	printShort(number >> 1);    
    	cout << remainder;
    }
    
    // Function to convert a float integer into binary 
    float *printFloat (int)
    {    
         int n,bin[100],i,j;    
         printf("Enter your choice: \n");    
         scanf("%d",&n);    
         printf("The Binary representation for %d is: \t",n);    
         for(i=0;n!=0;i++)    
         {        
                  bin[i]=n%2;        
                  n=n/2;    
         }    
         for(j=i-1;j>=0;j--)    
         {        
                  printf("%d",bin[j]);    
         }    
         printf("\n");
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I suspect your belief in the existence of a "float int" is probably the first issue.

    The output you give matches the IEEE representation of 1.15. Now 1.15 is not an int, so your printFloat function shouldn't accept an int; it needs to accept a float (C++ will apply the usual C++ rules for conversion between types; it will not blindly send through the same amount of bits). There doesn't seem to be a point in asking for a number in your printFloat function, since you've already done that in the loop in main. There's also no good reason to all of a sudden be using scanf. I also don't know why you have marked this function as returning a float*, since you make not attempt to return anything whatsoever (this holds true for all of your print functions, actually).

    If you are planning to use bitwise operations, rather than working out the bits yourself, then you will need to do something fancy (I believe reinterpret_cast would work, but I've not tried it) to get C++ to copy the bit pattern from a float to an int, rather than the value itself.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Hutto, TX
    Posts
    12
    Quote Originally Posted by tabstop View Post
    I suspect your belief in the existence of a "float int" is probably the first issue.

    The output you give matches the IEEE representation of 1.15. Now 1.15 is not an int, so your printFloat function shouldn't accept an int; it needs to accept a float (C++ will apply the usual C++ rules for conversion between types; it will not blindly send through the same amount of bits). There doesn't seem to be a point in asking for a number in your printFloat function, since you've already done that in the loop in main. There's also no good reason to all of a sudden be using scanf. I also don't know why you have marked this function as returning a float*, since you make not attempt to return anything whatsoever (this holds true for all of your print functions, actually).

    If you are planning to use bitwise operations, rather than working out the bits yourself, then you will need to do something fancy (I believe reinterpret_cast would work, but I've not tried it) to get C++ to copy the bit pattern from a float to an int, rather than the value itself.
    Yeah I was a little confused by the float. So do you think this would work better?

    Code:
    float *printFloat (float number)
    {
        union
        {
             float input;   // assumes sizeof(float) == sizeof(int)
             int   output;
        }    data;
    
        data.input = number;
    
        std::bitset<sizeof(float) * CHAR_BIT>   bits(data.output);
    
    
        std::cout << bits << std::endl;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't see any reason that wouldn't work (other than what's already mentioned in the comments).

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Hutto, TX
    Posts
    12
    Okay I think I got it now.. only thing left to figure out is how to put a space after every 4 digits on the output. Any Ideas?

    Here is me new code:

    ---------------------BinaryPrograms.cpp---------------------------------------
    Code:
    // Programmer: Robert Brown
    // Project Number: 1
    // Project Desc: Binary Representation of Data
    // Course: Data Structures
    // Date: Sep. 03, 2011
    //
    // Write a small main program (driver) to test your functions. 
    // It should be menu-driven.
    //
    // 1) A function called printChar that takes a character (char) argument. 
    //    The function should print the binary representation of the character 
    //    argument. 
    // 2) A function called printShort that takes a short integer (short) argument. 
    //    The function should print the binary representation of the short argument. 
    // 3) A function called printFloat that takes a single-precision floating-point 
    //    float) argument. The function should print the binary representation of 
    //    the float argument.
    
    #include <iostream>
    #include <bitset>
    using namespace std;
    
    // include the PrintFunctions program file
    #include "PrintFunctions.cpp"
    char printChar ();
    int printShort ();
    float printFloat ();
    
    int main()
    {
    	
        int choice;
        char charValue;
        int  intValue;
        float floatValue;
        
        // Display menu
        do
    	{
    		cout<< "\nMenu: \n";
        	cout<< "\t1 - Print the binary representation of a character.\n";
    	    cout<< "\t2 - Print the binary representation of a short integer\n";
    	    cout<< "\t3 - Print the binary representation of a float\n";
         	cout<< "\t4 - Exit program. \n\n";
           cout << "Enter your choice ( 1, 2, 3, or 4): ";
    	   cin>>choice;
    	//
    	if (choice >=1 && choice <=4)
    	{
    	// WHICH CHOICE DID USER SPECIFY?
    	         switch(choice)
    	         {
        // GET INPUT
        case 1: 
    			printChar();
    			break;
    	//
        //		
    	// GET INPUT
    	case 2:
                printShort ();
    			break;
    	// 
    	//
    	// GET INPUT
    	case 3:
                printFloat ();    
    			break;
    	//
        //
    	// EXIT
    	case 4:
    		cout<<"\n\n\nProgram terminated successfully!\n\n\n";
    		system("pause");
    		break;
            } // end case
        }
    	
    	else if (choice != 4)
    	    {
                  cout << "\nThe valid choices are 1 through 4.\n"; 
                  cout << "\nSelect one of the valid choices.\n";
                  }
            
       } while (choice != 4); // loop for more data
       //
    	
    	//
    	// DISPLAY TERMINATION MESSAGE
    	cout<<"\n\n\nProgram terminated successfully!\n\n\n";
    
        system ("pause");
        return 0;
    }	// END MAIN
    ---------------------2nd File : PrintFunctions.cpp------------------------------

    Code:
    // Programmer: Robert Brown
    // Project Number: 1
    // Project Desc: Binary Representation of Data
    // Course: Data Structures
    // Date: Sep. 03, 2011
    //
    #include <iostream>
    #include <bitset>
    using namespace std;
    
    // Function to convert a character into binary 
    char printChar ()
    {
        char inputChar;
        
        cout << "\nEnter a character: ";
        cin>>inputChar;
        cout << " \n"; 
        cout << "The binary representation for " << inputChar;
    	
        static char bin[CHAR_BIT + 1] = {0};
    	int i;
    	for(i=CHAR_BIT - 1;i >= 0;i--)
    	{
    		bin[i]=(inputChar%2) +'0';		
    		inputChar /= 2;		
    	}
    	cout << " is: " << bin << endl;
    
    }     
    
    // Function to convert a short integer into binary 
    int printShort () 
    {
    	int number;
        cout<<"\nEnter a short: ";
        cin >> number;
        cout << " \n";
        cout << "The binary representation for " << number << " is: ";
            for (int i=15; i>=0; i--) 
            {
                int bit = ((number >> i) & 1);
                cout << bit;
            }
            cout << endl;
    }
    
    // Function to convert a float point into binary 
    float printFloat ()
    {
        float number;
        cout<<"\nEnter a float: ";
        cin>>number;
        cout << " \n";
        
        union
        {
             float input;   // assumes sizeof(float) == sizeof(int)
             int   output;
        }    data;
    
        data.input = number;
    
        std::bitset<sizeof(float) * CHAR_BIT>   bits(data.output);
    
    
        std::cout << "The binary representation for " << number << " is: " << bits << std::endl; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issues with Binary conversion program
    By angel48797 in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2011, 05:44 PM
  2. Help with some binary issues
    By MoonKami in forum C++ Programming
    Replies: 19
    Last Post: 10-13-2008, 02:13 AM
  3. Decimal to Binary Conversion program
    By acidbeat311 in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 10:26 PM
  4. Visual Age C++ to MS VC++ conversion issues?
    By Ruchikar in forum Windows Programming
    Replies: 3
    Last Post: 08-10-2003, 10:54 PM
  5. please help! binary conversion program in c
    By anne in forum C Programming
    Replies: 3
    Last Post: 11-05-2002, 02:09 PM

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