花开自有丶花落 发表于 2016-7-27 11:12:53

类方法那里有问题,总是打开失败,去掉后信息成功写入文件

if(!fout.is_open());
每次都是打开失败然后退出,去掉后就写入信息了
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void fileit(ostream&os,double fo,const double fe[],int n);
const int LIMIT = 5;
int main()
{
    ofstream fout;
    const char * fh ="1.txt";
    fout.open(fh);
    if(!fout.is_open());
    {
                   system("pause");
                   exit(EXIT_FAILURE);
    }
    double objective;
    cout<<"Enter the focal length of your telescopr objective in mm";
    cin>>objective;
    double eps;
    cout<<"Enter the focal lengths, in mm, of "<<LIMIT<<"eyepieces:\n";
    for(int i = 0;i < LIMIT;i++)
    {
            cout<<"Eyepices #"<<i + 1<<": ";
            cin>>eps;
    }
    fileit(fout,objective,eps,LIMIT);
    fileit(cout,objective,eps,LIMIT);
   
    system("pause");
    return 0;
   
}
void fileit(ostream&os,double fo,const double fe[],int n)
{
   ios_base::fmtflags initial;
   initial = os.setf(ios_base::fixed);
   os.precision(1);
   os.width(12);
   os<<"f.1. eyepiece";
   os.width(15);
   os<<"magnification"<<endl;
   for(int i = 0;i < n;i++)
   {
             os.width(12);
             os<<fe;
             os.width(15);
             os<<int(fo/fe + 0.5)<<endl;
   }
   os.setf(initial);
}

花开自有丶花落 发表于 2016-7-27 11:35:59

自己2B了,后面有个分号,怪不得

子煜从嘉 发表于 2016-8-2 21:37:24

{:10_277:}
页: [1]
查看完整版本: 类方法那里有问题,总是打开失败,去掉后信息成功写入文件