I am trying to print multiple floats, but only some of them need to be a specific decimal place. The user will input two float values, a width and a height. Then the program will calculate the perimeter and area with the two values. Then it will print the values plugged into the equations and show the answer with only one decimal place.
So, an example would look like this:
Width: 3.14
Height: 4.2
Area: 3.14 * 4.2 = 13.2
Perimeter: 2 * (3.14 + 4.2) = 14.7
The issue I am having is that when I go to print the output, I can't get it to print the exact values of the floats that the user inputs and have it print out the answers with only one decimal place. I've tried using setprecision as well as printf, but I just can't get it to work. I tried following answered questions about showing exact decimal places, but nothing has been meeting what I need to do. Here's my code (Sorry if it's sloppy):
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float width = 0;
float height = 0;
cout << "Rectangle Calculator" << endl;
cout << "Please enter the width: ";
cin >> width;
cout << "Please enter the height: ";
cin >> height;
float area = width * height;
float perimeter = 2 * (width + height);
cout << "Area: " << width << " * " << height << " = " << fixed << setprecision(1) << area << endl;
cout << "Perimeter: 2 * (" << width << " + " << height << ")" << " = " << fixed << setprecision(1) << perimeter;
return 0;
}