错误之二:程序清单3.6 morechar.cpp

C++字符与整型对比
本文通过一个简单的C++程序实例,展示了字符类型(char)和整型(int)之间的转换及操作过程。通过将字符赋值给整型变量并显示其ASCII码值,以及通过对字符进行加一操作来改变其对应的ASCII码值,直观地展现了两种数据类型的内在联系。

书籍名称:C++ Primer Plus(中文第五版)

错误说明:char ch 应为 char c

Code:
  1. //morechar.cpp--the char type ande int type contrasted   
  2.   
  3. #include <iostream>   
  4. int main()   
  5. {   
  6.   using namespace std;   
  7.   char ch='M';      //assign ASCII   
  8.   int i=ch;              //store same code in an int   
  9.   cout<<"The ASCII code for "<<ch<<" is"<< i <<endl;   
  10.   
  11.   cout<<"Add one to the character code:"<<endl;   
  12.   ch=ch+1;         //change character code in c   
  13.   i=ch;                //save new character code in i   
  14.   cout<<"The ASCII code for "<<ch <<" is"<<i<<endl;   
  15.          //using the cout.put() member function to display a char   
  16.   cout<<"Displaying char ch using cout.put(ch):";   
  17.   cout.put(ch);   
  18.         //using cout.put() to display a char constant   
  19.   cout.put(ch);   
  20.         //using cout.put() to display a char constant   
  21.   cout.put('!');   
  22.   cout<<endl<<"Done"<<endl;   
  23.   return 0;     
  24.   
  25. }   

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值