Reputation: 25
I'm designing a program that calculates 2 roots using the quadratic formula. I'm trying to get my output statements root1
and root2
to output with the number to two decimal players (i.e. 2.00, 5.00, etc.). However, when I use the setprecision command, it doesn't seem to work. For example, when I input the three points to be (1, -1, 2), I get the points (2, -1), not (2.00, -1.00) like I'm looking for.
Here is my code:
#define _USE_MATH_DEFINES // for C++
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double pointa;
double pointb;
double pointc;
double discriminant1;
double root1;
double root2;
cout << "Please enter a, b and c: ";
cin >> pointa;
cin >> pointb;
cin >> pointc;
discriminant1 = pow(pointb, 2) - (4 * pointa * pointc);
root1 = (-pointb + sqrt(discriminant1)) / (2 * pointa);
root2 = (-pointb - sqrt(discriminant1)) / (2 * pointa);
std::cout << "Root1: " << std::setprecision(3) << root1 << endl;
std::cout << "Root2: " << std::setprecision(3) << root2 << endl;
return 0;
}
Upvotes: 0
Views: 85
Reputation: 145459
Choose fixed point format by outputting std::fixed
.
Upvotes: 1