我已將integer
分配給double
變量,但cout
將雙變量打印爲int
。不是double
。如果我在代碼中引入cout << showpoint;
,那麼我可以在輸出中看到小數值。爲什麼在第一種情況下如此?這是代碼。cout跳過小數點後的數字
#include <iostream>
using namespace std;
template <class T>
T sum(T a,T b)
{
T retval;
retval=a+b;
return retval;
}
int main()
{
double a,x;
float y,v=4.66;
int z(3);
x=z;
y=(double)z;
a=sum(5,6);
//cout << showpoint;
cout<<"The value of a is : "<<a<<endl;
cout<<"The value of x is : "<<x<<endl;
cout<<"The value of y is : "<<y<<endl;
}
在第一種情況下的輸出是
The value of a is : 11
The value of x is : 3
The value of y is : 3
輸出在第二種情況下啓用cout<<showpoint
後是
The value of a is : 11.0000
The value of x is : 3.00000
The value of y is : 3.00000
setprecision不存在,我編輯了問題 – blitz
'showpoint'是什麼? – Raxvan
@Raxvan:這裏是你可以得到答案的鏈接http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node83.html – blitz