注册 登录
编程论坛 C++教室

初学者,输出还像错了。多了个“N"~求解!

Cai_xw1993 发布于 2012-05-03 21:53, 990 次点击
#include <iostream>
using namespace std;
int main ()
{
    char ch = 'M';
    int i = ch;

    cout << "The ASCⅡ code for " << ch << " is "<< i << endl;
    cout << "Add one to the character code;"<< endl;

    ch = ch + 1;
    i = ch;
   
    cout << "The ASCⅡ code for " << ch << " is " << i << endl;
    cout << " Displaying char ch using cout.put(ch): " << cout.put(ch) << endl;
    cout << " Done !" << endl;

    return 0;
}

为什么得出来是:
The ASCⅡ code for M is 77
Add one to the character code;
The ASCⅡ code for N is 78;
N Displaying char ch using cout.put(ch):0047CE94
Done!

不是应该这样吗?:
The ASCⅡ code for M is 77
Add one to the character code;
The ASCⅡ code for N is 78;
Displaying char ch using cout.put(ch):0047CE94
Done!

怎么在Displaying前面会出现一个“N”??

[ 本帖最后由 Cai_xw1993 于 2012-5-3 22:00 编辑 ]
12 回复
#2
tongzhipeng2012-05-03 23:44
我自己百度了一下,因为以前没见过cout.put()的用法
cout.put(ch)的用法 等同于cout << ch;
在你的代码里面ch在+1后由M变成了N,这也就是你那个N产生的原因了,至于为什么在最前面我就不知道了,可能是cout.put()的功能是将字符插入到输出流的最前面。
cout.put(ch) man 了一下,应该是这段

 template<typename _CharT , typename _Traits > basic_ostream< _CharT, _Traits > & std::basic_ostream< _CharT,
       _Traits >::put (char_type __c)
       Simple insertion. Parameters:
           c The character to insert.

       Returns:
           *this

我猜是 返回的是输出流对象的指针,也就是你后面打印的0047CE94
其实这段代码真正的逻辑应该还是这样写的
程序代码:

#include <iostream>
using namespace std;
int main ()
{
    char ch = 'M';
    int i = ch;

    cout << "The ASCⅡ code for " << ch << " is "<< i << endl;
    cout << "Add one to the character code;"<< endl;

    ch = ch + 1;
    i = ch;
   
    cout << "The ASCⅡ code for " << ch << " is " << i << endl;
    cout << " Displaying char ch using cout.put(ch): ";
    cout.put(ch) << endl;
    cout << " Done !" << endl;
   
    return 0;
}


[ 本帖最后由 tongzhipeng 于 2012-5-3 23:46 编辑 ]
#3
rjsp2012-05-04 08:39
对于 cout << " Displaying char ch using cout.put(ch): " << cout.put(ch) << endl;
先执行 cout.put(ch),也就是先输出了一个N,然后将这个put的返回值传给 cout

我觉个简单的例子
int foo( int a ) { return 123; }
foo( foo(3) );
则相当于
int x = foo(3);
foo( x );
#4
Cai_xw19932012-05-04 12:34
回复 3楼 rjsp
其他都懂了。但为什么会打印出0047CE94
#5
Cai_xw19932012-05-04 12:34
回复 2楼 tongzhipeng
懂了~但指针还没学,不懂..
#6
江米条二号2012-05-16 09:38
没看懂
#7
小楼听雨a2012-05-16 21:56
不是ch = ch+1的问题..如果去掉的话  输出照样还得了n!
没搞懂哪里的问题 求教
#8
夏一站2012-05-17 15:34
+0,前面也出现M,可能是前段程序出了问题,看不出哪里问题,旁观!!
#9
荡气回肠2012-05-17 16:37
回复 楼主 Cai_xw1993
    os.put()——将字符ch放入ostream,返回os。os.put()是未格式化的操作一次一个字符地处理流,它们不忽略空白地读。标准库提供了丰富的支持未格式化IO的低级操作,这些低级操作能够将流作为未解释的字节处理,而不是作为数据类型(如 char、int、string等)的序列处理。而控制输入格式化默认情况下,输入操作符忽略空白(空格、制表符、换行符、进纸和回车)。
    对于您遇到的的问题,可以将程序修改如下:

#include <iostream>
using namespace std;
int main ()
{
    char ch = 'M';
    int i = ch;
   
    cout << "The ASCⅡ code for " << ch << " is "<< i << endl;
    cout << "Add one to the character code;"<< endl;
   
    ch = ch + 1;
    i = ch;
   
    cout << "The ASCⅡ code for " << ch << " is " << i << endl;
    cout << " Displaying char ch using cout.put(ch): ";
    cout << cout.put(ch) << endl;
    cout << " Done !" << endl;
   
    return 0;
}
只有本站会员才能查看附件,请 登录



[ 本帖最后由 荡气回肠 于 2012-5-17 16:39 编辑 ]
#10
nongzhaobo2012-05-17 21:57
还是不懂....唉..
#11
荡气回肠2012-05-19 08:03
#include <iostream>
using namespace std;
int main ()
{
    char ch = 'M';
    int i = ch;
   
    cout << "The ASCⅡ code for " << ch << " is "<< i << endl;
    cout << "Add one to the character code;"<< endl;
   
    ch = ch + 1;
    i = ch;
   
    cout << "The ASCⅡ code for " << ch << " is " << i << endl;
    cout << " Displaying char ch using cout.put(ch): ";
    cout.put(ch) << endl;  //而cout << cout.put(ch) << endl; 在输出N的同时也会输出ch的地址。
    cout << " Done !" << endl;
   
    return 0;
}
只有本站会员才能查看附件,请 登录
#12
Cai_xw19932012-05-21 18:59
回复 9楼 荡气回肠
只有本站会员才能查看附件,请 登录
表示还是出现乱码呀,这个输出流中没有。。。。
#13
Cai_xw19932012-05-21 19:01
回复 11楼 荡气回肠
好吧,表示刚看到你这个回复。懂了,原来那个“乱码”是地址,谢谢~~
1