Converting a decimal integer number to binary form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tukeind
    New Member
    • Apr 2007
    • 10

    #1

    Converting a decimal integer number to binary form

    Hello,

    I am receiving the following error: error C2065: 'to_binary' : undeclared identifier
    while running the code below.

    If anyone can help I'll appreciate it?

    Thank you,
    Tukeind

    // File: fractal.cxx
    // A demonstration program for the random_fractal function from Chapter 9.

    #include <cassert> // Provides assert
    #include <cstdlib> // Provides EXIT_SUCCESS
    #include <iostream.h> // Provides cin, cout
    #include "useful.h" // From Appendix H. Provides random_real, display
    //using namespace std;

    // PROTOTYPE for the function used in this demonstration program.
    void random_fractal
    (double left_height, double right_height, double width, double epsilon);
    // Precondition: width and epsilon are both positive.
    // Postcondition: The function has generated a random fractal from a line
    // segment. The parameters left_height and right_height are the heights of the
    // two endpoints of the line segment, and the parameter width is the segment's
    // width. The generation of the random fractal stops when the width of the
    // line segments reaches epsilon or less.
    // Method of displaying the output: The height of the right endpoint of
    // each line segment in the random fractal is displayed by calling the
    // function display(...).

    int main( )
    {
    cout << "The decimal number 10 equals ";
    to_binary( 10 );
    cout << " binary." << endl;

    cout << "The decimal number 100 equals ";
    to_binary( 100 );
    cout << " binary." << endl;

    cout << "The decimal number 1000 equals ";
    to_binary( 1000 );
    cout << " binary." << endl;

    cout << "The decimal number 10000 equals ";
    to_binary( 10000 );
    cout << " binary." << endl;

    return EXIT_SUCCESS;
    }

    void to_binary(int number)
    {
    if( number == 10)
    return;

    to_binary( number / 2);

    cout << number % 2;
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    You have to add the rprototype to the to_binary function before the main function.
    Code:
    #include <cassert> // Provides assert
    #include <cstdlib> // Provides EXIT_SUCCESS
    #include <iostream.h> // Provides cin, cout
    #include "useful.h" // From Appendix H. Provides random_real, display
    //using namespace std;
    void to_binary(int number);
    // PROTOTYPE for the function used in this demonstration program.
    void random_fractal
    (double left_height, double right_height, double width, double epsilon);
    // Precondition: width and epsilon are both positive.
    // Postcondition: The function has generated a random fractal from a line
    // segment. The parameters left_height and right_height are the heights of the
    // two endpoints of the line segment, and the parameter width is the segment's
    // width. The generation of the random fractal stops when the width of the
    // line segments reaches epsilon or less. 
    // Method of displaying the output: The height of the right endpoint of
    // each line segment in the random fractal is displayed by calling the
    // function display(...).
    
    int main( )
    {
    cout << "The decimal number 10 equals ";
    to_binary( 10 );
    cout << " binary." << endl;
    
    cout << "The decimal number 100 equals ";
    to_binary( 100 );
    cout << " binary." << endl;
    
    cout << "The decimal number 1000 equals ";
    to_binary( 1000 );
    cout << " binary." << endl;
    
    cout << "The decimal number 10000 equals ";
    to_binary( 10000 );
    cout << " binary." << endl;
    
    return EXIT_SUCCESS;
    }
    
    void to_binary(int number)
    {
    if( number == 10)
    return;
    
    to_binary( number / 2);
    
    cout << number % 2;
    }
    That will solve the issue.
    Thanks
    Raghuram

    Comment

    • Tukeind
      New Member
      • Apr 2007
      • 10

      #3
      Converting a decimal integer number to binary form

      Thank you that worked...earlie r i had void to_binary(int) as my prototype
      ...should had written it like you have it...Thanks a lot!!

      Originally posted by gpraghuram
      Hi,
      You have to add the rprototype to the to_binary function before the main function.
      Code:
      #include <cassert> // Provides assert
      #include <cstdlib> // Provides EXIT_SUCCESS
      #include <iostream.h> // Provides cin, cout
      #include "useful.h" // From Appendix H. Provides random_real, display
      //using namespace std;
      void to_binary(int number);
      // PROTOTYPE for the function used in this demonstration program.
      void random_fractal
      (double left_height, double right_height, double width, double epsilon);
      // Precondition: width and epsilon are both positive.
      // Postcondition: The function has generated a random fractal from a line
      // segment. The parameters left_height and right_height are the heights of the
      // two endpoints of the line segment, and the parameter width is the segment's
      // width. The generation of the random fractal stops when the width of the
      // line segments reaches epsilon or less. 
      // Method of displaying the output: The height of the right endpoint of
      // each line segment in the random fractal is displayed by calling the
      // function display(...).
      
      int main( )
      {
      cout << "The decimal number 10 equals ";
      to_binary( 10 );
      cout << " binary." << endl;
      
      cout << "The decimal number 100 equals ";
      to_binary( 100 );
      cout << " binary." << endl;
      
      cout << "The decimal number 1000 equals ";
      to_binary( 1000 );
      cout << " binary." << endl;
      
      cout << "The decimal number 10000 equals ";
      to_binary( 10000 );
      cout << " binary." << endl;
      
      return EXIT_SUCCESS;
      }
      
      void to_binary(int number)
      {
      if( number == 10)
      return;
      
      to_binary( number / 2);
      
      cout << number % 2;
      }
      That will solve the issue.
      Thanks
      Raghuram
      Last edited by Tukeind; Apr 20 '07, 04:41 AM. Reason: to place body of response at the top

      Comment

      Working...