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:

Why do I not get a properly indented output and weird characters at the values 96 - 255 ?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With