outputting ascii table in C

0 votes
#include <iostream>
#include <iomanip>
using namespace std;

class Ascii_output {
public:
    void run() {
        print_ascii();
    }
private:
    void print_ascii() {
        int i, j;                                                           // i is         used to print the first element of each row
                                                                        // j is used to print subsequent columns of a given row
    char ch;                                                            // ch stores the character which is to be printed
    cout << left;

    for (i = 32; i < 64; i++) {                                         // 33 rows are printed out (64-32+1)
        ch = i;
        if (ch != '\n')                                                 // replaces any newline printouts with a blank character
            cout << setw(3) << i << " " << setw(6) << ch;
        else
            cout << setw(3) << i << " " << setw(6);

        for (j = 1; j < 7; j++) {                                       // decides the amount of columns to be printed out, "j < 7" dictates this
            ch += 32*j;                                                 // offsets the column by a multiple of 32
            if (ch != '\n')                                             // replaces any newline printouts with a blank character
                cout << setw(3) << i+(32*j) << " " << setw(6) << ch;
            else
                cout << setw(3) << i+(32*j) << " " << setw(6);
        }

        cout << endl;
    }
    }
};

code:

#include <iostream>
#include <iomanip>
using namespace std;

class Ascii_output {
public:
    void run() {
        print_ascii();
    }
private:
    void print_ascii() {
        int i, j;                                                           // i is         used to print the first element of each row
                                                                        // j is used to print subsequent columns of a given row
    char ch;                                                            // ch stores the character which is to be printed
    cout << left;

    for (i = 32; i < 64; i++) {                                         // 33 rows are printed out (64-32+1)
        ch = i;
        if (ch != '\n')                                                 // replaces any newline printouts with a blank character
            cout << setw(3) << i << " " << setw(6) << ch;
        else
            cout << setw(3) << i << " " << setw(6);

        for (j = 1; j < 7; j++) {                                       // decides the amount of columns to be printed out, "j < 7" dictates this
            ch += 32*j;                                                 // offsets the column by a multiple of 32
            if (ch != '\n')                                             // replaces any newline printouts with a blank character
                cout << setw(3) << i+(32*j) << " " << setw(6) << ch;
            else
                cout << setw(3) << i+(32*j) << " " << setw(6);
        }

        cout << endl;
    }
    }
};

output:

 
enter image description here

Why do I not get a properly indented output and weird characters at the values 96 - 255 ?

Jun 13, 2022 in C++ by Nicholas
• 7,760 points
1,082 views

1 answer to this question.

0 votes

This line doesn't do the right thing:

ch += 32*j;

You want to count by 32, that's either

ch += 32;

or

ch = i + 32*j;

I strongly recommend making the numeric and character values match during your output. So change

cout << setw(3) << i+(32*j) << " " << setw(6) << ch;

to

cout << setw(3) << int(ch) << " " << setw(6) << ch;
answered Jun 13, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,420 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,158 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
925 views
0 votes
0 answers

How to traverse stack in C++?

Is traversing std::stack possible in C++? It is not possible to traverse using the following method.  Because there is no member end in std::stack. std::stack<int> foo; // .. for (__typeof(foo.begin()) it = foo.begin(); ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
2,228 views
0 votes
0 answers

Programming slim C++ programs (like uTorrent) for Windows

What methods do you recommend for writing ...READ MORE

May 13, 2022 in Others by Kichu
• 19,040 points
403 views
0 votes
1 answer

casting int to char using C++ style casting [duplicate]

Even though the precision is lost, yo ...READ MORE

answered Jun 13, 2022 in C++ by Damon
• 4,960 points
19,468 views
0 votes
1 answer

Has Windows an integrated built-in C/C++ compiler package?

Microsoft does not offer a compiler or ...READ MORE

answered Jun 14, 2022 in C++ by Damon
• 4,960 points
1,499 views
0 votes
0 answers

How to copy turbo c++ output?

How can I replicate the output of Turbo C++?  I had already Googled the issue, but in vain.  It suggests to either print scrn and paste or right click, select all, and paste.  I tried both, but neither worked.  The issue is that it simply copies what is on the current screen.  But I want the entire screen from the start.  (Alt+printscrn is also ineffective).  What should I do in this situation? printScrn Alt+printScrn markall None of them are operational!! I can't assist you if you require this archaic technique of programming for whatever reason, but I'd want to find a solution.  I tried forwarding the output stream to the file in this manner, but it did not work. #include<iostream.h> #include<conio.h> #include<stdlib.h> const int max=50; class dequeue{ int dq[max],r,f,c,x,i; public: dequeue(); void insertRear(); void insertFront(); void ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
1,456 views
0 votes
1 answer

Declare abstract class in c++

An abstract class is one that is intended to be used as a base class .  At least one pure virtual function exists in an abstract class.  A pure virtual function is declared in the class declaration by using a pure specifier (= 0) in the declaration of a virtual member function. Here is an example of an abstract class: class AB { public: virtual void f() ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
573 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
855 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP