How to properly read a file into multiple arrays

How do I properly read from a file that has both strings and numeric literals into an array for each? For example, I have a text file that lists 10 peoples last name, first name, a number of days they rented an item and the balance due. My class is asking me to create a program that reads in that file, and puts each of the 10 peoples proper info into a seperate array. The arrays would be last name, first name, rental days, and balance due.\

In the file there's also a header part which I wrote a loop to disregard them so they won't be stored in the arrays.

In my code, it allows me to store the first and last name into the arrays but for some reason I can't get the numbers to store in their arrays. This is the code I have so far (as for all the preprocessor directives, my professor wants me to include all of them to be safe)


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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <string.h>
using namespace std;

/**************************** TYPEDEF SECTION ****************************/

const int 		LIST_SIZE = 10,  // Declares how many names there are in the array
			NAME_SIZE = 30; // Declares how many characters a name can have
				
typedef char 	LIST_L[NAME_SIZE]; 
typedef char 	LIST_F[NAME_SIZE]; 
typedef int 	LIST_R[LIST_SIZE]; 
typedef double 	LIST_B[LIST_SIZE]; 

int main(int argc, char * argv[])
{
 	int		choice, // This is for choosing whether to display the results onto console or to write out to a file
 			idx = 0; // Counter variable for the loops
 				
 	LIST_R 		rentalDays;
 	
 	char		dummyV[15], // Dummy variable to ignore the header
	 		drive[2],
			disk_file[15],
			file[9];
	
	LIST_L		lastName;
	LIST_F		firstName;

	LIST_B 		balanceDue;				
					
	ifstream 	fin;
	ofstream 	outfile;
	
	fin.open("C:\\Users\\arlarv\\Desktop\\Array Assignment\\invoice1_test1.txt"); //This opens the text file
	
/**************************** INPUT SECTION ****************************/                                            	
	
	//This loop disregards the header of the input file
	while ( idx < 4)
	{	
		fin >> dummyV;
		idx++;
	}
		
	//This loop will read the text file and store the elements into their proper array	
	for (idx = 0; idx <= LIST_SIZE ; idx++)
	{
		fin >> lastName; //This one works
		fin >> firstName; // As does this one
		fin >> rentalDays; // But these bottom two don't work?
		fin >> balanceDue;
	}	
	
	}


And if it helps, this is what the text file looks like:

LastName FirstName DaysofRental BalanceDue
Smith Joe 15 100.50
Doe John 10 95.20
Anderson Paul 30 20.00
O'Donell Miriam 10 24.30
Foster Sam 30 15.00
Zom Pete 10 20.00
Mock Chilly 100 30
smitty Chris 200 200
xu Conor 1 200
anilo steve 0 0
Last edited on
What error are you getting when you try to read the balancedue..Is space used as a delimiter in the file.

FYI, always check to see if the file is opened first before you start extraction and remember to close all opened files at the end

hence is a sample modified code below for the FYI

try {


fin.open("C:\\Users\\arlarv\\Desktop\\Array Assignment\\invoice1_test1.txt"); //This opens the text file

//This loop disregards the header of the input file
while ( idx < 4)
{
fin >> dummyV;
idx++;
}

//This loop will read the text file and store the elements into their proper array
for (idx = 0; idx <= LIST_SIZE ; idx++)
{
fin >> lastName; //This one works
fin >> firstName; // As does this one
fin >> rentalDays; // But these bottom two don't work?
fin >> balanceDue;
}

fin.close();
}
catch (std::ifstream::failure e) {
std::cerr << "Exception opening file: " << std::strerror(errno) << "\n";
}
The error I get is:
[Error] no match for 'operator>>' (operand types are 'std::ifstream {aka std::basic_ifstream<char>}' and 'LIST_R {aka int [10]}')
Topic archived. No new replies allowed.