C++ Reading a txt file into an array.

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

using namespace std;
ifstream iFile;

void readCode(ifstream& infile, string namelist[],
			  int idlist[], float paylist[]);




int main()
{
	string name[10];
	int id[10];
	float pay[10];

	ifstream incode;
	ofstream outcode;

	char inputFile[25];
	char outputFile[25];

	cout << "What input file should i use?";
	cin >> inputFile;
	 cin.ignore(100,'\n');
	iFile.open(inputFile);


	cout << "What output file should i use?";
	cin >> outputFile;
	cin.ignore(100,'\n');
	iFile.open(outputFile);


	readCode(incode, name, id, pay);





	return 0;

}

void readCode(ifstream& infile, string namelist[], int idlist[], float paylist[])

{
	int count;
	int countid;
	int countpay;


	  for (count = 0; count < 10; count++)
	  {
	    getline(cin, namelist[count]);
	  }

}



The txt file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Jean Rousseau
1001 15.50
Steve Woolston
1002 1423.20
Michele Rousseau
1005 52.75
Pete McBride
1007 500.32
Florence Rousseau
1010 100323.338
Don McBride
1003 12.32
Chris Carroll
1008 32.356
Greg Brown
1009 332.356
Yolanda Agredano
1004 356.00
Candi Shiltz
1006 32.362



I'm pretty much lost on how I need to read this into my arrays. What I need to do is store the names into string name[10] then I need to store the ID # into int id[10] and then store the pay into float pay[10].

So an example would be
name Jean Rousseau
ID 1001
pay 15.50

That is what should be stored into each array. Then it should continue reading in like that to the arrays.

So If anyone can point me in the right direction that would be great thanks!
you could just do the following too (Note: code not compiled & does not do exhaustive error checking)

1
2
while (cin >> namelist[count] >> idlist[count] >> paylist[count])
count++;


Remember to replace cin with infile in your function.


Last edited on
Topic archived. No new replies allowed.