I am doing a file input into an array and the I cannot change the function protos so I cannot add a number of elements variable. I am calculating thrust from this file and it works until the time is greater than the last array input. So how do I do it without a number of elements variable?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
usingnamespace std;
// this function will read the engine thrust curve data
// from a data file specified in the first parameter
// the function will add the initial data point of (0,0)
// that is not actually in the file. Maximum of 50 data points
// the function will return a 0 if successfull and a 1 if failed
int getThrustData(char filename[], double thrustCurve[][2]);
// this function will return the thrust of the engine
// for any time given an array with
// thrust curve data
// the input is in seconds and the ooutput value is in newtons
double thrust(double time, constdouble thrustCurve[][2]);
// Constraints
constint Capacity = 50;
int main()
{
// Output Identification
system("CLS");
cout << "In Class #12 by Christopher Adique -\n "
<< "Engine Thrust from File\n\n";
// Variables
char filename[Capacity];
double thrustCurve[Capacity][2];
double time = 0;
getThrustData(filename, thrustCurve);
while (time != -1){
cout << "\nEnter a time of interest (seconds) [-1 to exit]: ";
cin >> time;
if(time != -1)
cout << "The engine thrust at that is " << setprecision(3) << thrust(time, thrustCurve) << " Newtons.\n";
}
cout << "\n\nEnd Program.\n";
return 0;
}
//
//
//
int getThrustData(char filename[Capacity], double thrustCurve[Capacity][2]){
// Local Variables
ifstream fin;
ofstream fout;
string fname;
string junk;
int i = 0;
int j = 1;
cout << "Enter the name of the engine data file: ";
std:getline (std::cin,fname);
fin.open(fname);
double dataTime = 0.0f; //file input time variable
double dataThrust = 0.0f; //file input thrust variable
//check to see if see if file has been opened.
if (!fin){
cout << "The file is not opening correctly" << endl;
return 0;
}
//filling array with file data
getline (fin,junk);
while(!fin.eof()){
fin >> dataTime >> dataThrust;
thrustCurve[i][0] = dataTime;
thrustCurve[i][1] = dataThrust;
i++;
}
}
//
//
//
double thrust(double time, constdouble thrustCurve[Capacity][2]){
double Newtons;
if (time <= 0)
Newtons = 0;
elseif (time <= thrustCurve[0][0])
Newtons = ((time)/(thrustCurve[0][0]))*(thrustCurve[0][1]);
elseif (time > thrustCurve[0][0]){
int i = 0; // local variable for setting initial values to call array parameters
int j = 1; // local variable for setting initial values to call array parameters
while(time >= thrustCurve[i][0]){
if (time >= thrustCurve[i][0] && time < thrustCurve[j][0]){
Newtons = ((time - thrustCurve[i][0])/(thrustCurve[j][0] - thrustCurve[i][0])) *
(thrustCurve[j][1] - thrustCurve[i][1]) +
(thrustCurve[i][1]);
}
else
Newtons = 0;
i++;
j++;
}
}
return Newtons;
}
the data file
the first line get junked but the rests is time and thrust