cannot find issue with code

I need a program that allows the user to input the initial velocity in fps, the firing angle in degree, and the time increment. I then need the program to for a table showing the time, x position, and y position. at the end it should also mention the maximum height, but I cannot seem to get the completed table or maximum height from 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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double deg, rad, x, y, fps, incrT, maxY;
    cout << "Enter velocity in fps: ";
    cin >> fps;
    cout << "Enter angle in degrees: ";
    cin >> deg;
    cout << "Enter time increment in seconds: ";
    cin >> incrT;
    const double G = 32.2;
    const double PI = 3.14159;
    rad = deg * (PI/180);
    cout << "t"<< "(s) ""      ""x"<< "(ft) ""      ""y"<< "(ft) " << endl;
    cout << "----        ----       -----" << endl;
    
    for (int t = 0; t > 100; t = t + incrT)
    {
        y = 0;
        maxY = 0;
        if (y >= 0)
        {
            x = (fps*cos(rad)*t);
            y = ((fps*sin(rad)*t) - (0.5*G*t*t));
            cout << t << "       "<< x << "       "<< y <<endl;
            if (y > maxY) maxY = y;
        }
        cout<< "max height = " << maxY<< endl;
        
    }
    return 0;
}
Hi,

for (int t = 0; t < 100; t += incrT)

How the the end condition work in a for loop?

I put in the += operator: it's better :+)

There are some other issues: be careful what you put inside the for loop.

Good Luck !!

Edit might be better with a while loop, so it ends in the appropriate place :+)
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double t, deg, rad, x, y, fps, incrT, maxY;
    
    cout << "Enter velocity in fps: ";
    cin >> fps;
    cout << "Enter angle in degrees: ";
    cin >> deg;
    cout << "Enter time increment in seconds: ";
    cin >> incrT;
    const double G =32.2;
    const double PI = 3.14159;
    rad = deg * (PI/180);
    cout << "t"<< "(s) ""      ""x"<< "(ft) ""      ""y"<< "(ft) " << endl;
    cout << "----        ----       -----" << endl;
   
    y = 0;
    t = 0;
    maxY = 0;
    
    while  (y >= 0)
    {
        x = (fps*cos(rad)*t);
        y = ((fps*sin(rad)*t) - (0.5*G*t*t));
        cout << t << "       "<< x << "       "<< y <<endl;
        if (y > maxY)
        maxY = y;
        t += incrT;
    }
    cout<< "max height = " << maxY<< endl;
    
    return 0;
}


Enter velocity in fps: 60
Enter angle in degrees: 45
Enter time increment in seconds: 1
t(s)       x(ft)       y(ft) 
----        ----       -----
0       0       0
1       42.4264       26.3264
2       84.8529       20.4528
3       127.279       -17.6209
max height = 26.3264
closed account (48T7M4Gy)
Or even better because less changes to OP:

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

int main()
{
    double deg, rad, x, y, fps, incrT, maxY;
    cout << "Enter velocity in fps: ";
    cin >> fps;
    cout << "Enter angle in degrees: ";
    cin >> deg;
    cout << "Enter time increment in seconds: ";
    cin >> incrT;
    const double G = 32.2;
    const double PI = 3.14159;
    rad = deg * (PI/180);
    cout << "t"<< "(s) ""      ""x"<< "(ft) ""      ""y"<< "(ft) " << endl;
    cout << "----        ----       -----" << endl;
    
    y = 0; // <--
    maxY = 0; // <--
    
    for (int t = 0; t < 100; t = t + incrT) // <---
    {
       
        if (y >= 0)
        {
            x = (fps*cos(rad)*t);
            y = ((fps*sin(rad)*t) - (0.5*G*t*t));
            cout << t << "       "<< x << "       "<< y <<endl;
            if (y > maxY) maxY = y;
        }
    } //<--
    cout<< "max height = " << maxY<< endl;
        
    return 0;
}


Enter velocity in fps: 60
Enter angle in degrees: 45
Enter time increment in seconds: 1
t(s)       x(ft)       y(ft) 
----        ----       -----
0       0       0
1       42.4264       26.3264
2       84.8529       20.4528
3       127.279       -17.6209
max height = 26.3264
Topic archived. No new replies allowed.