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

关于cout.setf(ios_base ::showpoint)的一个问题

zhaoyg 发布于 2008-01-26 20:55, 2400 次点击
#include <iostream>

using namespace std;

int main()
{
    float a=2.4;

    cout <<"before showpoint,a="<<a<<endl;    //output 2.4

    ios_base ::fmtflags back=cout.setf(ios_base ::showpoint);

    cout <<"after showpoint,a="<<a<<endl;    //output 2.400000
    cout.setf(back);

    cout<<"after restore , a="<<a<<endl;     //还是 2.400000

    system ("pause");
    return 0;
}
为什么执行了cout.setf(back);后还是输出2.400000?
2 回复
#2
eagleboycn2008-01-26 22:41
用cout<<resetiosflags(ios::showpoint);可以啊
你用cout.setf(back);是甚么意思啊?
***********************************************************************
The function setf() sets the io stream format flags of the current stream to flags. The optional needed argument specifies that only the flags that are in both flags and needed should be set. The return value is the previous configuration of io stream format flags.
*******************************************************************
resetiosflags( long f )
Turn off the flags specified by f
#3
zhaoyg2008-01-26 22:45
书上当时说是用cout.setf(back)就可以返回到默认状态,但我试了半天都没有看到效果。
其中back保存的是默认值

[[italic] 本帖最后由 zhaoyg 于 2008-1-26 22:46 编辑 [/italic]]
1