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 <iostream>
#include <limits>
#include <math.h>
/**
* 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<std::streamsize>::max(), '\n'); // skips to the next newline
std::cout << "Invalid input. Please enter a positive number: ";
}
int main()
{
constexpr double waterBuoyancy = 62.4;
Sphere obj;
char again = '\0';
do
{
// get input for height, re-read input if not a positive number
std::cout << "Enter the weight in pounds: ";
while(!(std::cin >> obj.weight) || obj.weight < 0) resetConsole();
// get input for radius, re-read input if not a positive number
std::cout << "Enter the radius in inches: ";
while(!(std::cin >> obj.radius) || obj.radius < 0) resetConsole();
double volume = ((4/3) * M_PI * pow(obj.radius, 3));
double bouyantForce = volume * waterBuoyancy;
if (obj.weight <= bouyantForce) std::cout << "The object will float." << std::endl;
else std::cout << "The object will sink." << std::endl;
std::cout << "Run the program again (y/N): ";
std::cin.get(); // absorb newline character from previous input
std::cin.get(again);
again = tolower(again);
if ('\n' == again) again = 'n';
} while (again == 'y');
}
The weight of the sphere
andThe height of the sphere
and no indication of what units they use. If you're lucky, whoever's maintaining this code realizes that the units are missing and tracks them down. If you're unlucky, they assume it's whatever units they're used to, and you lose half a billion dollars. \$\endgroup\$