Columns

So I'm writing a code that outputs ASCII code and I want it to be in columns.
Right now my output is like this,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
32  
33 !
34 "
35 #
36 $
37 %
etc
        43 +
        44 ,
        45 -
        46 .
        47 /
        48 0
        etc 

But I want it to be like this,
1
2
3
4
5
6
7
32        43 +
33 !      44 ,
34 "      45 -
35 #      46 .
36 $      47 /
37 %      48 0
etc       etc 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>   
#include <iomanip>    

using namespace std;
int main ()
{
    int a;
    for(a=32;a<=42;++a)
    {
        cout << a << setw(2) << static_cast<char>(a) << endl;
    }
    for(a=43;a<=53;++a)
    {
        cout << setw(10) << a << setw(2) << static_cast<char>(a) << endl;
    }
    return 0;
}
Last edited on
If you want "33 !" to be on the same row as "44 ," you need to come up with a way to output them, one after the other, without endl or other output in between.
You may find some ideas here:
http://www.cplusplus.com/forum/beginner/202829/
Thank you Chervil, that help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

inline char ascii(unsigned short int ascii_code = 0)
{
    return static_cast<char>(ascii_code);
}

int main()
{
    constexpr unsigned short int SECOND = 11;

    for (unsigned short int a = 32; a != 38; ++a)
    {
        const unsigned short int NEXT = a + SECOND;

        std::cout << a << ' ' << ascii(a) << '\t' << NEXT << ' ' << ascii(NEXT) << '\n';
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.