Calling functions! Program to read #s from .txt file, calculate discriminate,
Hey there! If someone could help me with my assignment, I would greatly appreciate it!
This is my problem from my professor:
Write a program (C++) that will read 5 sets of 3 values a, b, c from a file (quad.txt).
Create a function to calculate the discriminate and return either: "positive", "zero" or "negative"
In other words, the function will calculate the discriminate and determine if the discriminate is positive, zero and negative and return the correct string message.
Create a function that will calculate and display a, b, c and the roots or no roots based on the string message returned.
Read another set of a, b, c from a file.
Repeat this for 5 sets of a, b, c.
End
This is what I have so far (basically I have a program that does all of what is stated above, but I need it to call for the functions instead of calculating in main
Code:
//
#include "stdafx.h"
#include<iostream> //required for cout. endl.
#include<fstream>//Required for ifstream, ofstream
#include<cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Declare variables
double a, b, c, discriminant;
float r1, r2;
ifstream fin;
ofstream report;
//Open file and read first data line (a, b, c)
fin.open("quad.txt");
if(fin.fail() ) //open failed
{
cerr<<"File quad.txt could not be opened";
exit(1); //end execution of the program
}
while(!fin.eof())
{
fin >> a >> b >> c;
//Calculate discriminant
discriminant=((b*b)-4*a*c);
cout << discriminant << endl;
if(discriminant>0)
{
r1=(-1*b+sqrt(discriminant))/(2*a);
r2=(-1*b-sqrt(discriminant))/(2*a);
cout<<"r1="<<r1<<"\n";
cout<<"r2="<<r2<<"\n";
cout<<"there are two distinct roots";
}
else if(discriminant==0)
{
r1=(-1*b)/(2*a);
cout<<"r1="<<r1<<"\n";
cout<<"there is exactly one distinct real root";
}
else
{
discriminant=-1*discriminant;
cout<<"No real roots";
}
cout<<"\n";
system("pause");
}
return 0;
}
Thanks guys!