By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

Converting a decimal integer number to binary form

P: 10
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;
}
Apr 20 '07 #1
Share this Question
Share on Google+
2 Replies


gpraghuram
Expert 100+
P: 1,275
Hi,
You have to add the rprototype to the to_binary function before the main function.
Expand|Select|Wrap|Line Numbers
  1. #include <cassert> // Provides assert
  2. #include <cstdlib> // Provides EXIT_SUCCESS
  3. #include <iostream.h> // Provides cin, cout
  4. #include "useful.h" // From Appendix H. Provides random_real, display
  5. //using namespace std;
  6. void to_binary(int number);
  7. // PROTOTYPE for the function used in this demonstration program.
  8. void random_fractal
  9. (double left_height, double right_height, double width, double epsilon);
  10. // Precondition: width and epsilon are both positive.
  11. // Postcondition: The function has generated a random fractal from a line
  12. // segment. The parameters left_height and right_height are the heights of the
  13. // two endpoints of the line segment, and the parameter width is the segment's
  14. // width. The generation of the random fractal stops when the width of the
  15. // line segments reaches epsilon or less. 
  16. // Method of displaying the output: The height of the right endpoint of
  17. // each line segment in the random fractal is displayed by calling the
  18. // function display(...).
  19.  
  20. int main( )
  21. {
  22. cout << "The decimal number 10 equals ";
  23. to_binary( 10 );
  24. cout << " binary." << endl;
  25.  
  26. cout << "The decimal number 100 equals ";
  27. to_binary( 100 );
  28. cout << " binary." << endl;
  29.  
  30. cout << "The decimal number 1000 equals ";
  31. to_binary( 1000 );
  32. cout << " binary." << endl;
  33.  
  34. cout << "The decimal number 10000 equals ";
  35. to_binary( 10000 );
  36. cout << " binary." << endl;
  37.  
  38. return EXIT_SUCCESS;
  39. }
  40.  
  41. void to_binary(int number)
  42. {
  43. if( number == 10)
  44. return;
  45.  
  46. to_binary( number / 2);
  47.  
  48. cout << number % 2;
  49. }
  50.  
That will solve the issue.
Thanks
Raghuram
Apr 20 '07 #2

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

Hi,
You have to add the rprototype to the to_binary function before the main function.
Expand|Select|Wrap|Line Numbers
  1. #include <cassert> // Provides assert
  2. #include <cstdlib> // Provides EXIT_SUCCESS
  3. #include <iostream.h> // Provides cin, cout
  4. #include "useful.h" // From Appendix H. Provides random_real, display
  5. //using namespace std;
  6. void to_binary(int number);
  7. // PROTOTYPE for the function used in this demonstration program.
  8. void random_fractal
  9. (double left_height, double right_height, double width, double epsilon);
  10. // Precondition: width and epsilon are both positive.
  11. // Postcondition: The function has generated a random fractal from a line
  12. // segment. The parameters left_height and right_height are the heights of the
  13. // two endpoints of the line segment, and the parameter width is the segment's
  14. // width. The generation of the random fractal stops when the width of the
  15. // line segments reaches epsilon or less. 
  16. // Method of displaying the output: The height of the right endpoint of
  17. // each line segment in the random fractal is displayed by calling the
  18. // function display(...).
  19.  
  20. int main( )
  21. {
  22. cout << "The decimal number 10 equals ";
  23. to_binary( 10 );
  24. cout << " binary." << endl;
  25.  
  26. cout << "The decimal number 100 equals ";
  27. to_binary( 100 );
  28. cout << " binary." << endl;
  29.  
  30. cout << "The decimal number 1000 equals ";
  31. to_binary( 1000 );
  32. cout << " binary." << endl;
  33.  
  34. cout << "The decimal number 10000 equals ";
  35. to_binary( 10000 );
  36. cout << " binary." << endl;
  37.  
  38. return EXIT_SUCCESS;
  39. }
  40.  
  41. void to_binary(int number)
  42. {
  43. if( number == 10)
  44. return;
  45.  
  46. to_binary( number / 2);
  47.  
  48. cout << number % 2;
  49. }
  50.  
That will solve the issue.
Thanks
Raghuram
Apr 20 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.