Trying to make Binary Look Pretty

So the goal of this program was to set a table of different sets of numbers from different bases. When I print out Binary numbers, I wanted to get them spaced like this 10_1101_0011..so that every 4 spots a '_' appears. I could not wrap my head around how and what kind of loop I should have.
I thought that this below would work...ironically it complies in vs2008, however I get this big nasty error message when it runs.
fixed = fixed.insert(4*p,space)
p was going to be part of a count that would tell it basicly where to place this.1*4 = 4 2*4=8 etc...
SO what would be good to achieve this goal of printing out pretty Binary OR why is that nasty error coming out?
Thanks again guys for your time and energy.
-Cheers.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  # include<iostream>
# include<string>
# include<iomanip>
using namespace std;

string numToBinString(int);
string prettyStr(string);


void prtOneRow ( int num )
{
   //The count is located in  main
   cout << right;
	cout << setw(9) <<dec << num << setw(13) << showbase << hex << num << setw(8) << oct << num << setw(11) << numToBinString(num) << endl;
}


string numToBinString( int num )
{
   string str = " ";
   do
   {
		int digit = num%2;
            num = num/2;
      char ch = '0' + digit;
		str = ch + str ;
	}while(num != 0);
	return str ;
}



string prettyStr(string fixed)
{
	
	for(int p = 0; p <35; p++ )
	{
	string space = "_";
	fixed = fixed.insert(4*p,space);//!!! here is Where i need help!!!
	}
	
	return fixed;
}
int scoutsNumber(int num)
{
	int weridNum[]= {64, 128, 256, 512, 1021, 2048, 4096, 8192, 16384, 32768, 65536};
	return weridNum[num];
}


int main()
{
   	
   int n;	
	cout << setfill('=') <<setw(43) << "  " << endl; 
	cout << "12345678901234567890123456789012345678901234567890123456789" << endl;
	cout << setfill(' ') << setw(9) << "decimal" << setw(13) << "hexidecimal" << setw(8) << "octal" << setw(11) << "binary" << setfill( '=' ) << endl;
for ( int i = 0; i < 34; i++ )
 {
	 cout << setw(42) << " " << setfill(' ') << endl;
        prtOneRow(i);
 }

	cout << setfill('~') <<setw(58) << "  " << endl; 
	cout << setfill(' ') << setw(9) << "decimal" << setw(13) << "hexidecimal" << setw(11) << "octal" << setw(23) << "binary" << setfill( ' ' ) << endl;
	cout << setfill('~') <<setw(58) << "  " << setfill(' ') << endl;
for(n = 0; n < 11; n++)
{
	int n0 = 0;
	n0= n0 + scoutsNumber(n);
		
	int  n1,n2,n3;
	n1= n0 - 1;
	n2= n0;
	n3= n0 + 1;
		// The 3 lines of code bellow more than likely code have been place some how in a loop of ther own.
		// however I could not figure out how to do this to clean this part up.
	cout << setw(9) <<dec << n1 << setw(13) << showbase << hex << n1 << setw(11) << oct << n1 << setw(24) << numToBinString(n1) << endl;
	cout << setw(9) <<dec << n2 << setw(13) << showbase << hex << n2 << setw(11) << oct << n2 << setw(24) << numToBinString(n2) << endl;
	cout << setw(9) <<dec << n3 << setw(13) << showbase << hex << n3 << setw(11) << oct << n3 << setw(24) << numToBinString(n3) << endl;
}


cout << "HEllo"<< endl;
cout << numToBinString( 601 ) << endl;
	
cout << prettyStr(numToBinString(1231)) << endl;
cout << prettyStr(" Hello world") << endl;//checking how far the code as ran 
Last edited on
Why do the assign? The string object is modified. No need to assign it to itself.
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
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
#include <limits>
#include <bitset>
#include <string>

int scoutsNumber( unsigned int num )
{
    // static constexpr int weridNum[]= {64, 128, 256, 512, 1021, 2048, 4096, 8192, 16384, 32768, 65536};
	return num == 4 ? 1021 : 1u << (num+6) ;
}

std::string pretty_string( std::string str )
{
    if( str.empty() ) return "" ;

    std::string pretty( 1, str.front() ) ;
    for( std::size_t i = 1 ; i < str.size() ; ++i )
    {
        pretty += str[i] ;
        if( i%4 == 3 ) pretty += ' ' ;
    }

    return pretty ;
}

constexpr int digits = std::numeric_limits<unsigned int>::digits ;

std::string to_bin( int n ) { return pretty_string( std::bitset<digits>(n).to_string() ) ; }

int main()
{

    const auto dec = [] ( std::ostream& stm ) -> std::ostream& { return stm << std::dec << std::setw( std::numeric_limits<int>::digits10 + 3 ) ; };
    const auto oct = [] ( std::ostream& stm ) -> std::ostream& { return stm << std::oct << std::setw( (digits+2) / 3 + 2 ) ; };
    const auto hex = [] ( std::ostream& stm ) -> std::ostream& { return stm << std::hex << std::setw( (digits+3) / 4 + 2 ) ; };
    const auto bin = [] ( std::ostream& stm ) -> std::ostream& { return stm << std::setw( to_bin(0).size() + 5 ) ; };

    //std::cout << std::showbase ;
    for( int i = 0 ; i < 11 ; ++i )
    {
        const int n0 = scoutsNumber(i);
        for( int j : { -1, 0, 1 } )
        {
            const int n = n0 + j ;
            std::cout << dec << n << oct << n << hex << n << bin << to_bin(n) << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/37a8d1da6d880f48
Topic archived. No new replies allowed.