为什么它不工作
why is setw not working?
#include<fstream.h>
#include<iomanip.h>
#include<iostream.h>
using namespace std;
ifstream f("atestat.in");
ofstream g("atestat.out");
int n,i,nr=0;
float v[100];
void a(int n)
{
for(i=1;i<=n;i++)
f>>v[i];
for(i=1;i<=n;i++)
cout<<v[i]<<" ";
}
int main()
{
f>>n;
a(n);
cout<<endl;
float s=0;
for(i=1;i<=n;i++)
{
if(v[i]<0)
{
s=s+v[i];
nr++;
}
}
cout<<setw(2)<<s/nr<<endl;
}
我"atestat 。在"文件"中包含:6-56.765 2.3 4.56 -1.2 -1.8 3
程序首先显示"atestat"第二行上的所有数字。在"文件"中,通过使用一个数组,然后它应该显示该数组中所有负数的算术平均值,精度为小数点后2个数字。由于某种原因,setw(2)什么也没做,因为我的cout<<setw(2)<<s/nr<<endl;
显示的是"19.9217"而不是"19.92"…有人能告诉我为什么吗?我是不是用错了?
,精度为小数点后2个数字
为此,您需要:
std::cout << std::fixed;
std::cout << std::setprecision(2) << f << 'n'; //assume f the number you wanna print
std::setw不能用于此目的:
当用于表达式out <<Setw (n) or in>> Setw (n),设置出流或入流的宽度参数正好为n。
相关文章:
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 为什么我的 std::ref 无法按预期工作?
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 有人能解释一下为什么下界是这样工作的吗C++的
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么stream::忽略未按预期工作
- 为什么 HeapFree() 不能正常工作?
- 为什么我在 AVR 中的中断无法正常工作?
- 为什么指针在对二维数组进行排序时无法正常工作?
- 为什么C++需要公共继承,忽略朋友声明,才能使动态向下工作?
- 为什么 sizeof(ar)/ sizeof(ar[0]) 在传递给函数时无法在向量上正常工作?
- 为什么它在不分配内存的情况下工作正常
- 为什么重载运算符"="动态数组的类上无法正常工作?C++
- std::async 如何工作:为什么它会调用这么多次复制/移动?
- 静态 constexpr 函数在模板结构中工作,但不能在结构中工作.为什么?
- 禁用 GPU 使我的 CNTK 程序正常工作.为什么
- C++余弦在没有 std 命名空间的情况下工作 - 为什么
- c++的pow(2,1000)对于double来说通常太大了,但它正在工作.为什么
- 程序不是在win上工作,而是在mac上工作.为什么?
- 当我用字符串代替char时,代码可以完美地工作.为什么如此