Decimal Problem: setprecision
Jun 15, 2014 at 6:51pm UTC
So after slaving away at making a BMI calculator and making it work for both metric and standard measuring systems, I can't get the program to display the BMI with 2 decimal places. Did I declare the variable wrong or something? Also if you see any other errors in the program feel free to comment about it. We were suppose to stylize the results section with the *'s and what not.
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
/*Assignment #2
BMI Calculator*/
{
//Variable declarations
double heightstandard, heightin, heightft, weightstandard, heightmetric, weightmetric, result, type;
//Text Display
cout << "Body Mass Index Calculator (BMI)" << endl << endl << "BMI is a measure of an adult's body fat based on his/her weight and height." << endl << endl;
//Metric or Standard
cout << "Plese enter 1 if you want to use standard measurement system (American measurement system) or 2 for metric system measurement." ;
cin >> type ;
//Decision Point
//Standard
if (type = 1)
{cout << "Please enter your weight in pounds" ;
cin >> weightstandard;
cout << "Please enter your height in feet" ;
cin >> heightft;
cout << "Please enter the remaining inches in your height" ;
cin >> heightin;}
//Metric
else
{cout << "Please enter your weight in kilograms" ;
cin >> weightmetric ;
cout << "Please enter your height in meters" ;
cin >> heightmetric ;}
//Conversion of Feet To Inches
{heightstandard = ((heightft * 12) + heightin);}
//BMI Calculations
//Standard
if (type =1)
{result = ((weightstandard * 703)/(pow(heightstandard, 2)));}
//Metric
else
{result = (weightmetric/(pow(heightmetric, 2)));}
//Results Display Standard
if (type = 1)
cout << "*****************************" << endl << "*****************************" << endl << endl << std::left << " " << "Weight:" << weightstandard << " " << "Height:" << heightstandard << endl << " "
<< "Your BMI value:" << std::setprecision(2) << result << endl;
//Results Display Metric
else
{
cout << "****************************" << endl << "****************************" << endl << endl << std::left << " " << "Weight:" << weightmetric << " " << "Height:" << heightmetric << endl
<< " " << "Your BMI value:" << std::setprecision(2) << result << endl;}
//Display Results BMI
cout << std::left << " " << "Congradulations you are " ;
//Results
if (result <= 18.5)
{cout << "underweight" ;}
if (result >= 18.5)
{cout << "normal" ;}
if (result >= 25)
{cout << "overweight" ;}
if (result >=30)
{cout << "obese" ;}
//End Lines
cout << endl << "*****************************" << endl << "******************************" ;
system("pause" );
return 0;
}
Jun 15, 2014 at 8:24pm UTC
1 2
double value = 395.279315;
std::cout << std::fixed << std::setprecision(2) << value;
395.28
Jun 16, 2014 at 4:31am UTC
@Eevee
You also have a small problem in this type of coding..
1 2 3 4 5 6
//BMI Calculations
//Standard
if (type =1)// Single equal means to assign. Change to a double equals to compare
// Do at both places
{result = ((weightstandard * 703)/(pow(heightstandard, 2)));}
Jun 16, 2014 at 5:04am UTC
@whitenite1
Gotcha, that explains why if I ran it using metric it'd still run both.
@MiiNiPaa
That worked perfectly, thank you!
Topic archived. No new replies allowed.