该书中有一个好玩的打印
case 1:
#include <iostream>
int main()
{
char ch;
std::cout << "Type, and I shall repeat.\n";
std::cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
std::cout << ch;
else
std::cout << ++ch;
std::cin.get(ch);
}
std::cout << "\nPlease excuse the slight confusion\n";
return 0;
}
执行结果:
因为++ch,所以打印的是输入的下一个字符。
case 2:
使用ch+1代替++ch
#include <iostream>
int main()
{
char ch;
std::cout << "Type, and I shall repeat.\n";
std::cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
std::cout << ch;
else
std::cout << ch + 1;
std::cin.get(ch);
}
std::cout << "\nPlease excuse the slight confusion\n";
return 0;
}
执行结果:
发现输入的是字符,输出的是字符对应的ascii码,原因是加法操作使char型提升为int。
case 3:
打印ch
#include <iostream>
int main()
{
char ch;
std::cout << "Type, and I shall repeat.\n";
std::cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
std::cout << ch;
else
std::cout << ch;
std::cin.get(ch);
}
std::cout << "\nPlease excuse the slight confusion\n";
return 0;
}
结果: