Not understanding setprecision function in c++ - c++

I'm learning C++ and am supposed to make a program which takes height in inches, weight in pounds, and age from the user, and gives them the size of their clothing. You get the size of their hat by dividing their weight by their height and multiplying that result with 2.9. I have been testing my code and the output is correct but always has an extra 1 (like 9.941 instead of 9.94) at the end. My answer should only have two digits after the decimal point, even if it's a zero. Does anyone have any tips? Thanks guys. Here is my code:
#include<iostream>
#include<iomanip>
using namespace std;
void HatSize(double userHeight, double userWeight) {
cout << setprecision(2) << fixed << ((userWeight / userHeight) * 2.9);
}
int main(){
double height;
double weight;
double age;
cout << "Give me your height in inches, weight in pounds, and age in years. I will give you your hat size, jacket size (inches at chest)\nand waist size in inches." << endl;
cin >> height;
cin >> weight;
cin >> age;
HatSize(height, weight);
cout << HatSize;
return 0;
}

You're using setprecision correctly, the issue is that you have an additional statement that is generating the 1.
Remove the cout << HatSize; line. HatSize is a function that returns void, so you're sending the actual function itself as input to cout, which is being interpreted as 1.
I would also recommend adding a << endl to the cout in your HatSize function, so that your output finishes with a newline.
void HatSize(double userHeight, double userWeight) {
cout << setprecision(2) << fixed << ((userWeight / userHeight) * 2.9)
<< endl; // Newline to make output nicer
}
int main(){
double height;
double weight;
double age;
cout << "Give me your height in inches, weight in pounds, and age in years. I will give you your hat size, jacket size (inches at chest)\nand waist size in inches." << endl;
cin >> height;
cin >> weight;
cin >> age;
HatSize(height, weight);
// cout << HatSize !!!!!!!!! Get rid of this
return 0;
}

Related

How to debug my C++ BMI calculator program?

I've only recently learnt C++. I had a school project for making a BMI calculator. Unfortunately it is showing up errors beyond my scope of understanding. I'm not sure if I should use a different data type for my height and weight variables - should these by double?
#include <iostream>
using namespace std;
float bmi_calc(int height, int weight){
float bmi_user = weight / height * height;
return bmi_user;
}
int main()
{
int weight_user;
int height_user();
cout << "Enter your weight in kilograms";
cin >> weight_user;
cout << "Enter your height in meters";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
}
fixed code:
#include <iostream>
using namespace std;
float bmi_calc(float height, float weight) {
return weight / (height * height);
}
int main()
{
float weight_user;
float height_user;
cout << "Enter your weight in kilograms ";
cin >> weight_user;
cout << "Enter your height in meters ";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
return 0;
}
since your height is given in meters you need a float, since a int can only be used for whole numbers. also use proper parenthesis to get the formula right :)

Trying to calculate jacket, hat, and waist size for assignment. Output is "0x8049f64" instead of answer

I'm getting some odd outputs when running this program. Any suggestions? Pardon the mess. Typed it up in a hurry. This is the guidelines for the assignment.
Write a program that asks for the user’s height, weight, and age, and
then computesclothing sizes according to the formulas:
1.Hat size = weight in pounds divided by height in inches and all multiplied by 2.9
2.Jacket size (chest in inches) = height times weight divided by 288 and then adjusted by adding 1/8 of an inch for each 10 years over the age of 30. (Note that the adjustment only takes place after a full 10 years. Thus, there is no adjustment for ages 30 through 39, but 1/8 of an inch is added for age 40.)
3.Waist in inches=weight divided by 5.7 and then adjusted by adding 1/10 of an inch for each 2 years over age 28. (Note that the adjustment only takes place
after a full 2 years. Thus, there is no adjustment for age 29, but 1/10 of an inch is added for age 30.)
#include <iostream>
using namespace std;
double hat(double,double);
double jacket(double,double,int);
double waist(double,double,int);
int main ()
{
double height, weight;
int age;
char answer;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
do
{
cout<< "Enter the customer's height in inches: ";
cin>>height;
cout<< "Enter the customer's weight in pounds: ";
cin>>weight;
cout<< "Enter the customer's age: ";
cin>>age;
cout << cout<< "\tYour Hat size: " << cout << "\tYour Jacket size: "< cout << "\tYour Waist size: "<< cout<< "Would you like to continue (y/n)? ";
cin>>answer;
}while(toupper(answer) == 'Y');
return 0;
}
double hat(double weight ,double height)
{
return ((weight/height) * 2.9);
}
double jacket(double height,double weight,int age)
{
double size;
int j;
if (age>=30)
{
if((age % 10) !=0)
age = age-(age%10);
j= (age-30)/10;
size =((height * weight) / 288)+((1.0/8)*j);
}
else
size =((height * weight) / 288);
return size;
}
double waist(double height,double weight,int age)
{
double size2;
int k;
if(age >= 28)
{
if((age % 2) !=0)
age = age-(age%2);
k = (age-28)/2;
size2 = (weight/(5.7))+( (1.0/10)*k);
}
else
size2 = weight / (5.7);
return size2;
}
Your final cout << line in the do loop is piping cout to cout, instead of your answer to cout. Not to mention there's a < in there after jacket size instead of a <<
cout << cout<< "\tYour Hat size: " << cout << "\tYour Jacket size: "< cout << "\tYour Waist size: "<< cout<< "Would you like to continue (y/n)? ";
You never even tried to do the calculation, so you have no answer to print.
Instead I see a lot of cout << cout in your code. Trying to print cout itself doesn't turn out well. What happens is that iostreams don't know how to print an output stream, however in old versions of C++, cout has an implicit conversion to a pointer, and a stream knows how to display that. So you are seeing the pointer equivalent to cout.
You should enable C++11 (or later) support in your compiler. Then cout won't have an implicit conversion to void*, and the compiler will detect mistakes like this.
you just need to replace your cout line with this line of code
cout<< "\tYour Hat size: " << hat(weight ,height) << "\tYour Jacket size: "<< jacket( height, weight, age) << "\tYour Waist size: "<< waist( height, weight, age)<< "\n \nWould you like to continue (y/n)? ";

