Nested For Loop Help

Hello, I'm having an issue trying to implement nested for loop. I must accept 4 user inputs (range of length and radius) and display a table of calculated values like so:

1
2
3
4
5
6
7
Radius Length Volume Area
1      1      *.**   *.**
1      2      *.**   *.**
1      3      *.**   *.**
2      1      *.**   *.**
2      2      *.**   *.**
2      3      *.**   *.**


My biggest issue is that I have spent hours stuck on trying to figure out HOW I could possibly get multiple "column 2" values for the same "column 1" value. My code isn't outputting the above, but rather outputs this:
1
2
3
4
5
6
7
8
9
10
[ Enter only positive integers! ]
Enter starting length: 1
Enter end length: 3
Enter starting radius: 1
Enter end radius: 3
Radius  Length  Volume  Area
1       1       3.14    6.28
1.00    1       3.14    6.28
1.00    1       3.14    6.28
1.00    1       3.14    6.28

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
56
57
58
59
60
#include <iostream>
#include <iomanip>

using namespace std; 

// func prototyping
float volCalc(float pi, float rad, float len);
float surfCalc(float pi, float rad, float len);

int main()
{

	float pi = 3.14159265;
	int radStart, radEnd, lenStart, lenEnd;

	// data i/o
	cout << "[ Enter only positive integers! ]" << endl
		 << "Enter starting length: ";
	cin  >> lenStart;
	cout << "Enter end length: ";
	cin  >> lenEnd; 
	cout << "Enter starting radius: ";
	cin  >> radStart;
	cout << "Enter end radius: ";
	cin  >> radEnd;

	cout << "Radius	Length	Volume	Area" << endl;

	// for each radius, there will be lenStart to lenEnd calculations
	int totalRows = (radEnd - radStart) * (lenEnd - lenStart);

	for (int rows = 0; rows < totalRows; rows++) // iterate through all rows
	{

		// column iterator
		for (int i = lenStart; i < (lenEnd - lenStart); i++) 
		{
			float radius = radStart, length = lenStart;

			// outputting each row
			cout << radius << setprecision(2) << fixed << "	"
				 << length << "	" << volCalc(pi,radius,length) << "	"
				 << surfCalc(pi,radius,length) << "	" << endl;
			radius++;
			length++;
		}

	}

	return 0;
}

float volCalc(float pi, float rad, float len)
{
	return (pi*rad*rad*len);
}

float surfCalc(float pi, float rad, float len)
{
	return (2.0f*pi*rad*len);


(P.S. Yes, this is part of the curriculum I am learning)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std; 

int main() {
    const double pi = 3.141592653589793238;
    int radStart = 1, radEnd = 3;
    int lenStart = 2, lenEnd = 4;
    cout << fixed << setprecision(2);
    cout << "Radius Length   Volume     Area\n";
    for (int rad = radStart; rad <= radEnd; ++rad) {
        for (int len = lenStart; len <= lenEnd; ++len) {
            cout << setw(6) << rad            << ' '
                 << setw(6) << len            << ' '
                 << setw(8) << pi*rad*rad*len << ' '
                 << setw(8) << 2*pi*rad*len   << '\n';
        }
    }
}

Radius Length   Volume     Area
     1      2     6.28    12.57
     1      3     9.42    18.85
     1      4    12.57    25.13
     2      2    25.13    25.13
     2      3    37.70    37.70
     2      4    50.27    50.27
     3      2    56.55    37.70
     3      3    84.82    56.55
     3      4   113.10    75.40

What the heck? I really appreciate your assistance, but I don't understand why this even works. Why did you use the incremental operator before the variable? Why didn't you do rad++/len++ instead? Finally, how did you know the iterator range (i.e. how did you know which start values to set the iterator to)?

Sorry about all the questions, but your answer really has saved me!
++len is called pre-increment; len++ is called post-increment. Although there is a difference (which you can look up), it doesn't actually matter in this instance. I prefer the pre-increment form in these cases although the post-increment form is more common.

The iterator range is just the inputs from your cin statments, like this:

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
#include <iostream>
#include <iomanip>
using namespace std; 

int main()
{
    const double pi = 3.141592653589793238;
    int radStart, radEnd, lenStart, lenEnd;

    cout << "[ Enter only positive integers! ]" << endl
         << "Enter starting length: ";
    cin  >> lenStart;
    cout << "Enter end length: ";
    cin  >> lenEnd; 
    cout << "Enter starting radius: ";
    cin  >> radStart;
    cout << "Enter end radius: ";
    cin  >> radEnd;

    cout << fixed << setprecision(2);
    cout << "Radius Length   Volume     Area\n";

    for (int rad = radStart; rad <= radEnd; ++rad)
    {
        for (int len = lenStart; len <= lenEnd; ++len)
        {
            cout << setw(6) << rad            << ' '
                 << setw(6) << len            << ' '
                 << setw(8) << pi*rad*rad*len << ' '
                 << setw(8) << 2*pi*rad*len   << '\n';
        }
    }
}

Topic archived. No new replies allowed.