HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMajor

If she floats then she is not a witch like we thought

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
shewitchthoughtlikethenfloatsnot

Problem

Continuing my C++ saga, this is the third project for my CS1 class:


Buoyancy is the ability of an object to float. Archimedes' 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


$$ F_b = V \cdot y $$


where \$ F_b \$ is the buoyant force. \$ V \$ is the volume of the
submerged object, and \$ y \$ is the specific weight of the fluid. If
\$ F_b \$ 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 \$ y = 62.4 \dfrac{\text{lb}}{\text{ft}^3} \$ as the
specific weight of water. The volume of a sphere is computed by \$ \left( \dfrac{4}{3} \right) \pi r^3\$.

Additionally, I must have a loop in this program that allows the user to run it as many times as they want to. I have to ask the user each time whether they want to continue.

It is also supposed to be fully commented, which I think I have done within reason.

buoyancy.cpp:

```
/**
* @file buoyancy.cpp
* @brief Calculates if a sphere will sink or float in water given the weight and radius
* @author syb0rg
* @date 9/12/14
*/

#include
#include
#include

/**
* Sphere
* @var weight The weight of the sphere
* @var radius The height of the sphere
*/
struct Sphere
{
double weight = 0;
double radius = 0;
};

/**
* Resets the command line so we can input data again, and signals user to re-enter proper data
*/
void resetConsole()
{
std::cin.clear(); // clear the error flag that was set so that future I/O operations will work correctly
std::cin.ignore(std::numeric_limits::max(), '\n'); // skips to the next newline
std::cout > obj.weight) || obj.weight > obj.radius) || obj.radius < 0) resetConsole();

double volume = ((4/3) M_PI pow(obj.radiu

Solution

There are several small things that you can improve:

-
double weight = 0; works fine, but double weight = 0.0; would be more pedantic since weight is a double.

-
By the way, the following line is a good example to illustrate the benefits of literals pedantry:

double volume = ((4/3) * M_PI * pow(obj.radius, 3));


Here, 4/3 performs an integer division and not a floating point one; that expression will yield 1 instead of 1.333333333 (I doubt that you want it to yield 1). You should change it to 4.0/3.0 to get the desired result.

-
That said, there is another problem with the aforementioned line: M_PI is not standard C++. It is not standard C either. It is a standard POSIX addition to `. You should rewrite your own constants or use Boost ones for examples if you want your code to be portable.

-
Speaking of
... You are using C++, therefore you should use the C++ standard library headers too and not the C ones:

#include 


-
And you notably forgot to include the header
for std::tolower.

-
That said, it is good practice in C++ to fully qualify the names of the functions from the standard library: you should write
std::tolower (and thus actually include instead of ). It's not really longer and it may help to prevent name clashes (I doubt that you will have problems with tolower`, but it can be worse with some more common names).

Code Snippets

double volume = ((4/3) * M_PI * pow(obj.radius, 3));
#include <cmath>

Context

StackExchange Code Review Q#63708, answer score: 31

Revisions (0)

No revisions yet.