Reading a file into multiple arrays

For my assignment i have to build a program that prompts the user for an input and output .txt file, reads data into 3 parallel arrays (name, id#, balance), and outputs data relative to whom the user searches for.

I'm having trouble reading in the data from the InFile.txt. I copied and paste an old chunk of code that worked on sothing similar to this but this was the result:
"Jean 0 1.2641e+029
0 3.76453e-039
1875687368 1.25968e+029
2686504 1.26428e+029
1875607209 3.76454e-039
0 3.76455e-039
6 1.2641e+029
1875687368 8.40779e-045
1875655176 0
1875685200 8.40779e-045"

This is what is in InFile.txt:
Jean Rousseau
1001 15.50
Steve Woolston
1002 1423.20
Michele Rousseau
1005 52.75
Pete McBride
1007 500.32
Florence Rousseau
1010 1323.338
Lisa Covi
1009 332.356
Don McBride
1003 12.32
Chris Carroll
1008 32.356
Yolanda Agredano
1004 356.00
Sally Sleeper
1006 32.362

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 void ReadArrayFromFile(int index, string inputFile, string nameArray[10],
		int IDArray[10], float balanceArray[10])
{
	ifstream infile;
	infile.open (inputFile.c_str());

	if(infile.fail())
	{
		cout << "Error opening file\n";
	}

	for(index=0;index<10;index++)
	{
		infile >> nameArray[index] >> IDArray[index]
				>> balanceArray[index];
	}

	infile.close();
}
You might try reading the file using getline().
You can use getline to read the name (which is composed of name and surname), then a couple of >> to read the values in the following line.

Here is some 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
36
37
38
39
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char const *argv[])
{
	string nameArray[10];
	int IDArray[10];
	float balanceArray[10];
	string inputFile = "InFile.txt";
	ifstream infile;
	infile.open(inputFile.c_str());

	string tmp;

	if(infile.fail())
	{
		cout << "Error opening file\n";
	}

	for(int index = 0; index < 10; index++)
	{
		getline(infile, nameArray[index]);
		infile >> IDArray[index] >> balanceArray[index];
		getline(infile, tmp);    // to skip the endline character
	}

	// print the array to show that the file was read correctly
	for(int i = 0; i < 10; ++i)
	{
		cout << nameArray[i] << "\n";
		cout << IDArray[i] << "\n";
		cout << balanceArray[i] << "\n\n";
	}

	infile.close();
	return 0;
}


Let me know if you have any doubt.
It's more common practice to use istream::ignore() to skip the rest of a line (cf. calling getline() again.)
http://www.cplusplus.com/reference/istream/istream/ignore/

22
23
24
25
26
27
	for(int index = 0; index < 10; index++)
	{
		getline(infile, nameArray[index]);
		infile >> IDArray[index] >> balanceArray[index];
		infile.ignore(1024, '\n');    // to skip the rest of the line
	}


For this program (an exercise) I think that assuming a maximum line length of 1024 is fair enough. But in more careful code should include <limits> and use std::numeric_limits<streamsize>::max()
http://www.cplusplus.com/reference/limits/numeric_limits/
http://www.cplusplus.com/reference/ios/streamsize/?kw=streamsize )

Also, ideally you would not assume you have 10 entries in your file; all you know is that the maximum number you can store is 10.

Andy
Last edited on
Topic archived. No new replies allowed.