| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 604 人关注过本帖
标题:请教一个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
快速回复:请教一个c++的小问题
数据加载中...
 
   



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

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