#include "stdafx.h"
#include <iostream>
#include <cstring>
usingnamespace std;
int main()
{
//declarations
constdouble Y = 62.4; // weight of water in pounds per cubic feet
constdouble PI = 3.141; //pi
double r, num1, V, B, V2; // V and V2 are for volume, B for buoyancy force
//input
cout << "Enter the number of sphere(s) that you want to have.\n";
cin >> num1;
cout << "Enter the radius of your sphere(s) here in feet.\n";
cin >> r;
V = (4.0 / 3) * (PI)* (r * r * r);
V2 = num1 * V;
if ((V2 <= 0) && (r <= 0) && (num1 <= 0) && (V <= 0))
{
cout << "Error. Please try again.\n";
return 0;
}
else
{
cout << "The weight of your object(s) is\n";
cout << V2 << " feet cubed.\n";
}
//Processing
B = V2 * Y;
//output
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout << "Your force of buoyancy is\n";
cout << B << " pounds per feet cubed.\n";
for (B = 0; B >= 0; B++)
{
if (B >= V2)
{
cout << "The sphere floats.\n";
return 0;
}
else
{
cout << "The sphere sinks.\n";
return 0;
}
}
}
Why do you need a for loop there?
In any case, your for loop resets B to 0, which is probably not what you want.
Does it run correctly if you comment out line 41?
EDIT: Oh wait, I see what you're trying to do.
You're looping over the wrong piece of code....
If you want to be able to input num1 spheres, put your for loop around all of your code from line 16 to line 54. And don't make the variable B (you already have a B)...make it i or something.
Either you need a new variable OR you should be running a while loop.
so
1 2 3 4
while( B >= 0; B--) //We actually want this loop to terminate right?
{
...
}
Edit.
I don't actually see what's wrong with the code except that rather unused include at the top.
Enter the number of sphere(s) that you want to have.
1
Enter the radius of your sphere(s) here in feet.
2
The weight of your object(s) is
33.504 feet cubed.
Your force of buoyancy is
2090.650 pounds per feet cubed.
The sphere sinks.
At what point are you not getting the output you want?