How to read data from file

I need to write a program that reads the thrust data for a model rocket engine from a file and prints the data to the screen.The first line is information about the motor and can be skipped. The remainder of the file is paired of floating point numbers. The first number is time in seconds and the second number is the thrust in newtons.

Example output:
Enter the name of the file: A8.txt

The thrust data for that engine is:
Time Thrust
0.041 0.512
0.084 2.115
0.127 4.358
0.166 6.794
....
End program.

so far this is what i have but I'm confused on how to output the data??

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


int main( )
{
	string filename;
	double time;
	double thrust;
	ifstream infile;
	
	
	// Output Identification
	system("CLS");
	cout << " - "
		 << "Print Engine Data\n\n";

	cout << "Enter the name of the file:";
	cin >> filename;

	infile.open(filename);


	  if (infile.fail())
	  {
		cerr << "unable to open file " << filename << " for reading" <<       endl;
		return(1);
	  }


	
	return 0;
}
After you have opened the file for input you would need to read the data line by line and put the data into their respective variables.

Here's some code that reads an entire line from the file and places it into a string:

1
2
3
4
5
while (!infile.eof()) {
    string line;
    getline(infile, line); // get the next line from the file
    cout << line << endl; // outputs the entire line of the file
}
OK I corrected my program and It outputs the data but how do i skip the first line of information??

Print Engine Data

Enter the name of the file:I:\gmayer\cis-123\RocketData\EngineData\B6.txt (file name)

B6 18 70 0-2-4-6 .0056 .01822 E (line of information)??????
0.023 0.688
0.057 2.457
0.089 4.816
0.116 7.274
0.148 9.929
0.171 12.140
0.191 11.695
0.200 10.719
0.209 9.240
0.230 7.667
0.255 6.488
0.305 5.505
0.375 4.816
0.477 4.620
0.580 4.620
0.671 4.521
0.746 4.226
0.786 4.325
0.802 3.145
0.825 1.572
0.860 0.000
Press any key to continue . . .
Last edited on
You could read the first line of data into a 'dummy' variable that you aren't going to use just before you enter the loop that reads all the lines from the file.

Like this:

1
2
3
4
5
6
string dummy;
getline(infile, dummy);

while (!infile.eof()) {
...
}
Yes that's what i did. Thank you for the help!!


I started working on another program and while going through the instructions I was confused.
Hope you are able to give me some ideas on how to design the program..Thanks!!


So we got a curve, the curve represents the thrust of a engine. I have to divide the curve into 10 points(5 segments). So the program ask for the segments, then ask for a time for which thrust will be calculated. The thrust is calculated by
((time_entered - firstpoint) /(second point - first point)) *(thrust of second point - thrust of first point) + thrust of first point).

Example output:

Enter thrust data (0 for thrust to end list):
0.19 14.5
0.24 6.0 (user enters the point and thrust at that point)
0.40 4.4
1.80 4.2

Enter a time: 1.0 (user enters time for which thrust is calculated)
The thrust at time 1.0 is 4.3 newtons

The Instructions say that the data should be read into a pair of arrays or one two dimensional array. This is where I'm confused, how to read the data into array and call them to calculate the thrust at a given time??

Last edited on
Sorry, but I can't quite understand what numbers you are using to get 4.3 Newtons for 1 second. Don't get me wrong, I trust your answer far more than my answer. I must have been using the wrong numbers because I managed to get -123.2 Newtons which isn't quite possible in this situation.

Tell me if I'm using the wrong numbers, but here's exactly what I did:
((1 - 0.19) / (0.24 - 0.19)) * (6 - 14.5) + 14.5

I'm clearly having a bad day because I'm also not sure whether the thrust data is meant to be entered into the program by the user or that it is meant to be read from file.

I need just a little bit more context to be more helpful.
Last edited on
I apologize for not being clear enough. Yes you are using the wrong time segment. It should be the segment of (0.40-1.80)

here is an example:

We make the time 0.19 to 0.24 a segment and the thrust at 0.19 is 14.5 newtons and the thrust at 0.24 is 6.0 newtons. then if I call the function with a time of 0.21 it will return the value of:
((0.21 - 0.19)/(0.24 - 0.19)) * (6 - 14.5) + 14.5 = 11.1 newtons

And yes, the thrust data is entered by the user for example:

Enter thrust data:
(time/points) (thrust at point)
0.19 14.5
0.24 6.0
0.40 4.4

Once again Thank you for your help!!!
Last edited on
Thanks, that's much better now. First of all, you obviously need to actually get input from the user for the data you require for the calculations. You could either get input with both pieces of data entered at once or you could ask for them separately.

I'm not going to spoil what I've done just yet, I'm going to give you the opportunity to try and figure this one out yourself; all I'm going to say is that you need to use cin >> to take input. And then once you've got the input you just simply need to put the data into the formula and calculate the thrust.
Last edited on
I made a program that calculates thrust for any given time on the curve. The program ask the user for a time then a function calculates the thrust using if else statements to check the segments. There are still major things to add like the array for the thrust data and a for loop to allow the user enter up to 50 curve points.

Creating the array to store the data and how to call the data to calculate the thrust at given time is where im confused??

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <cstdlib>
using namespace std;


double C6thrust(double time);

