Program not outputting to file? C++

664 views Asked by At

? I'm stuck on an where we are required to create a quadratic formula program using function headers and pass by value numbers and references. Everything seems to be right with the calculations but it's not outputting anything to the output file I am directing it towards. Any advice guys?

#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

There are 2 answers

0
KTing On

You did not close the if (!out) condition properly. Change it to:

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

You will want to delete the last curly brace at the end of the main function.

1
druckermanly On

By formatting your code (I used clang-format, there are other tools, but I digress) your error is made much more clear.

Your code:

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;                                                    
  }                                                              
}  

The ultimate cause is (at least! I'm assuming there are no other errors!) a misplaced brace. Fixing that in main yields:

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;                                                      
}