Getting unknown number of integers from .txt file

Hello, I need help with getting a set of values from a .txt file into a C++ code, where I am supposed to categorize them into x axis values and y axis values, then use the trapezoidal rule to get the area under that set of values. Getting the values into the C++ code was completed, however, I do not know how many values will be in the .txt file. Is there a way to limit it to the number of values in the .txt file? The text file and the code I made are shown below. We have been instructed to use structures, so that is what I used here.

The text file is named "curve_s1.txt", with the following values:
1.0 4.5
3.0 10.0
5.0 8.5
6.0 6.0
7.0 7.3
8.0 8.2
9.0 10.5
11.0 13.5


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
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <string>
#include <iomanip>

using namespace std;

typedef struct
{
	double xAxis[100];
	double yAxis[100];
} AxisValues;

int main()
{
	AxisValues aValues;
	int n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0, n6 = 0, n7 = 0, n8 = 0;
	string fileName;
	ifstream infile;
	
	cout << "Numerical Integrator - calculating area under a curve.\n\n";
	cout << "Enter the name of the file which contains data points (x and y coordinates) that form the curve:\n";
	cin >> fileName; //enter curve_s1.txt here

	infile.open(fileName);
	if (infile.fail()) { cout << "Error\n\n"; }

	for (n1 = 0; n1 < 20; n1++)
	{
		infile >> aValues.xAxis[n2];
		n2++;
			for (n3 = 0; n3 < 20; n3++);
			{
				infile >> aValues.yAxis[n4];
				n4++;
			}
	}

	cout << "x	y\n";
	for (n5 = 0; n5 < 20; n5++)
	{
		cout << aValues.xAxis[n6] << "	";
		n6++;
		for (n7 = 0; n7 < 20; n7++);
		{
			cout << aValues.yAxis[n8] << "\n";
			n8++;
		}
	}



	system("pause");
	return 0;
}
Have you read about std::vector? It is dynamic. It can grow as needed.


You have exactly one struct object in your program.
You read 20 values to its first member array (the xAxis).
You read 400 values to its second member array (the yAxis).


How about a struct that contains only one data point?
Create a list (std::vector) of points.
Topic archived. No new replies allowed.