bool function causes runtime termination

Can I have a little help troubleshooting? I am working on a program that will calculate altitude and acceleration from time and velocity inputs in a vector. My code is written, but I am not getting a correct output. I am not exactly sure how to implement a bool function to begin with. Can someone give me some tips with this? Using cout commands, I know that my function works until I get to line 49. I cut that line out a part of my attempt to fix the problem.

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cmath>
#include <fstream>
#include <vector>

using namespace std;

bool calculateAccelerationArray(vector<double> t, vector<double>& p, vector<double> v, vector<double>& a);
void displayAcceleration(vector<double> t, vector<double> p, vector<double> v, vector<double> a);
    vector<double> t, v, a, p, n;
    double Number=0;
    double Time, Velocity, Acceleration, Altitude;
int main(int argc, char *argv[])
{
    const int Length_of_Line(50);
    char quit, string1[Length_of_Line];
    std::ifstream Rocket_Data_Input;
    Rocket_Data_Input.open("rocketVelocityData.txt");     //This will open the input file.
    Rocket_Data_Input.getline(string1,Length_of_Line);
    if (Rocket_Data_Input.fail()) // Checks for proper opening of the report.
        {
        cerr << " Your Report failed to open. \n";
        }
//    cout << " Number  " << "  TIME  " << "    Velocity    " << endl;
while (!Rocket_Data_Input.eof()) // Takes data from input file.
    {
    Rocket_Data_Input >> Time >> Velocity;
//    cout << "   " << left << setw(5) << Number << "   " << left << setw(8) << Time
//        << left << setprecision(12) << setw(16) << Velocity << endl;
    t.push_back(Time);
    v.push_back(Velocity);
    }
calculateAccelerationArray(t, p, v, a);
displayAcceleration(t, p, v, a);

    quit = '\0';        //slows down the logout to allow user time to see screen.
    while (quit != 'q')
    {   cout << "Press q to quit ";
        cin >> quit;
    }
    return EXIT_SUCCESS;
}

bool calculateAccelerationArray(vector<double> t, vector<double>& p, vector<double> v, vector<double>& a)
{
//while (!calculateAccelerationArray(t, p, v, a)==false)
//    {
     //Pushing the first(zero) value to the acceleration vector and the position vector.
        for (int j=0; j<t.size()-1; j++)
            {//This is for the first instance when other method will not work.
                if (j==0)
                {
                    int next=j+1;
                    int prev=j-1;
                    Acceleration = (v.at(next)-v.at(j))/(t.at(next)- t.at(j));
                    Altitude = 0;
                    a.push_back(Acceleration);
//                    p.push_back(Altitude);
                cout << Number << "  " << Time << "  " << Altitude << "  " << Velocity << "  " << Acceleration << endl;
                break;
                }
            }
        for (int i=0; i<t.size()-1; i++)
        {//This is for the remaining data.
            int next=i+1;
            int prev=i-1;
            //Pushing correct values into the remaining spots in the position and acceleration vector objects.
            if (i>=1 && i<t.size()-1)
            {
                Altitude = ((p.at(prev))+((t.at(i)-t.at(prev))*(v.at(i)+v.at(prev)))/2);
                p.push_back(Altitude);

                Acceleration = (v.at(next)-v.at(prev))/(t.at(next)- t.at(prev));
                a.push_back(Acceleration);
                cout << Number << "  " << Time << "  " << Altitude << "  " << Velocity << "  " << Acceleration << endl;
                break;
            }
        }
         for (int i=0; i=t.size()-1; i++)
        {//This is for the remaining data.
            int next=i+1;
            int prev=i-1;
            //Pushing correct values into the remaining spots in the position and acceleration vector objects.
            if (i=t.size()-1)
            {
                Altitude = ((p.at(prev))+((t.at(i)-t.at(prev))*(v.at(i)+v.at(prev)))/2);
                p.push_back(Altitude);

                Acceleration = (v.at(i)-v.at(prev))/(t.at(i)- t.at(prev));
                a.push_back(Acceleration);
                cout << Number << "  " << Time << "  " << Altitude << "  " << Velocity << "  " << Acceleration << endl;
            break;
            }
        }
        return true;
//    }
}
void displayAcceleration(vector<double> t, vector<double> p, vector<double> v, vector<double> a)
{
    cout << "Number  Time(s)  Altitude(m)  Velocity(m/s)  Acceleration(m/s"<<char(253)<<")"<<endl;
    cout.setf(ios::fixed);
    cout.precision(3);
    for (int i=0; i<t.size()-1 ; i++)
    {
        cout<<setw(3)<<i<<setw(11)<<t[i]<<setw(11)<<p[i]<<setw(13)<<v[i]<<setw(16)<<a[i]<<endl;
    }
}


The output is:

0 65.8 0 -5.43735 0
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3) execution time : 4.050 s
Press any key to continue.


The desired output should be:

Number Time(s) Altitude(m) Velocity(m/s) Acceleration(m/s²)
0 0.000 0.000 0.000 0.000
1 0.100 0.000 0.000 0.000
2 0.200 0.000 0.000 0.083
3 0.300 0.001 0.017 0.458
4 0.400 0.006 0.092 1.874
5 0.500 0.030 0.391 4.615
6 0.600 0.101 1.015 9.024
7 0.700 0.261 2.196 14.282
8 0.800 0.565 3.871 17.966
9 0.900 1.048 5.789 19.228
10 1.000 1.723 7.717 18.504
Last edited on
Topic archived. No new replies allowed.