-
decimal ?
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
float a, b, c;
float x1, x2;
float root, temp;
cout << "Enter the coefficient of the quadratic function (a*x^2+bx+c): ";
cin >> a >> b >> c;
temp = (b*b) - (4*a*c);
if(temp>=0)
{
root = sqrt(temp);
x1 = (-b+root) / (2.0*a);
x2 = (-b-root) / (2.0*a);
if (x1==x2)
{
cout << "The single root for the quadratic function -> x = "
<< setprecision(2) << x1 << endl;
}
else
{
cout << "The root for the quadratic function -> x = "
<< setprecision(2) << x1 << " and " << setprecision(2) << x2 << endl;
}
}
else
cout << "Invalid input" << endl;
system("pause");
return 0;
}
when i enter 1 2 1, the output is -1. Why the output is -1 and not -1.00 ?
-
I believe you need to set the format flags to floating-point, something like this IIRC:
Code:
cout.setf(ios_base::fixed, ios_base::floatfield);
-
i declared all my variables as float .... but why it still come out as integer value ? even if i do not use the flag ... it should printf out float value right ? the value stored in each variable should be in decimal format rite ?
-
setprecision sets how precise it will be, up to 2 decimal places in this case...if both those are zero, then it wont print them out, but if the number was something like 1.2345 it prints out 1.23