How to calculate BMI in C++ with ft and inches in calculation

How do you calculate BMI = mass (lb) x 703/ (height(in))squared in C++.
BMI answer was 33.4695
I entered:
cout << "weight(lbs)";
cin >> lbs;
cout << "height";
cin >> height >> in;
BMI = ((lbs) * 703)/pow(height(in)), 2);
I'm having and error: no instance of overloaded function "pow" matched the argument list argue types are: (<error-types>).
I have included the <cmath> file and height is defined as double.
Let's start saying you don't need to call pow() at all, but in your case you misplaced the braces. It should be
pow(height, 2)
Assuming that height is a numeric variable.
It's not clear if you want to include somehow the units of measures of phisical quantities (inches or pound) into your variables. That's a more challenging task, but meanwhile you can use this:
float weight = 0, height = 0;
cout << "Enter weight(lbs): ";
cin >> weight;
cout << "Enter height(inches):";
cin >> height;
float BMI = weight * 703.0 / (height * height);
cout << "BMI = " << BMI << '\n';

C++ Airfare Charge Calculation project

I have to create a program to calculate charges for airfare. It's a simple program so far and I am not done adding to it, but every time I run it the result turns out to be 0. Is there something missing in my code? I am a beginner and I would appreciate any advice on improving my code. Thank you.
#include <iostream>
using namespace std;
void main () {
int distance = 0;
int num_bags= 0;
int num_meals= 0;
double distance_price = distance * 0.15;
double bag_price = num_bags * 25.00;
double meal_price = num_meals * 10.00;
double total_airfare = 0.00;
cout << "CorsairAir Fare Calculator" << endl;
cout << "Enter the distance being travelled: " << endl;
cin >> distance;
cout << "Enter number of bags checked: " <<endl;
cin >> num_bags;
cout << "Enter the number of meals ordered: " << endl;
cin >> num_meals;
total_airfare = (distance_price + bag_price + meal_price);
cout << total_airfare;
}
Your confusion is completely understandable - the piece you're missing is that when you assign a variable, you're assigning the left side to the result of the right side at that moment in time. It's not like algebra, where you say f(x) = x + 5 and f(x) is always whatever x + 5 is.
So, you assign double distance_price = distance * 0.15 when distance is 0 (which you just initialized). distance_price remains 0 even after you ask for input and change distance.
Do your price calculations after you ask for input, and everything will work just fine.
You are calculating the distance_price bag_price meal_price with default values i.e. 0 not with the value which you took from user.
Below code works fine and you won't see the issue.
#include <iostream>
using namespace std;
// My compiler did not allow void main so used int main
int main () {
int distance = 0;
int num_bags= 0;
int num_meals= 0;
double distance_price ;
double bag_price ;
double meal_price;
double total_airfare;
cout << "CorsairAir Fare Calculator" << endl;
cout << "Enter the distance being travelled: " << endl;
cin >> distance;
cout << "Enter number of bags checked: " <<endl;
cin >> num_bags;
cout << "Enter the number of meals ordered: " << endl;
cin >> num_meals;
distance_price = distance * 0.15;
bag_price = num_bags * 25.00;
meal_price = num_meals * 10.00;
total_airfare = 0.00;
total_airfare = distance_price + bag_price + meal_price;
cout << total_airfare;
return 0;
}
Result
CorsairAir Fare Calculator
Enter the distance being travelled:
200
Enter number of bags checked:
2
Enter the number of meals ordered:
2
100

A question about cin in C++

When I declare int weight and then input a double value 165.1 a 2nd cin >> height; doesn't work and there is no any error message. Can you tell me why?
VS2010 Console Application used.
#include <iostream>
using namespace std;
const double lbs_to_kg = 2.2046, inches_to_meter = 39.370;
int main()
{
int weight, height;
double kilograms, meters;
cout << "\nEnter weight in pounds: ";
cin >> weight;
kilograms = weight / lbs_to_kg;
cout << "\nEnter height in inches: ";
cin >> height;
meters = height / inches_to_meter;
cout << "\nYour BMI is approximately "
<< "\nbody fat ratio is "
<< kilograms / (meters * meters)
<< ". Under 25 is good."
<< endl;
}
output:
Enter weight in pounds: 165.1
Enter height in inches:
Your BMI is approximately
body fat ratio is 1.57219e-013. Under 25 is good.
If you try to have cin extract data into a variable that can't hold it, the data is left in the input stream and cin is flagged as having failed. You need to check if it's failed with !cin, and use cin.clear() to clear the fail flag so you can read again (future extract operations will automatically fail until the flag is cleared). You can either extract the data into a different variable that's capable of holding it, or use cin.ignore() to discard it