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. 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)
    {
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Why don't you mention exactly what issues you are having. Regarding the conversion of a float to binary, how do you want to present this? In two's complement?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You may want to post this in the C++ forum, since it's not C.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Hutto, TX
    Posts
    12
    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
    ******

    Here is function I was trying to make.
    Code:
    // 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");
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by angel48797 View Post
    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
    ******
    That's nothing even close to what you have below:
    Quote Originally Posted by angel48797 View Post
    Here is function I was trying to make.
    Code:
    // Function to convert a float integer into binary 
    float *printFloat (int)
    This function ... assuming it would even compile ... takes an int (which has no name) as an argument, and returns a pointer to a float. Why?
    Quote Originally Posted by angel48797 View Post
    Code:
    {    
         int n,bin[100],i,j;    
         printf("Enter your choice: \n");    
         scanf("%d",&n);
    Then you read an integer.
    Quote Originally Posted by angel48797 View Post
    Code:
         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");
    }
    None of this is going to work, because it's an integer.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Issues
    By GoatMafioso in forum C Programming
    Replies: 6
    Last Post: 04-17-2010, 10:22 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