I need help with the beginer c++ code!

So in intro to c++ class we are writing codes using functions and importing text files to read it and outputting them in a diffrent text file. I wrote the functions correctly, and dont know what to write for my int main() function.

this is what i have so far:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double Readcoeff(ifstream& fin)
{
double coeff;
fin>>coeff;
return coeff;
}
double Root1(double a, double b, double c)
{
double Root1 = (-b+ sqrt(pow( b,2)-4* a* c)/(2* a));
return Root1;
}
double Root2(double a, double b, double c)
{
double Root2 = (-b- sqrt(pow( b,2)-4* a* c)/(2* a));
return Root2;
}
double discriminant( double a, double b, double c)
{
double discriminant = (pow(b,2)-4*a*c);
return discriminant;
}

int main()
{
ifstream inFile;
ifstream inputFile("coeffs.txt");
double coeff;
double calcroot1;
double calcroot2;
double calcdiscriminant;

inFile >> coeff;
double a;
inFile >> coeff;
double b;
inFile >> coeff;
double c;
inFile.close();
calcroot1 = Root1 (a,b,c);
cout << calcroot1;
calcroot2 = Root2 (a,b,c);
cout << calcroot2;
calcdiscriminant = discriminant (a,b,c);
cout << calcdiscriminant;



return 0;
}

here is the pseudocode for what we're suppose to do:

main() — Opens coeffs.txt for reading. Calls ReadCoeff() three times to read the coefficients a, b, and c from the input file. Calls
CalcRoot1() and CalcRoot2() to calculate the two roots. Calls Output() to write the equation and the roots to the output file
roots.txt.

• ReadCoeff() — Reads a double from the input file and returns it. Very Important Note: when passing the file stream fin from
main() to ReadCoeff() you must list the data type of fin in the formal parameter list as ifstream& fin. I will explain why the
ampersand is necessary later in the course.

• CalcRoot1() — Calculates root1 using the quadratic formula presented in Section 4 (-b + ...).
• CalcRoot2() — Calculates root2 using the quadratic formula presented in Section 4 (-b ‒ ...).
• Discriminant() — Returns the discriminant which is presented in Section 4.
• Output() — Writes the equation and the roots to the output file roots.txt.
Last edited on
Instead of this
1
2
    inFile >> coeff;
  double a;


I would suggest: double a = Readcoeff(inputFile);

The pseudocode says the output goes to file "roots.txt" which means you need an output file as well as an input file.

The discriminant is used to determine whether the roots are real or imaginary. The square root of a negative number is imaginary, the normal sqrt() function will give an out-of-range error if you attempt it.

http://www.regentsprep.org/Regents/math/algtrig/ATE3/discriminant.htm

Topic archived. No new replies allowed.