filling a struct from a text file

guys, I have a text "sales.txt" file which contains (id, lastname, quarter, sales) in the following format.
1
2
3
4
5
6
7
8
9
10
11
12
123 smith 1 333.20
221 doe 1 345.50
342 johnson 1 774.50
123 smith 2 333.20
221 doe 2 555.50
342 johnson 2 25.50
123 smith 3 254.20
221 doe 3 652.50
342 johnson 3 32.50
123 smith 4 354.20
221 doe 4 51.50
342 johnson 4 1000.50


I'm trying to put the file into a struct for output to another text file, but so far I'm having problem with the second method, which extract the sale by quarter and put it into array, who is inside struct, but is not working as intended, if someone could give me a hand with both methods

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

using namespace std;

struct employees { int id; string lname; double qtrSale[4]; double tsale; }; 
void getIdName(employees list[], ifstream& infile, int num);
void getData(employees list[], ifstream& infile, int num);
const int lsize = 3;

int main()
{
	ifstream infile;
	string file("file1.txt");
	
	infile.open(file);
	employees list[lsize];
	//getting first & last name
	getIdName(list, infile, lsize);
	infile.close();

	infile.open(file);
	//getting quarter sale
	getData(list, infile, lsize);
	infile.close();

	for(int i = 0; i < lsize; i++)//checking struct
	{
		cout << list[i].id << " " << list[i].lname << endl;
		for(int j = 0; j < 4; j++)
		{
			cout << list[i].qtrSale[j] << " | ";
		}
		cout << "\n";
	}
	
	system("pause");
	return 0;
}

void getIdName(employees list[], ifstream& infile, int lsize)
{
	double sale, qtr;
	for(int i = 0; i < lsize; i++)
	{
		infile >> list[i].id >> list[i].lname >> qtr >> sale;
		for(int j = 0; j < 4; j++)
		{
			list[i].qtrSale[j] = 0.0; //cero out the quarters
		}
		
	}
}
void getData(employees list[], ifstream& infile, int lsize)
{
	int id, qtr, j = 0, i = 0; 
	string name;
	double amount;
	for(int t = 0; t < 12; t++)
	{
		infile >> id >> name >> qtr >> amount;
							
		if(list[j].id == id && qtr == 1) { i = 0; list[j].qtrSale[i] = amount; }
		if(list[j].id == id && qtr == 2) { i = 1; list[j].qtrSale[i] = amount; }
		if(list[j].id == id && qtr == 3) { i = 2; list[j].qtrSale[i] = amount; }
		if(list[j].id == id && qtr == 4) { i = 3; list[j].qtrSale[i] = amount; }
		
		if( t < 3 ) { j = 0; }
		if( t > 2 && t < 6 ) { j = 1; }
		if( t > 5 && t < 9 ) { j = 2; }
		
	}
}
Last edited on
Wow, ouch! Just read everything in at once and pass an array of your structs around the program. It is so much easier then reading the file into memory at every function.
I will try that approach... but what kind of array though? because the txt file contains string and int which I need to make calculation. Also notice that the functions is taking ifsteam& as a reference not as a parameter; therefore, is not allocating more space in memory
Last edited on
An array of "employees" of course, why complicate things?
yeah, may be I should pass employees& as reference too... instead of passing it as variable, but passing array as reference I think is not allow... I could be wrong
Last edited on
In this instance that would come down to style preference. Your text file isn't that big and the program isn't too terribly complex that performance would become an issue. But if you want to be cool and practice the right way to do things then yes you would pass it by reference.
Last edited on
yeah, I just tried doing it by reference by my IDE: Visual C++, gave out an error.... "passing by reference not allow"
Last edited on
Did you pass a specific element in the array? For example:&employees[0] or something like that?
closed account (zb0S216C)
Why not use a Vector?
Vectors are a better choice. I just feel like I'm telling everyone to use vectors instead of array's lately and it seems odd to keep doing it over and over again.
this is how I tried calling it:
declaration:
void getData(employees& list, ifstream& infile, int num);
calling function
getData(list, infile, lsize);
function implementation:
void getData(employees& list, ifstream& infile, int lsize)
error:
main.cpp(20): error C2664: 'getData' : cannot convert parameter 1 from 'employees [12]' to 'employees &'
what is the correct way of passing an array by reference?
Topic archived. No new replies allowed.