Stuck on Final bit of for looping code

Hello just looking for a little bit of help to finish my code. I have it up and running the only problem is my for looping. I've tried many ways to figure it out but none works not to mention am stuck on an infinite loop. I need the program output to look like this so far everthing works except for the
"At 1.44 seconds: 91.77 meters high" part my loop never goes on to the next step. Any help would be appreciated.The code is below


// OUTPUT
7. Lets see how far a projectile can be launched!
8.
9 You are on the surface of Earth at a gravity of 9.81 meters per second squared.
10.
11. Please enter the initial angle of your projectile (0.0 to 90.0): 45
12. Please enter the initial velocity your projectile (0.0 to 999.99):100
13. Please enter the number of height measurements to display (0 to 99):10
14.
15. At 1.44 seconds: 91.77 meters high
16. At 2.88 seconds: 163.15 meters high
17. At 4.33 seconds: 214.14 meters high
18. At 5.77 seconds: 244.73 meters high
19. At 7.21 seconds: 254.93 meters high
20. At 8.65 seconds: 244.73 meters high
21. At 10.09 seconds: 214.14 meters high
22. At 11.54 seconds: 163.15 meters high
23. At 12.98 seconds: 91.77 meters high
24. At 14.42 seconds: 0.00 meters high
25.
26. Total Time: 14.42
27. Total Distance: 1019.72
28. Height at Apogee: 254.93








#include<iostream>
#include<cmath>
#include<iomanip>
#include<math.h>
#include<stdio.h>
using namespace std;


const double GRAVITY = 9.80665;
double Angle(double Angle);
double Velocity(double Velocity);
double increments(double increments);

int main()
{

const double GRAVITY = 9.80665;
double Angle;
double Velocity;
double increments;

cout<<"Lets see how far a projectile can be launched!"<< endl;

cout<< endl;

cout<<"You are on the surface of Earth at a gravity of 9.81 meters per secound squared."<<endl;

cout<<endl;


cout<<"Please enter the initial angle of your projectile (0.0 to 90.0):";

for (cin >> Angle; Angle <= 0.0 || Angle >= 90.0; cin >> Angle)
{
cout<<"Please enter the initial angle of your projectile (0.0 to 90.0):";
}


cout<<"Please enter the initial velocity your projectile (0.0 to 999.99):";


for (cin >> Velocity; Velocity <= 0.0 || Velocity >= 999.99; cin >> Velocity)
{
cout<<"Please enter the initial angle of your projectile (0.0 to 999.99):";
}

cout<<"Please enter the number of height measurments to display (0 to 99):";


for (cin >> increments; increments <= 0 || increments >= 99; cin >> increments)
{
cout<<"Please enter the number of height measurments to display (0 to 99):";
}



double totTime,
totDist,
maxHeight;

Angle = M_PI / 180 * Angle; // Converts angle degrees to radians

totTime = (2.0 * Velocity * sin(Angle)) / GRAVITY;
totDist = Velocity * cos(Angle) * totTime;
maxHeight = Velocity * sin(Angle) * totTime / 2 - 0.5 * GRAVITY * pow(totTime / 2.0, 2.0);
metersHigh = velocity * sin(angle) * time - 0.5 * GRAVITY * pow(time, 2.0)


cout<<endl;

// This is were my problems at
for ( double i = totTime/increments; totTime/increments < increments; i++)
{
cout<<"At"
<< i
<<"seconds:"
<< endl;
}


//for ( double i =

cout<<maxHeight
<<"meters high";
break;
// }

cout<<endl;


cout<<endl;

cout << "time: " << totTime << endl;
cout << "distance: " << totDist << endl;
cout << "height at Apogee: " << maxHeight << endl;


system ("pause");
return EXIT_SUCCESS;
}

closed account (zwA4jE8b)
totTime/increments < increments must be in terms of i...

for loops are usually just counters. (well at least in this case)
1
2
3
4
for (i = start value; i <= end value; i++)
{
   do you calculation and output it here.
}

in this case end value is the number of height measurements to display....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (cin >> Angle; Angle <= 0.0 || Angle >= 90.0; cin >> Angle)
 {
 cout<<"Please enter the initial angle of your projectile (0.0 to 90.0):";
 }


 cout<<"Please enter the initial velocity your projectile (0.0 to 999.99):";


 for (cin >> Velocity; Velocity <= 0.0 || Velocity >= 999.99; cin >> Velocity)
 {
 cout<<"Please enter the initial angle of your projectile (0.0 to 999.99):";
 }

 cout<<"Please enter the number of height measurments to display (0 to 99):";


 for (cin >> increments; increments <= 0 || increments >= 99; cin >> increments)
 {
 cout<<"Please enter the number of height measurments to display (0 to 99):";
 }


should all be while loops.

1
2
3
4
while (not a valid value)
{
   get a value;
}


http://cplusplus.com/doc/tutorial/control/

this link will help
Last edited on
Thanks for the advice as for all the looping in the beginning I found it easier that why. It just worked for me. Am new to programing so sometimes I program a longer more time consuming way useing another method to get some practice time.
closed account (zwA4jE8b)
thats cool, and if those 3 work thats good.

but for this one..

1
2
3
4
5
6
7
8
// This is were my problems at 
 for ( double i = totTime/increments; totTime/increments < increments; i++)
 {
 cout<<"At"
 << i
 <<"seconds:"
 << endl;
 }


the structure is wrong.

all it does is output i, and the condition is incorrect because you increment i but i is not tested.

1
2
3
4
totTime = (2.0 * Velocity * sin(Angle)) / GRAVITY;
 totDist = Velocity * cos(Angle) * totTime;
 maxHeight = Velocity * sin(Angle) * totTime / 2 - 0.5 * GRAVITY * pow(totTime / 2.0, 2.0);
 metersHigh = velocity * sin(angle) * time - 0.5 * GRAVITY * pow(time, 2.0)


this creates an intial set of values but is never updated.

if the for loop needs to represent units of time then you could do something like....

1
2
3
4
5
6
for (int i = 0; i < increments; i++)
{
   use i as the time variable in your calculations in here;
   calculate needed data;
   output data;
}


if the time is in second then when i = 0 is time 0, i = 1 is one second, and so on.

if you need different deltas then you can change the way i increments.

1
2
3
4
5
6
for (double i = 0; i < increments; i += (totTime/increments))  // or whatever value you need to increment by.
{
   use i as the time variable in your calculations in here;
   calculate needed data;
   output data;
}


this way your i will increment appropriately
Last edited on
Topic archived. No new replies allowed.