Nested for loop help or other..

Hi this is my first time being in this website and i am pretty much newbie with programming.

Straight forward, here's my question:

Is 3 nested for loops even possible?

Here's my code
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cmath>
#include <string.h>
#include <limits.h>
#include <iomanip>

using namespace std;

const double gravity = 9.8;

int main()
{
	//Declaration
    {
	double angleDeg = 0.0, v = 0.0, angleRad = 0.0, R = 0.0, x = 0.0, y = 0.0, T = 0.0, t = 0.0;// v = velocity
    bool done = false;

	//input
    cout <<"What's the angle?" <<endl;
    cin >> angleDeg;

        while (angleDeg < 0 || angleDeg > 90){  // between 0 to 90
            cout << "Between 0~90 angle please :)" << endl; // Seems like this line is ignoring the value..?

        while(!(cin >> angleDeg)) // Run this loop as long as the cin value fails.
        {

        	cin.clear(); // if fails, clear the failed flag.
            while(cin.get() != '\n'){} // A loop the cleans out anything that is left over in the input stream.

            cout << "Only numbers please: " << endl << "What's the angle again?" <<endl;// Output failed prompt.

        }



                                            }

    cout << "Velocity?" <<endl;
    cin >> v;
        while (v < 0){  // between 0 to 90
            cout << "You know you can't go negative.. :)" << endl;

        while(!(cin >> v)) // Run this loop as long as the cin value fails.
        {

        	cin.clear(); // if fails, clear the failed flag.
            while(cin.get() != '\n'){} // A loop the cleans out anything that is left over in the input stream.

            cout << "Only numbers please: " << endl << "What's the velocity again?" <<endl;// Output failed prompt.

        }



                     }
    //processing
	angleRad = angleDeg*M_PI/180.0;
    R = (pow(v, 2.0)/gravity)*sin(2.0*angleRad); // = (v^2/g)sin(2x) Trajectory formula
    T = (2.0*v*sin(angleRad))/gravity; // Total time
    x = (v*cos(angleRad)); // x coordinate
    y = (v*sin(angleRad)); // y coordinate

        {

    //output
    cout << "Your total distance: " << R << "meters" << endl << endl;
    cout << "Your travel time is: " << T << "s" << endl << endl;
    cout << "Max X coordinate: " << x << "meters" << endl << endl;
    cout << "Max Y coordinate: " << y << "meters" << endl << endl;

            }
    double a = R/20;
    double b = x/20;
    double c = y/20;

    for (double i = R/20; i <= R; i = i + a)// gives me constant numbers..
    {
       for (double o = x/20; o < x; o = o + b)// gives me 3 of 10 constant numbers

            for (double p = y/20; p < y; p = p + c)// give ms 3 of 10 different numbers(what i want)

        cout << i << " " << o << " " << p << endl;



    cout << "Thank you for using the program!" << endl;
   return 0;
            }
        }
    }





If you look at for loops part, I do not get errors, how ever I do not get values that i wanted.
For example i suppose to get:

1 5 10
2 7 11
3 8 12
4 9 13
5 10 14
6 11 15
7 12 16
... so on

But I am getting:

1 2 10
1 2 11
1 2 12
1 2 13
1 2 14
1 2 15
1 2 16
1 2 17
^
multiply that by 3

What's my problem and How do I fix it?
Reformatting the for loops area:

1
2
3
4
5
6
7
8
9
10
	for (double i = R/20; i <= R; i = i + a)// gives me constant numbers..
	{
		for (double o = x/20; o < x; o = o + b)// gives me 3 of 10 constant numbers

			for (double p = y/20; p < y; p = p + c)// give ms 3 of 10 different numbers(what i want)
				cout << i << " " << o << " " << p << endl;

		cout << "Thank you for using the program!" << endl;
		return 0;
	}


Do you really intend to end the program during your first iteration of the outer for loop?

Am I not suppose to?

Is there any better way rather than using nested loops?
Because the outer loop will never execute more than once, i will never be anything but one value. So you tell me.. are you not supposed to? What's the point of having a loop that.. cannot loop?

Hmmm... So do i gotta use string in here to store the datas for three lines? I thought I can use three different variable in for loops/nested loops, guess not.
Topic archived. No new replies allowed.