Converting a string into binary using functions

So my class assignment we had to convert a string to a set of binary. My original code is as follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
   	//Declaring
	string msg;
	int binn, e, num;
	char p, u;//first half
        p = 0;//Placement in string
	   
	cout << "Enter a message: " << endl;
	cin >> msg;
	cout << "The binary value is: ";
	   
	while (p < msg.length()) {
	      u = msg[p];
	      num = (int) u;
	      e = 7;
	      while (e >= 0) {
		 binn = num/pow(2, e);
		 num -= pow(2, e)*binn;
		 cout << binn;
		 e -= 1;
	      }
	      p += 1;
	 }
	   
	  ...// rest of my program cause we had to do it in reverse too 


However, my teacher reassigned it so we have to include functions. I did it as follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

//FUNCTIONS
string str_to_bin(string str);

int main() {
   string msg, bin;

   cout << "Enter a message: " << endl;
   cin >> msg;
   cout << str_to_bin(msg);

   cout << "Enter a set of binary: " << endl;
   cin >> bin;

   return 0;
}

string str_to_bin(string msg) {
   char plc;
   int num, e, binn, p;
   for(p = 0; p < msg.length(); p++){
      plc = msg[p];
      num = (int) plc;
      for(e = 7; e >= 0; e--) {
	 binn = num/pow(2, e);
	 num -= pow(2, e)*binn;
      }
      //return binn; not sure what to do after this
      //cout << binn;
   }
   //return 0;
}


Our prof wanted us to do it in 10 lines. And I can't figure out how to output the binary since I can't just loop it. I was thinking of using a string but I'm not sure how I would go about that either since I've yet to work with them. Help.
> Our prof wanted us to do it in 10 lines.

Twenty seven lines.
Eighteen lines excluding blank and comment only lines.
Ten lines excluding blank and comment only lines, lines with only a single brace and #include directives.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <bitset>
#include <limits>

std::string str_to_bin( std::string str )
{
    // http://en.cppreference.com/w/cpp/utility/bitset
    using bits = std::bitset< std::numeric_limits< unsigned char >::digits > ;

    std::string result ;

    // http://en.cppreference.com/w/cpp/utility/bitset/to_string
    for( unsigned char c : str ) result += bits(c).to_string() ;

    return result ;
}

int main()
{
    std::string str ;

    std::cout << "enter a message: " ;
    std::getline( std::cin, str ) ;

    std::cout <<  str_to_bin(str) << '\n' ;
}
There is an argument I've observed several times.

Most likely he meant 10 statements, i.e. 10 semicolons. This is usually the same as lines, but it raises several points:

The language allows multiple statements without new lines
The language permits mostly free use of white-space
The language permits mostly free use of comments
Do macros count? Intuitively it seems they should, but n a header file would the include guards count? How about conditional compilation statements, pragmas, constants and header file includes
What exactly then is a line?

"Lines" is a tad vague from a programmers perspective, your coding style could significantly affect the number of lines of text you type.

If you're brave enough, and don't mind failing the course, challenge him on the precise use of "lines" and do it in 5 lines of text like so:

1
2
3
4
5
#include <iostream> 
#include <string> 
#include <bitset> 
#include <limits> 
std::string str_to_bin( std::string str ){   using bits = std::bitset< std::numeric_limits< unsigned char >::digits > ;    std::string result ;     for( unsigned char c : str ) result += bits(c).to_string() ;     return result ; } int main(){    std::string str ;    std::cout << "enter a message: " ;    std::getline( std::cin, str ) ;    std::cout <<  str_to_bin(str) << '\n' ;}


Please never style your code like this for real, only when you're trying to be a pedantic wise ass.

Topic archived. No new replies allowed.