程序没有输出到文件? C ++

时间:2016-12-15 06:07:20

标签: c++ function quadratic

?我被困在一个我们需要使用函数头创建二次方程序并传递值数和引用的地方。一切似乎都是正确的计算,但它没有输出任何东西到我指导它的输出文件。有什么建议吗?

#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<iomanip>
using namespace std;


void GetInputs(ifstream&in, double &a, double &b, double &c);
int Quadroots(double a, double b, double c, double &r1, double &r2);
void Print(ofstream &out, double a, double b, double c, double r1, double r2, int EquationKind);
ifstream in;
ofstream out;


void GetInputs(ifstream &in, double &a, double &b, double &c)
{
    in >> a >> b >> c;

}

int Quadroots (double a, double b, double c, double& r1, double& r2)
{
    double radical;
    if (a==0)
    {
        return -1;
    }

    radical = b*b-4*a*c;

    if (radical < 0)
    {
        return -2;
    }

    else
    {   r1 = (-b + sqrt(radical))/2*a;
        r2 = (-b - sqrt(radical))/2*a;

        return 0;
    }
}
void Print(ofstream& out, double a, double b, double c, double r1, double r2, int EquationKind)
{

        out << "Solving roots for Quadratic equations (ax^2+bx+c)"<< endl;
        out << "a   "<< "b  " << "c          " << "Root1          "<< "Root2          "<< "message" << endl;
        out << "-----------------------------------------------------------------------------------------------" << endl;
        out << a << "     "<< b << "     "<< c << endl;


        if (r1 != 10000.0 & r2 != 10000.0)
        out << r1 <<"       "  << r2  << "      " << "Two Real roots." << endl;
        if (r1!=10000.0 || r2 !=10000.0)
        out <<r1 <<"             " <<  "One real roots" << endl;
        if (a==0)
        out << "                          " << "It is a line"<< endl;
        if (EquationKind== -2)
        out << "                          " << "No real solution" << endl;
}



int main()
{

    int Quadroot1, Quadroot2, EquationKind;
    double a, b, c, r1=10000.0, r2=10000.0;

    in.open("input.txt");
    if (!in)
    {
        out << "error opening file";
        return -1;
    }
    out.open("output.txt");
    if (!out)
    {
       out << "Output file cannot be created. Program ends" << endl;
       in.close();
       return -1;

GetInputs(in, a, b, c);

    while(!in.eof())
    {

        EquationKind = Quadroots(a, b, c, r1, r2);
        Print(out, a, b, c, r1, r2, EquationKind);
        GetInputs(in, a, b, c);
    }

Print(out, a, b, c, r1, r2, EquationKind);

out.close();

return 0;


}}

2 个答案:

答案 0 :(得分:0)

您没有正确关闭if(!out)条件。将其更改为:

if (!out)
{
   out << "Output file cannot be created. Program ends" << endl;
   in.close();
   return -1;
}

您需要删除主函数末尾的最后一个花括号。

答案 1 :(得分:0)

通过格式化代码(我使用了clang-format,还有其他工具,但我离题了)你的错误变得更加清晰。

您的代码:

int main() {                                                     
  int Quadroot1, Quadroot2, EquationKind;                        
  double a, b, c, r1 = 10000.0, r2 = 10000.0;                    

  in.open("input.txt");                                          
  if (!in) {                                                     
    out << "error opening file";                                 
    return -1;                                                   
  }                                                              
  out.open("output.txt");                                        
  if (!out) {                                                    
    out << "Output file cannot be created. Program ends" << endl;
    in.close();                                                  
    return -1;                                                   

    GetInputs(in, a, b, c);                                      

    while (!in.eof()) {                                          
      EquationKind = Quadroots(a, b, c, r1, r2);                 
      Print(out, a, b, c, r1, r2, EquationKind);                 
      GetInputs(in, a, b, c);                                    
    }                                                            

    Print(out, a, b, c, r1, r2, EquationKind);                   

    out.close();                                                 

    return 0;                                                    
  }                                                              
}  

最终的原因是(至少!我假设没有其他错误!)错误的支撑。修复main中的结果:

int main() {                                                     
  int Quadroot1, Quadroot2, EquationKind;                        
  double a, b, c, r1 = 10000.0, r2 = 10000.0;                    

  in.open("input.txt");                                          
  if (!in) {                                                     
    out << "error opening file";                                 
    return -1;                                                   
  }                                                              
  out.open("output.txt");                                        
  if (!out) {                                                    
    out << "Output file cannot be created. Program ends" << endl;
    in.close();                                                  
    return -1;                                                   
  }                                                              

  GetInputs(in, a, b, c);                                        

  while (!in.eof()) {                                            
    EquationKind = Quadroots(a, b, c, r1, r2);                   
    Print(out, a, b, c, r1, r2, EquationKind);                   
    GetInputs(in, a, b, c);                                      
  }                                                              

  Print(out, a, b, c, r1, r2, EquationKind);                     

  out.close();                                                   

  return 0;                                                      
}