| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 604 人关注过本帖
标题:请教一个c++的小问题
只看楼主 加入收藏
xjbmcx
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-9-22
结帖率:80%
收藏
已结贴  问题点数:18 回复次数:6 
请教一个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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9024
专家分:54030
注 册:2011-1-18
收藏
得分:9 
你这代码,不单……,而且还有滑稽的逻辑错误,所以我也不多说,只回答你的问题。

请问这个程序里面如果要将平均分的结果保留两位小数的话
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
要把这三行放在哪里?谢谢

--- 答:放在输出之前任意处。
2013-10-09 08:27
xjbmcx
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-9-22
收藏
得分:0 
有逻辑错误吗?但是我运行时是得到了结果的。请教下哪里错误了。谢谢
2013-10-09 11:58
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9024
专家分:54030
注 册:2011-1-18
收藏
得分:0 
以下是引用xjbmcx在2013-10-9 11:58:49的发言:

有逻辑错误吗?但是我运行时是得到了结果的。请教下哪里错误了。谢谢
  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;
2013-10-09 12:38
CL0419
Rank: 2
等 级:论坛游民
帖 子:8
专家分:15
注 册:2013-3-18
收藏
得分:9 
我来解决,首先,你在你新建的工程目录下,要新建两个文档:infile.txt,outfile.txt,然后再代码是这样:
#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");
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  
  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;
  in_stream>>first>>second>>third>>fourth>>fifth>>sixth>>seventh>>eighth>>nineth>>tenth;
  average=(first+second+third+fourth+fifth+sixth+seventh+eighth+nineth+tenth)/10;
  
  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<</* cout.precision(2)<<*/endl;
  
  in_stream.close();
  out_stream.close();
  return 0;
}
2013-10-09 16:59
CL0419
Rank: 2
等 级:论坛游民
帖 子:8
专家分:15
注 册:2013-3-18
收藏
得分:0 
对了,忘了告诉你,要在infile.txt这个文档中写十个数字,就可以了,就可以求出来了……
2013-10-09 16:59
CL0419
Rank: 2
等 级:论坛游民
帖 子:8
专家分:15
注 册:2013-3-18
收藏
得分:0 
回复 4楼 rjsp
把: in_stream>>first>>second>>third>>fourth>>fifth>>sixth>>seventh>>eighth>>nineth>>tenth;这个放在average=(first+second+third+fourth+fifth+sixth+seventh+eighth+nineth+tenth)/10;之前。
2013-10-09 17:00
快速回复:请教一个c++的小问题
数据加载中...
 
   



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

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