I'm trying to do a basic BMI calculator, and it works fine when I do the metric side of it, however, the imperial side returns a wrong answer.
cout << "What is your weight? ";
cin >> weight;
cout << "What is your height in either inches or meters? ";
cin >> height;
cout << "Is that metric or imperial? Type 1 for metric, or 0 for imperial ";
cin >> unit;
if (unit = 1)
answer = weight / (height * height);
if (unit = 0)
answer = (weight * 703) / (height * height);
cout << "Your BMI is " << answer << endl;
system("PAUSE");
return 0;
I thought the equation for BMI for imperial, according to Wikipedia was
Weight(in pounds) * 703 / (height in inches squared)
Use == for testing equality not =
You are using the assignment operator "=" in your if statements, both of which will ALWAYS return true, because you ARE able to assign the value of "0" or "1" to unit.
To test for equality you must use "==".
This should fix this problem! (:
Related
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
int weight;
string planet;
if (planet== "Mercury")
weight * 0.4155;
else if (planet== "Venus")
weight * 0.8975;
else if (planet== "Earth")
weight * 1.0;
else if (planet== "Mars")
weight * 0.3507;
else if (planet== "Jupiter")
weight * 2.5374;
else if(planet== "Saturn")
weight * 1.0677;
else if(planet== "Uranus")
weight * 0.8947;
else if(planet== "Neptune")
weight * 1.1794;
else if(planet== "Pluto")
weight * 0.0899;
else (planet != "Mercury" && planet != "Venus" && planet != "Earth" && planet != "Mars"
&& planet != "Jupiter" && planet!= "Saturn" && planet!= "Uranus" && planet!= "Neptune" &&
planet!= "Pluto")
"Unknown Planet";
cout << "Assignment 5" << endl;
cout << "The program is written by Micheal" << endl;
cout << "Please Enter Weight (as an integer of pounds): ";
cin >> weight;
cout << "Please Enter Planet name (ie: Earth): ";
cin >> planet;
cout << "Enter entered a weight of" << weight << "and a planet name of" << planet << endl;
// cout << "On" << planet << "your weight in pounds would be" <<
system("pause");
return 0;
}
Hello Everyone,
The purpose of my project is to take what the user's weight is and convert their weight to what it would be on a different planet and to have them enter the name of the planet. For example, if I weigh 175 pounds on Earth, I would weigh 72.71 on Mercury. 0.4155 * 175 and I already have all the other ratio calculations. But my question is how do I use my nested if-else statements to output the correct calculated weight to last line of this expression: cout << "On" << planet << "your weight in pounds would be" <<. I have attached my C++ code above.
Your if-else statements are currently useless: they get executed before planet is initialized.
You should:
Read user input.
Modify weight according to the planet (ifs go here).
Print modified weight.
Check your code carefully and you'll see where you go wrong.
You're also not updating the weight variable inside the ifs, which you should (or define a new variable weight_at_planet = weight * 0.855, for example), and print that.
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 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';
I Have this code to find out the total purchase price of some numbers and i need to know how to round the numbers to only 2 decimal places.
#include <iostream.h>
#include <cstdlib.h>
#include <string.h>
int main() {
float sale_price;
float tax_rate;
float discount_rate;
system("cls");
system("color 07");
cout << "\n\nWelcome to the second version of my total purchase price calculator!\n";
cout << "How much did your recently purchased item cost? ";
cin >> sale_price;
cout << "What is the tax rate in your area? ";
cin >> tax_rate;
cout << "What was the discount rate, if any (if none, just put down 1) ";
cin >> discount_rate;
float tax = sale_price*(tax_rate/100);
float discount = sale_price*(discount_rate/100);
float total_price = sale_price + tax - discount;
cout << "The total price of your item is $"<<total_price<<" with $"<<tax<<" tax minus $"<<discount<<" due to discount.\n";
cout << "Would you like a reciept? y or n. ";
string answer;
End:
cin >> answer;
if (answer == "y") {
goto Reciept;
}
else if (answer == "n") {
return 0;
}
else {
cout << "Try another answer\n";
goto End;
}
Reciept:
system("cls");
system("color 70");
cout << "\xda\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xbf\n";
}
this usually gives me 4 decimal places just FYI
Round to 2 decimals: int((n * 100.0) + 0.5) / 100;
Round to 3 decimals: int((n * 1000.0) + 0.5) / 1000;
etc.
The easy way is to use setprecision:
std::cout << "The total price of your item is $"
<< std::setprecision(2) << total_price ...
This will occasionally get things wrong. A better solution is to use an improved rounding function that is not a part of the standard (e.g., How does Excel successfully Rounds Floating numbers even though they are imprecise?). An even better solution is to implement your own fixed point arithmetic algorithm so as to avoid this problem entirely. You can still go one better than that: Use a fixed point arithmetic package that someone else has already written and tested to the nth degree.
You should use Iomanip's setprecision:
setprecision reference
When you output with cout, use the setprecision() operator.
cout << fixed;
cout << setprecision(2) << sale_price*(tax_rate/100);
Do
cout.precision(2);
cout << fixed;
before you output the floats.
I'm really new to C++, and this is homework. I don't understand what's going on. When I run this, everything works fine if I put in 60000 for current salary and .05 for pay increase, but if I put in something like 52000 for current salary and .23 for pay increase, then the retroactive pay comes out as 5982.00 instead of 5980.00. Is it something to do with the decimals or something? I don't really know. Thanks in advance.
// Variables
char fullName[30]; // The user's full name - INPUT
int currentAnnual; // The users's current annual salary - INPUT
float percentIncrease; // The percent increase due on the
// current annual salary - INPUT
// The retroactive pay
float retroactive;
// The new monthly salary based on the pay increase and the new salary
float monthlySalary;
// The new salary the user should receive based on their old salary
// and their pay increase
float newSalary;
for(int lcv = 1; lcv <= 3; lcv++)
{
// INPUT
cout << "What is your full name? ";
cin.getline(fullName, 30);
cout << "What is your current salary? ";
cin >> currentAnnual;
cout << "What is your pay increase (please input percentage in"
"decimal format)? ";
cin >> percentIncrease;
// PROCESSING
newSalary = (currentAnnual * percentIncrease) + currentAnnual;
monthlySalary = newSalary / 12;
retroactive = (monthlySalary - (currentAnnual / 12)) * 6;
// OUTPUT
cout << endl;
cout << fullName << "'s Salary Information\n";
cout << left << setw(15) << "New Salary" << setw(19) << "Monthly Salary"
<< "Retroactive Pay\n";
cout << setprecision(2);
cout << fixed;
cout << right << setw(10) << newSalary << setw(19) << monthlySalary
<< setw(20) << retroactive << endl << endl;
cout << "<Press enter to continue>";
cin.ignore(100, '\n');
cin.ignore(1000, '\n');
cout << endl;
}
currentAnnual / 12
The division of two integers in C is an "integer division" (it gives an integer), and I think you dont want that. One solution is to change it to currentAnnual / 12.0. Anyway, it's important that you understand what is happening here.
Change currentAnnual / 12 to currentAnnual / 12.0 to force a floating-point calculation to be done. Otherwise, that part of the computation will be rounded off to the nearest integer below.
You should do monthlySalary = newSalary / 12.0; instead of monthlySalary = newSalary / 12;
so you have to specify that you want to divide on float number. Otherwise result will be integer. For example 125./12 = 10 but 125./12. = 10.41(6). And of course you get wrong results.
Try to add .0 or just . to all your constants.
Floating point calculations are not as accurate as you might expect. Try replacing float with double in the above code (double is a "double-precision floating-point number"; it can store many more values than a float, and is much more accurate).
Also, order of operations is sometimes significant, so you may want to re-order the retroactive calculation to something like
6 * monthlySalery - currentAnnual / 0.5;
and try other combinations to see what works best.