Help - Table in this

Hello i did the conversion but i still have more questions. Because it doesnt fit that topic anymore i create a new one.

So the problem is simple. It prints out a table where when it hits f it creates a new line, but how do i make it print out in a nicer table, i tried with setw or setfill but it just doesnt work, and if there are any other problems please let me know

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int noob;
    do
    {
    int  a, b=1, i=1, dec;
    do
    {
        dec=i*b;
            do
            {
            a=dec%16;
            dec/=16;
            if(a<=9&&a!=0) cout<< a;
            else if(a==10) cout<< 'a';
            else if(a==11) cout<< 'b';
            else if(a==12) cout<< 'c';
            else if(a==13) cout<< 'd';
            else if(a==14) cout<< 'e';
            else if(a==15) cout<< 'f';
            }while(a>0);
            cout<<" ";
        i++;
        if(i==17)
        {
        cout<<endl;
         b++;
         i=1;
        }
    }while(b<17);
    cout << "Do you wish to continue(0=No, 1=yes)"<<endl;
    cin >> noob;
    }while(noob==1);

}


So simply how do i setw for the integer and char i get from

1
2
3
4
5
6
7
8
9
10
11
12
            do
            {
            a=dec%16;
            dec/=16;
            if(a<=9&&a!=0) cout<< a;
            else if(a==10) cout<< 'a';
            else if(a==11) cout<< 'b';
            else if(a==12) cout<< 'c';
            else if(a==13) cout<< 'd';
            else if(a==14) cout<< 'e';
            else if(a==15) cout<< 'f';
            }while(a>0);
Last edited on
You're still having issues with reversals on any two-(or more) digit (hex) number. Your do{} while (a > 0) is printing the least significant digit first, then on to significant.

You can see why:

Assume dec is 47. ( 2F Hex )
47 % 16 is 15 (hex F), so you print "F"
47 / 16 = 2 and 2 % 16 is 2 so you print "2" which is more significant than the F.

If you know what the highest power of 16 will be in your conversion, you can start there:

47/16 = 2; print 2
47 - (16 * 2) = 15; print 15 ("C")

You probably need to build a string or a char[] to hold your hex digits as you pass through the loop, then cout << setw(4) << (the last value first, then the next, etc).

If you build a string, concatenate the string to your new character rather than the other way around.
Topic archived. No new replies allowed.