注册 登录
编程论坛 C++教室

请教一个c++的小问题

xjbmcx 发布于 2013-10-08 21:25, 604 次点击
请问这个程序里面如果要将平均分的结果保留两位小数的话
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;
}
6 回复
#2
rjsp2013-10-09 08:27
你这代码,不单……,而且还有滑稽的逻辑错误,所以我也不多说,只回答你的问题。

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

--- 答:放在输出之前任意处。
#3
xjbmcx2013-10-09 11:58
有逻辑错误吗?但是我运行时是得到了结果的。请教下哪里错误了。谢谢
#4
rjsp2013-10-09 12:38
以下是引用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;
#5
CL04192013-10-09 16:59
我来解决,首先,你在你新建的工程目录下,要新建两个文档: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;
}
#6
CL04192013-10-09 16:59
对了,忘了告诉你,要在infile.txt这个文档中写十个数字,就可以了,就可以求出来了……
#7
CL04192013-10-09 17:00
回复 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;之前。
1