Issues with Discriminant program

I'm having issues with the code below:

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
61
62
#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 << "Coded by Paye W. Kialain" << "\t"<< endl;
    system("pause");
    return 0;
}


In the project description, I was told to do the following:

Create an input file containing a, b, and c, which I did.

Format the output in a certain way, which is also correct (a table displaying the a, b, and c values along with the 2 calculated roots.)

Use a trailer of 0 0 0 at the end of the data file, which I haven't been able to do without ruining everything so I put -99 -99 -99 as the sentinel line instead.

Satisfy the following criteria:
If the result < 0, there are no real roots to the equation
If the result = 0, there is one real root (-b/2a)
If the result > 0, there are two real roots to the equation
Do not solve for roots if a = 0 – code as an error message

However, the calculations of the roots seem to be off. Are my if statements the issue?
However, the calculations of the roots seem to be off. Are my if statements the issue?

In the majority of cases, root1 and root2 are not assigned any value.
Do you think there's a way to assign specific values to root1 and root2 for each set of a, b and c values?
Place the assignments inside the respective if/else bodies.
I attempted that but the loop didn't stop running...
Provide your updated code. Note that your specifications gave you explicit instructions on when not to do a calculation. In that case you need to assign some value to root1/root2 to indicate they have not been calculated (or supply alternate output to the user.)
Topic archived. No new replies allowed.