I', trying to make a simple BMI calculator using C++. When I input my personal height and weight, I get the correct result but I can't round the number to the nearest whole number. I looked up some videos and some articles and many of them suggested using the "round()" function. I tried that and the result I got was 0!
All feedback helps. Thanks!
#include <iostream>
#include <cmath>
using namespace std;
float Calculate(float kilo, float centimeter)
{
float meter = centimeter * 100;
return kilo / (meter * meter);
}
int main()
{
float kilo, centimeter;
float bmi;
cout << "BMI calculator." << endl;
cout << "Please enter your weight in kilograms. ";
cin >> kilo;
cout <<"Please enter your height in centimeters. ";
cin >> centimeter;
bmi = Calculate(kilo, centimeter);
cout << round(bmi) << endl;
return 0;
}
Your formula for calculating BMI is wrong just change the line float meter = centimeter/100;.
because according to your formula any number multiplied by 100 and then squared becomes so big that you get very small floating point number after the division with weight that is eventually rounded that's why you always get 0 in output.
Related
Heres the given:
Write a program to compute for the surface area and volume of a sphere if the unit of the radius
is in centimeters (cm).
Filename: exer10.cpp
Formulas: area = 4*pi*radius2
Volume = (4/3)*pi*radius3
I was skeptical because of the "if" on the given as you read it, now I don't know if what I did is right. But I have some ideas on how to do it
I Have a 2 ideas in mind 1st is where if I input a value there will be a formula to distinguish if that value is in centimeter, the thing is I don't know how.
2nd idea is I will use the if else method where after I input a value, it will ask if its in centimeter or not, if I type "Y" it will do its thing and continue its computation but if I type "N" it`ll will not compute and end the program.
Any suggestion guys?
By the way here's my code (My given ideas are not written here)
#include <iostream>
using namespace std;
int main()
{
float radius, area, volume;
cout << "This program computes for the surface area and volume of a sphere (centimeters)"
"\n\n";
cout << "Input the radius of a sphere : ";
cin >> radius;
area = (4 * 3.1416 * radius * radius);
volume = (4 / 3) * (3.1416 * radius * radius * radius);
cout << "The surface area of a sphere is : " << area << "\n";
cout << "The volume of a sphere is : " << volume;
return 0;
}
You can ask the user to input the unit. Consider this example, if it's in centimeters then perform the operation/task or else exit the program.
#include <iostream>
#include <string>
#include <cstdio>
int main() {
int radius;
std::string unit;
std::cout << "Enter radius of sphere in centimeters (e.g. 5 cm) : ";
std::cin >> radius >> unit;
if (unit != "cm") {
std::cout << "\nPlease enter in centimeters (e.g. 5 cm)";
exit(1);
}
// perform operation
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
/* FINDS AND INITIALIZES TERM */
void findTerm(int t) {
int term = t * 12;
}
/* FINDS AND INITIALIZES RATE */
void findRate(double r) {
double rate = r / 1200.0;
}
/* INITALIZES AMOUNT OF LOAN*/
void findAmount(int amount) {
int num1 = 0.0;
}
void findPayment(int amount, double rate, int term) {
int monthlyPayment = amount * rate / ( 1.0 -pow(rate + 1, -term));
cout<<"Your monthly payment is $"<<monthlyPayment<<". ";
}
This is the main function.
int main() {
int t, a, payment;
double r;
cout<<"Enter the amount of your mortage loan: \n ";
cin>>a;
cout<<"Enter the interest rate: \n";
cin>>r;
cout<<"Enter the term of your loan: \n";
cin>>t;
findPayment(a, r, t); // calls findPayment to calculate monthly payment.
return 0;
}
I ran it over and over again, but it still gives me the incorrect amount.
My professor gave us an example that goes like this:
Loan=$200,000
Rate=4.5%
Term: 30 years
And the findFormula() function is supposed to produce $1013.67 for the mortgage payment. My professor gave us that code as well (monthlyPayment = amount * rate / ( 1.0 – pow(rate + 1, -term));). I'm not sure what's wrong with my code.
The formula may be fine, but you are not returning, nor using, any value from your conversion functions, so its inputs are wrong.
Consider this refactoring of your program:
#include <iostream>
#include <iomanip> // for std::setprecision and std::fixed
#include <cmath>
namespace mortgage {
int months_from_years(int years) {
return years * 12;
}
double monthly_rate_from(double yearly_rate) {
return yearly_rate / 1200.0;
}
double monthly_payment(int amount, double yearly_rate, int years)
{
double rate = monthly_rate_from(yearly_rate);
int term = months_from_years(years);
return amount * rate / ( 1.0 - std::pow(rate + 1.0, -term));
}
} // end of namespace 'mortgage'
int main()
{
using std::cout;
using std::cin;
int amount;
cout << "Enter the amount of your mortage loan (dollars):\n";
cin >> amount;
double rate;
cout << "Enter the interest rate (percentage):\n";
cin >> rate;
int term_in_years;
cout << "Enter the term of your loan (years):\n";
cin >> term_in_years;
cout << "\nYour monthly payment is: $ " << std::setprecision(2) << std::fixed
<< mortgage::monthly_payment(amount, rate, term_in_years) << '\n';
}
It still lacks any checking of the user inputs, but given the values of your example, it outputs:
Enter the amount of your mortage loan (dollars):
200000
Enter the interest rate (percentage):
4.5
Enter the term of your loan (years):
30
Your monthly payment is: $ 1013.37
The slightly difference from your expected output (1013,67) could be due to any sort of rounding error, even a different overload of std::pow choosen by the compiler (since C++11, the integral parameters are promoted to double).
I was doing a program which first takes 2 numbers (with float datatype) from the user and then ask the user about up-to what digit he want's to get the number divided and finally divides it up-to that number and 'cout<<' it. It compiled but din't worked up-to the mark when I calculated 22/7 which is an irrational no. up-to 100 digits it just calculated up-to 30 or 40 digits and then rest of was filled with zeros. Something like this:
3.1428570747375488281250000000000000000000000000000000000000000000000000000000000000000000000000000000
Here is my code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
system("clear");
float y;
int z;
float x;
float a;
cout << "\nHello User\n";
cout << "\nEnter first num to be divided: ";
cin >> x;
cout << "\nCool!! Now enter the 2nd number: \n";
cin >> y;
cout << "\Exelent!! Enter the place upto which u wanna caculate: ";
cin >> z;
a = x / y;
cout << fixed << showpoint;
cout << setprecision(z);
cout << "Calculating......\n" << a << endl;
return 0;
}
Floating point types have certain precision. You don't get exact results when operating on floats (or doubles). Now to get a better precision use double instead of float (See this post for more details).
You could #include <limits>, remove the step that gets the precision from input and change your code to:
std::cout << std::setprecision(std::numeric_limits<float>::max_digits10);
to display the result with maximum precision for the type you use.
I attempted to make a program that asks for the input for the radius of a sphere, and the weight of the sphere. it uses these inputs to calculate the buoyancy of the sphere and the program determines whether or not it can float in water. However, I keep getting "6.95207e-308", no matter what my inputs are
These are the instructions for the programming assignment:
"Buoyancy is the ability of an object to float. Archimede's Principle
states that the buoyant force is equal to the weight of the fluid that
is displaced by the submerged object. The buoyant force can be
computed by:
buoyant force = (object volume) times (specific gravity of the fluid)
If the buoyant force is greater than or equal to the weight of the
object then it will float, otherwise it will sink.
Write a program that inputs the weight (in pounds) and radius (in
feet) of a sphere and outputs whether the sphere will sink or float in
water. Use 62.4 lb/cubic foot as the specific weight of water. The
volume of a sphere is computed by (4/3)π times the radius cubed."
This is my code:
//Written by: Edward Santiago
//Assignment: HW_03_No14.cpp
//Class: CO SCI 243
//Date: October 17, 2014
//Description: Prime Numbers
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
double sphere_volume (double);
double buoy_force (double,double);
int main ()
{
double rad,volume,force,buoyancy;
double weight;
double water = 62.4;
cout << "~~Buoyancy calculator~~" << endl;
cout << "Please enter the radius of your sphere" << endl;
cin >> rad;
cout << "Please enter the weight of your sphere" << endl;
cin >> weight;
volume = sphere_volume(rad);
buoyancy = buoy_force(volume,weight);
cout << "The force of your sphere is "<<force<<endl;
if (force <= water)
cout << "Your sphere will float in the water"<<endl;
else
cout <<"Your sphere will sink :( "<<endl;
return 0;
}
double sphere_volume (double radius)
{
double vol;
vol = ((4/3) * (M_PI) * (pow(radius,3)));
return vol;
}
double buoy_force (double vol,double weight)
{
double force;
force = vol * weight;
return force;
}
You never initialize force.
buoyancy = buoy_force(volume,weight);
cout << "The force of your sphere is "<<force<<endl;
Change the assignment to force = buoy_force(volume, weight).
You assigned the big answer to "buoyancy" and then printed out "force".
Add
force=buoyancy;
just before printing force. This is because force is uninitialized and you are trying to print it.
I'll cut to the chase: I made a program in C++ that calculates if a spherical object is bouyant or not for a class. However, after I (from what I thought) successfully made the program in Visual Studio 2013 when I submitted it where I need to (Pearon's terrible myProgrammingLab) I get the wrong output compared to Pearon's. (IE: Mine says it floats, they say it sinks, but don't show the calculations themselves.)
Here is my code:
// Bouyancy formula:
// Fb = V * y
// Where:
// Fb is the bouyant force
// V is the volume of the submerged object
// y is the specific weight of the fluid
// If Fb is greater than or equal to the weight of the object, then it will float, otherwise it will sink.
// Sphere volume formula:
// (4/3)pi(radius cubed)
#include "stdafx.h"
#include <iostream>
#define _USE_MATH_DEFINES // Used with math.h to provide access to M_PI (used in calculation of volume)
#include <math.h> // M_PI is the value of pie (3.14..)
using namespace std;
int main()
{
float sphere_radius, sphere_weight; // Stores the value of the Sphere's radius and weight in feet and pounds respectively.
double water_weight = 62.4; // Value set to 62.4lb /cubic feet, this value is the "y" value in the above formula.
double bouyant_force, volume; // Defines Fb and V in the Bouyancy formula listed above.
cout << "Enter the radius of the sphere, in feet: ";
cin >> sphere_radius;
cout << "\nEnter the weight of the sphere, in pounds: ";
cin >> sphere_weight;
cout << endl;
volume = ((4.0 / 3.0) * M_PI * (pow(sphere_radius, 3))); // Calculates the volume of the sphere
bouyant_force = (volume * water_weight);
if (bouyant_force >= sphere_weight)
{
cout << "The sphere will float." << endl;
}
else if (bouyant_force < sphere_weight)
{
cout << "The sphere will sink." << endl;
}
else { cout << "Something went terribly, terribly, wrong.. Oh dear.."; }
char x;
cin >> x; //Waits for user to press a key before closing the program.
return 0;
}
Can anyone please help me understand why this isn't correct or why it isn't being registered as correct? Thanks in advance!
Judging by your code, the error seems to be you directly comparing the weight you accept against the buoyant force. You should be multiplying the mass you accept(the pound is a unit of mass) by g in the unit system you are using. That seems to account for you getting that it flaots while the other side calculates that it sinks.