For loop issues

I have a problem with the for loop. It's not giving me the result I want. What is my issue?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	//declarations
	const double Y = 62.4; // weight of water in pounds per cubic feet
	const double 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.
Last edited on
closed account (zybCM4Gy)
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?
Last edited on
line 41 and it has to be a for loop. not sure what im doing for the for loop here.
oops. I misread what the program wanted. I got it solved now. Thanks everyone. I really do appreciate your help. You people rock!!! Keep it up.
Topic archived. No new replies allowed.