Hey everyone! Okay. So I need help. I'm supposed to be writing a program that solves a quadratic equation when given variables a, b, and c. I have to read in these variables from a txt file formatted as follows.
6 -10 -4
2 6 9
2 4 8
0 2 4
2 4 2
-99 -99 -99
Now, instead of a trailer of -99, I'm SUPPOSED to have a trailer of 0, but I can't figure out how to do that since I'm ALSO supposed to not even try solving for roots if a=0, as is the case in row 4.
For the output, we're supposed to print a, b, c, the two roots, and/or any errors messages in the following format (the error message occurring whenever a=0).
A B C Root 1 Root 2
6 -10 -4 ? ?
2 6 9 ? ?
. . . . .
. . . . .
I'm just having a really hard time getting this to work, because I orignally coded the program to actually create a txt "output" file, which apparently we're not supposed to do? Just print. And as dumb as it seems, I can't figure out what to get rid of or what to leave in the program I originally made that created an output file in order for the program to still read the input file and but only PRINT the correct data. I tried deleting lines 60-69, but then it just tells me fout is not declared. My teacher isn't being very helpful and at this point I'm just incredibly frustrated and confused. Any help anyone can offer would be wonderful.
***Extra note, if anyone is angelic and patient enough to help me.
My program is currently printing this:
6 -10 -4 -0.333333 2
2 6 9 2 4
8 0 2 4 -0.333333
2 2 4 2 -1
This program coded by Allysen Jones.
--------------------------------
Process exited after 0.1099 seconds with return value 0
Press any key to continue . . .
It's shifting the numbers all weird. The second row should only be 2, 6, 9, and the root1 and root2 spaces should both be blanks as there are no roots for those variables.
Really. I'm in a mess.***
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
int main()
{
ifstream fin("input.txt");
ofstream fout("output.txt");
float discriminant, A, B, C, root1, root2;
fin >> A >> B >> C;
while (A != -99)
{
discriminant = (pow(B,2.0)-4*A*C);
if (A == 0)
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else if (discriminant > 0)
{
root1 = (-B - sqrt(discriminant))/(2.0*A);
root2 = (-B + sqrt(discriminant))/(2.0*A);
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else if (discriminant == 0)
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
fin >> A >> B >> C;
}
fout.close();
ifstream fin2("output.txt");
fin2 >> A >> B >> C >> root1 >> root2;
while(!fin2.eof())
{
cout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
fin2 >> A >> B >> C >> root1 >> root2;
}
cout << endl;
cout << "This program coded by Allysen Jones.";
}
|