【发布时间】:2012-11-06 14:01:42
【问题描述】:
代码:
#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;
}
}
};
输出:
为什么在值 96 - 255 处我没有得到正确缩进的输出和奇怪的字符?
【问题讨论】:
-
也许从正确缩进代码开始。
-
ASCII 表从 0 到 127。
-
请注意,这不会在不使用 ASCII 的系统上打印出 ASCII。
-
您的代码还有其他问题...字符
a是 ASCII 97,而不是 161。我猜这是您问题的根源,您打印出一个数字,但大多数的时间不是那个数字的正确字母。 -
我还建议您使用
std::isprint之类的东西来确保字符不可打印。例如,ASCII 127 不可打印,可能会导致您的格式错误。
标签: c++ windows char ascii iostream