Same response all the time

Hi,

This project is basically very simple. I think coding
wise looks okay. But when I run it, I always get the
same answer "The object will float".

I took C++ a couple of years ago at avc. Since then occasionally
I practice to refresh my mind. Any body can help, I will be grateful.
Also please let me if I can make the source code look better.
Thanks.
Shervin


/* Buoyancy is the ability of an object to float.
Buoyant force is equal to the weight of the fluid
that is displace by the submerged object.
If force is greater than or equal to 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 sepher will sink or
float in water.
*/

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()
{
double weight;
double radius;
const double WEIGHT_OF_WATER = 0.624; //Specific weight of water 62.4 lb/ft³
double volume;
double buoyantForce;
const double PI = 0.314;

cout<<"Enter weight of the object: ";
cin>>weight;
cout<<"Enter radius of a sphere: ";
cin >> radius;
volume = (4/3)*(PI * pow(radius, 3)); // volume of the sepher = (4/3)πr³
buoyantForce = weight * volume; //formula to calculate Buoyant force

cout <<"The buoyant force is "<<buoyantForce <<endl;
if(buoyantForce >= weight )
{
cout << "The object will float."<<endl;
}
else
{
cout <<"The object will sink."<<endl;
}

system("Pause");

return 0;
}
> const double WEIGHT_OF_WATER = 0.624; //Specific weight of water 62.4 lb/ft³

Is this not relevant? Where have you used it?
(4/3) will evaluate to 1. do (4.0/3.0).

also, i can get the object to float if i put 0.00001 as the weight and 0.00001 as the radius.

are you sure that's the equation for buoyant force?
Last edited on
const double PI = 0.314; Are you sure??

Below, the code and the comment differ by a factor of 100 ?
const double WEIGHT_OF_WATER = 0.624; //Specific weight of water 62.4 lb/ft³
Last edited on
Thanks all. I fixed it.
Topic archived. No new replies allowed.