【C++】针对char 字符类型cout的三种输出情况(++ch 和 ch+1 和 ch++)难点加重点
【摘要】 1.++ch,代表字符+1输出,比如输入是a,那么输出是b
#include<iostream>using namespace std;int main(){ char ch; cout << "输入,我将重复\n"; cin.get(ch); while (ch != '.') { if (ch == '\n') cout << ch; else cout ...
1.++ch,代表字符+1输出,比如输入是a,那么输出是b
-
#include<iostream>
-
using namespace std;
-
int main()
-
{
-
char ch;
-
-
cout << "输入,我将重复\n";
-
cin.get(ch);
-
while (ch != '.')
-
{
-
if (ch == '\n')
-
cout << ch;
-
else
-
cout << ++ch;//将ch往后加1输出字符
-
cin.get(ch);
-
}
-
cout <<"OK"<< endl;
-
system("pause");
-
return 0;
-
}
运行结果:
2.ch+1,代表字符+1后ASCII输出,比如输入是a,那么输出是98
-
#include<iostream>
-
using namespace std;
-
int main()
-
{
-
char ch;
-
-
cout << "输入,我将重复\n";
-
cin.get(ch);
-
while (ch != '.')
-
{
-
if (ch == '\n')
-
cout << ch;
-
else
-
cout << ch+1;
-
cin.get(ch);
-
}
-
cout <<"OK"<< endl;
-
system("pause");
-
return 0;
-
}
运行结果:
当然如果想输出原始ASCII值,可以ch+1-1,但是这样不如int(ch)方便
3.ch++,代表字符先输出,后加1,所以输入abc,输出还是abc
-
#include<iostream>
-
using namespace std;
-
int main()
-
{
-
char ch;
-
-
cout << "输入,我将重复\n";
-
cin.get(ch);
-
while (ch != '.')
-
{
-
if (ch == '\n')
-
cout << ch;
-
else
-
cout << ch++;
-
cin.get(ch);
-
}
-
cout <<"OK"<< endl;
-
system("pause");
-
return 0;
-
}
运行结果
有人问,那ch++没有任何作用吗?
回答是否定的!肯定有作用,我们加一句cout输出语句
-
#include<iostream>
-
using namespace std;
-
int main()
-
{
-
char ch;
-
-
cout << "输入,我将重复\n";
-
cin.get(ch);
-
while (ch != '.')
-
{
-
if (ch == '\n')
-
cout << ch;
-
else
-
cout << ch++;
-
cout << " ch++ " << ch <<endl;
-
cin.get(ch);
-
}
-
cout <<"OK"<< endl;
-
system("pause");
-
return 0;
-
}
文章来源: kings.blog.csdn.net,作者:人工智能博士,版权归原作者所有,如需转载,请联系作者。
原文链接:kings.blog.csdn.net/article/details/84578545
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)