标题:请教一个c++的小问题
取消只看楼主
xjbmcx
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-9-22
结帖率:80%
已结贴  问题点数:18 回复次数:1 
请教一个c++的小问题
请问这个程序里面如果要将平均分的结果保留两位小数的话
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
要把这三行放在哪里?谢谢

程序为:
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
  ifstream in_stream;                 
  ofstream out_stream;
  
  in_stream.open("infile.txt");
  if(in_stream.fail())
  {   
    cout<<"input file opening failed.\n";
       exit(1);
  }
  out_stream.open("outfile.txt");
  if(out_stream.fail())
  {
    cout<<"output file opening failed.\n";
    exit(1);
  }
  int first,second,third,fourth,fifth,sixth,seventh,eighth,nineth,tenth;
  double average;
  average=(first+second+third+fourth+fifth+sixth+seventh+eighth+nineth+tenth)/10;
  
  in_stream>>first>>second>>third>>fourth>>fifth>>sixth>>seventh>>eighth>>nineth>>tenth;

  cout << first<< endl;

  cout << second<< endl;

  cout<< third<< endl;

  cout<<fourth<<endl;

  cout<<fifth<<endl;

  cout<<sixth<<endl;

  cout<<seventh<<endl;

  cout<<eighth<<endl;

  cout<<nineth<<endl;

  cout << tenth<< endl;
  out_stream<<"the average of the 10\n"
            <<"number in infile.txt\n"
            <<"is"
            <<(first+second+third+fourth+fifth+sixth+seventh+eighth+nineth+tenth)
            <<endl;
  cout<<"the average is:"<<(first+second+third+fourth+fifth+sixth+seventh+eighth+nineth+tenth)/10<<endl;
  in_stream.close();
  out_stream.close();
  return 0;
}
搜索更多相关主题的帖子: include failed file 
2013-10-08 21:25
xjbmcx
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-9-22
得分:0 
有逻辑错误吗?但是我运行时是得到了结果的。请教下哪里错误了。谢谢
2013-10-09 11:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-421593-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.213640 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved