if command help

Question: The following code is for calculation quadratic equation. after putting values of a,b and c it prints as ax^2 + bx + C
so I want to include if command that if any value of a and b is 1 then it will ignore the 1. so, if a is 1 then equation should be x^2 + b + C. if b is 1 equation should be ax^2 + x + c. Simple there should not be any 1 in the equation other than c.
can anyone please enter the if command in my code? i have highlighted where you may put it.

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

#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, const char * argv[])
{
    double a;
    double b;
    double c;
    
    cout << "Please enter the coefficients below_" << endl;
    cout << "\ta = "; cin >> a;
    cout << "\tb = "; cin >> b;
    cout << "\tc = "; cin >> c;

   //////////////////////// if command to view the equation


    double d = (b*b) - (4*a*c);
    double root = sqrt(d);
    double e = (-b + root) / (2*a);
    double f = (-b - root) / (2*a);
    
    cout << "The roots are_" << endl; cout << "\tx1 = " << e << endl << "\tx2 = " << f << endl;
    cout << "Verifying results by substituting x1 and x2 into equation...";

    
    cout << "(" << a << e << ")^2 + " << b << "." << e << " " << c << " = " << a*(e*e) + b*e + c <<endl;
    cout << "(" << a << f << ")^2+ " << b << "." << f << " " << c << " = " << a*(f*f) + b*f + c <<endl;
    
    
    return 0;
}
You don't want a or be to actually be absent, you just don't want them displayed when the equation is being printed. The equations are printed in lines 30 and 31, so that's where you need to make the check.

Each of these lines would become multiple lines something like:

1
2
3
4
5
6
7
8
9
10
11
if (a != 1.0)
{
    cout << a;
}
cout  << "(" << e << ")^2 +"; 

if (b != 1.0)
{
    cout << b << ".";
}
cout << e << " + " << c;


Note: what you currently print out is incorrect. You do not want (Ax)^2, you want A(x^2). Also you forgot the '+' before c
Thanks allot bro!! Really helped me! :)
Topic archived. No new replies allowed.