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 :)
Related
This is my code. It runs correctly with custom example inputs, but fails when I submit it.
Also I don't know how to add constraints as given in question.
The link to the question is : https://www.codechef.com/submit/HS08TEST.
#include <iostream>
#include<iomanip>
using namespace std;
int main() {
// your code goes here
int r;
cin >> r;
float balance;
cin >> balance;
float amount;
if (r%5==0 && r<balance ){
amount = float(balance - r - 0.5);
cout << fixed;
cout << setprecision(2) << amount;
}
else {
cout<< fixed;
cout << setprecision(2) << balance;
}
return 0;
}
The account should have enough balance for bank charges.
#include <iostream>
#include<iomanip>
using namespace std;
int main() {
// your code goes here
int r;
cin >> r;
float balance;
cin >> balance;
float amount;
if (r%5==0 && (r+0.5) <= balance ){
amount = float(balance - r - 0.5);
cout << fixed;
cout << setprecision(2) << amount;
}
else {
cout<< fixed;
cout << setprecision(2) << balance;
}
return 0;
}
Consider constraint: 120 120.49
Your code outputs -0.01 for this while you can see answer should be 120.49 as when bank will charge you 0.5 for transaction, your balance become insufficient.
Try using this in the if condition:
if (r%5==0 && r+0.5<=balance)
I think you got where the code is missing.
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;
}
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 am trying to make an area calculator but I think I messed something up with defining the length and width.
I get an error saying error: uninitialized const 'length' [-fpermissive]| (same thing with the width)
I'm new to programming
#include <iostream>
#include <string>
using namespace std;
int main()
{
const char length;
const char width;
cout << "Please enter the your length: ";
cin >> length;
cout << "Please enter your width: ";
cin >> width;
string area =
length * width;
cout << " The area of these values is :" << area << "\n";
}
cin >> length;
Are you sure about variable declarations like const char length;
const actually indicates, you can't change these variable values, and trying to do so is undefined behavior.
Also note length should be of type size_t, not just unsigned char, which is only able to hold up a size of 255 maximum.
Your variable declarations of length and width shouldn't be const. The error you're getting is because const values need to be initialized (have a value assigned to them) when they are declared. They cannot have a value assigned to them which is what cin >> does.
There is a lot wrong with the code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
const char length; // Making these const will break as soon as you try to write to it
const char width; //
cout << "Please enter the your length: ";
cin >> length; // The fact you made it a char means it will only read the first char. "10" it would read "1"
cout << "Please enter your width: ";
cin >> width;
string area = // this will not work. There is no assignment operator for char to string
length * width;
cout << " The area of these values is :" << area << "\n";
}
Fixed
#include <iostream>
using namespace std;
int main()
{
float length;
float width;
cout << "Please enter the your length: ";
cin >> length;
cout << "Please enter your width: ";
cin >> width;
float area = length * width;
cout << " The area of these values is :" << area << "\n";
}
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