volume comparison [Solved]

i was making a program to have a user enter a radius, a integer of radius growth and a ending radius into a program, then comparing the volume differences in all the radius' of the spheres that were made, but i ave run into a snag, the while loop i was going to use dosent want to work for my purposes, and i am getting tired of this program altogether, could some1 just give me th next step of the problems i wil run into. I'm not asking for a full code of the solution, but that could always help

****** and no this is not some dumbass high school kid asking people to do his homework*********

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
#include <iostream>
#include <math.h>

using namespace std;

/* volume calculation*/

int main()
{
    double radini, old, current, radc, radend, vol, vol2, voldiff;
    cout<<"Enter the initial radius of the sphere: ";
    cin>>radini;
    cout << "Enter the incremental value of the radius: ";
    cin >> radc;
    cout << "enter the ending value of the radius: ";
    cin >> radend;
    vol = (4.0 / 3.0) * (3.14) * (pow(radini, 3.0));
    cout<<" The radius initialy is "<<radini;
    cout << "\n\n";
    current = radini;
    cout << " The volume with a radius of " << current;
    cout << " is " << vol;
    old = radini;
    
    while (current < radend)
    {
        current = radini + radc;
        voldiff = vol-vol2;
        vol2 = (4.0 / 3.0)* (3.14) * (pow(old, 3.0)); 
        vol = (4.0 / 3.0) * (3.14) * (pow(current, 3.0));
        voldiff = vol - vol2;

        cout << current << "\n the current radius is ";
        cout << vol << "\n the current volume is ";
        cout << vol2 << "\n the old radius is ";
        cout << "\n so the difference is " << voldiff;
        
        
        
        
    }
    
}
Last edited on
You never change the value of current. Line 27 should instead be current = current + radc;. Don't forget to set current to radini before the loop though or it will contain garbage.
Also, set old = current; right before changing current.
so just in a general sense, am i on the right track? i know python but i only started c++ about 4 weeks ago
i got it all to work, the new code is

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
#include <iostream>
#include <math.h>

using namespace std;

/* volume calculation*/

int main()
{
    double radini, old, current, radc, radend, vol, vol2, voldiff;
    cout<<"Enter the initial radius of the sphere: ";
    cin>>radini;
    cout << "Enter the incremental value of the radius: ";
    cin >> radc;
    cout << "enter the ending value of the radius: ";
    cin >> radend;
    vol = (4.0 / 3.0) * (3.14) * (pow(radini, 3.0));
    cout<<" The radius initialy is "<<radini;
    cout << "\n\n";
    current = radini;
    cout << " The volume with a radius of " << current;
    cout << " is " << vol;
    old = radini;
    
    while (current <= radend)
    {
        vol2 = (4.0 / 3.0)* (3.14) * (pow(old, 3.0)); 
        old = current;
        vol = (4.0 / 3.0) * (3.14) * (pow(current, 3.0));
        voldiff = vol - vol2;

        cout << "\n the current radius is " << current;
        cout << "\n the current volume is " << vol;
        cout << "\n the old volume is " << vol2;
        cout << "\n so the difference is " << voldiff;
        current = current + radc;

        
        
        
    }
    
}
Topic archived. No new replies allowed.