// Main Program
int main( )
{
	
	char answer;          // answer enter by user if program should runs again
	double thtime,        // time of interest in seconds
		   enginethrust;  // engine thrust during time of interest
	

	// Output Identification
	system("CLS");
	cout << " - "
		 << "Engine thrust\n\n";


	do
	{
		// ask user for the time of interest for thrust 
		cout << "Enter a time (seconds): ";
		cin >> thtime;


		// function call for engine thrust
		enginethrust = C6thrust(thtime);

		// outputs the engine thrust for time of interest 
		cout << "The thrust at time " << thtime << " is " << enginethrust << " newtons.\n";

		// ask user if program should run again
		cout << "Would you like to run the program again? ";
		cin >> answer;

	}while(answer == 'Y' || answer == 'y');

	

	return 0;
}

// function definition 
double C6thrust(double time)
{
	double eng;  // engine thrust

	// calculates engine thrust in segment #1
	if(time > 0 && time < 0.1)
	{

		// calculates slope of line
		double slp = (6 - 0)/(0.1 - 0.0);

		// calculates the y-intercept
		double yint = 6 - (slp * 0.1);

		// calculates engine thrust
		eng = slp * time + yint;
	}

	// calculates engine thrust in segment #2
	else if(time >= 0.1 && time < 0.19)
	{

		// calculates slope of line
		double slp = (14.5 - 6)/(0.19 - 0.1);

		// calculates the y-intercept
		double yint = 14.0 - (slp * 0.19);

		// calculates engine thrust
		eng = slp * time + yint;
	}

	// calculates engine thrust in segment #3
	else if(time >= 0.19 && time <= 0.24)
	{

		// calculates slope of line
		double slp = (6 - 14.5)/(0.24 - 0.19);

		// calculates the y-intercept
		double yint = 6 - (slp * 0.24);

		// calculates engine thrust
		eng = slp * time + yint;
	}

	// calculates engine thrust in segment #4
	else if(time >= 0.24 && time <= 0.4)
	{

		// calculates slope of line
		double slp = (4.5 - 6)/(0.4 - 0.25);

		// calculates the y-intercept
		double yint = 4.5 - (slp * 0.4);

		// calculates engine thrust
		eng = slp * time + yint;
	}

	// calculates engine thrust in segment #5
	else if(time >= 0.4 && time <= 1.8)
	{

		// calculates slope of line
		double slp = (4.5 - 4.3)/(1.8 - 0.4);

		// calculates the y-intercept
		double yint = 4.9 - (slp * 1.8);

		// calculates engine thrust
		eng = slp * time + yint;
	}

	// calculates engine thrust in segment #6
	else if(time >= 1.8 && time <= 1.85)
	{

		// calculates slope of line
		double slp = (0 - 4.5)/(1.85 - 1.8);

		// calculates the y-intercept
		double yint = 0 - (slp * 1.85);

		// calculates engine thrust
		eng = slp * time + yint;
	}

	// outputs 0 if time = 0
	else if (time <= 0)
	{
		eng = 0;
	}

	// outputs 0 if time = 1.9
	else if(time >= 1.9)
	{
		eng = 0;
	}

	return eng;

}
Last edited on
Well you could either input the thrust data from the file, as you did from your previous program, or you could ask the user to input the data. I think inputting from file would be easier considering you already wrote the code to do it in the previous program. All you need to do with the data is split it up into the necessary numbers and then put those values into an array.

The below example splits off a line of the file and then places the numbers into 2 arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const int ITEMS = 6;
int time[ITEMS];
int thrust[ITEMS];

string line = "0.19 14.5"; // an example line of data from the file

// search for the space in the line that splits the numbers
int sp = line.find(" ");
if (sp != string::npos) {
    // a space exists
    double ti; // the time
    double th; // the thrust
    ti = stod(line.substr(0, sp)); // splits the first number and converts it into a double
    th = stod(line.substr(sp + 1, line.length() - sp)); // does the same with the second

    time[0] = ti;
    thrust[0] = th;
}

You just have to loop through all of the lines continuing to split the numbers up and placing them into the respective arrays.
Last edited on
So I added a for loop that ask user for thrust data (time, thrust at time) and stores it in a multidimensional array. But now im confused on how to pass the array to the function to calculate the thrust for given time. What do I put in for the function parameters in the function call, definition and prototype??? I added question marks for the areas where im confused.

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


#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;


double thr(???????????????);

// Main Program
int main( )
{
	double timethrust[10][10];
	char answer;                 // answer enter by user if program should runs again
	double enginethrust,         // engine thrust during time of interest
	       entered_time_by_user;           // time for which thrust is calculated

	// Output Identification
	system("CLS");
	cout << " - "
		 << "Compute Engine Thrust Using Arrays\n\n";


	do
	{
		cout << "Enter thrust curve data (0 for thrust to end list):\n";
		for(int i=0; i<10; i++)
		{
			cin >> timethrust[i][0] >> setw(5) >> timethrust[0][i];
		}

		cout << "Enter a time: ";
		cin >> entered_time_by_user;

		// function call for engine thrust
		enginethrust = thr(??????????????);

		// outputs the engine thrust for time of interest 
		cout << "The thrust at time " << entered_time_by_user << " is " << enginethrust << " newtons.\n";

		// ask user if program should run again
		cout << "Would you like to run the program again? ";
		cin >> answer;

	}while(answer == 'Y' || answer == 'y');
	
	return 0;
}

// function definition 
double thrust(????????????????)
{
}
Last edited on
If you are wanting to pass the entire array at once to the function, then the function would look something like this:
double thrust(double data[10][10])

And your prototype would look very similar:
double thrust(double[10][10]);

Then when you want to pass the array to the function you just type the name of the array as you would for a normal variable, like so:
enginethrust = thrust(timethrust);
Last edited on
Topic archived. No new replies allowed.