Help Having trouble reading/writing data through structures

I'm trying to pass a list from a text file through a function using a structure then output the data in a more formatted manner to another text file, though im having a little trouble with a few errors also im not exactly sure how i'd get these two to run in tandem

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 President
{
	string first_name;
	string last_name;
	int begin_year;
	int end_year;
	string party_affil;
};

President curr_prez[17];

void getData(President curr_prez);
void outputPresident(President curr_prez);

//****************************************************************************************************************************************************************************
int main()
{
	
	getData(curr_prez);
	outputPresident(curr_prez);

	return 0;
}

//****************************************************************************************************************************************************************************
void getData(President curr_prez);
{
	inFile.open("prez_data.txt");
	ofstream outfile;

	for (int i = 0; i < 17; i++)
	{
		curr_prez[i].first_name = "";
		curr_prez[i].last_name = "";
		curr_prez[i].begin year = 0.0;
		curr_prez[i].party_affil = "";
	}

	for (int i = 0; i < 17; i++)
	{
		getline(inFile, curr_prez[i].first_name);
		getline(inFile, curr_prez[i].last_name);
		inFile >> curr_prez[i].begin_year;
		inFile >> curr_prez[i].end_year;
		getline(inFile, curr_prez[i].party_affil);

		inFile.ignore();


	}
}

//****************************************************************************************************************************************************************************
void outputPresident(President curr_prez)
{
	for (int i = 0; i < 17; i++)
	{
		ofstream outFile;
		outFile.open("a2.txt", ios::app);

		outFile << "Name: " << curr_prez.first_name; << " " << curr_prezz.last_name << endl;
		outFile << "Entered Office: " << curr_prez.begin_year << end;
		outFile << "Left Office: " << curr_prez.end_year << endl;
		outFile << "Party Affiliation: " << curr_prez.party_affil << endl;
		outFile << endl;
	}
}


1
2
3
4
5
6
7
8
9
	6	IntelliSense: no suitable constructor exists to convert from "President [17]" to "President"	c:\Users\Lain\Documents\Visual Studio 2013\Projects\2\2\Source.cpp	26	10	2
	7	IntelliSense: no suitable constructor exists to convert from "President [17]" to "President"	c:\Users\Lain\Documents\Visual Studio 2013\Projects\2\2\Source.cpp	27	18	2
	8	IntelliSense: expected a declaration	c:\Users\Lain\Documents\Visual Studio 2013\Projects\2\2\Source.cpp	34	1	2
	9	IntelliSense: expected a declaration	c:\Users\Lain\Documents\Visual Studio 2013\Projects\2\2\Source.cpp	58	1	2
Error	5	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)	c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp	66	1	2
Error	2	error C2664: 'void outputPresident(President)' : cannot convert argument 1 from 'President [17]' to 'President'	c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp	27	1	2
Error	1	error C2664: 'void getData(President)' : cannot convert argument 1 from 'President [17]' to 'President'	c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp	26	1	2
Error	3	error C2447: '{' : missing function header (old-style formal list?)	c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp	33	1	2
Error	4	error C2143: syntax error : missing ';' before '<<'	c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp	65	1	2
Last edited on
1
2
void getData(President curr_prez[]);
void outputPresident(President curr_prez[]);
Last edited on
peter im still getting these errors
1
2
3
4
5
6
7
8
	7	IntelliSense: no operator "<<" matches these operands
            operand types are: std::basic_ostream<char, std::char_traits<char>> << <unknown-type>	c:\Users\Lain\Documents\Visual Studio 2013\Projects\2\2\Source.cpp	70	57	2
	6	IntelliSense: expected an expression	c:\Users\Lain\Documents\Visual Studio 2013\Projects\2\2\Source.cpp	69	48	2
	4	IntelliSense: expected a declaration	c:\Users\Lain\Documents\Visual Studio 2013\Projects\2\2\Source.cpp	35	1	2
	5	IntelliSense: expected a declaration	c:\Users\Lain\Documents\Visual Studio 2013\Projects\2\2\Source.cpp	59	1	2
Error	3	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)	c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp	70	1	2
Error	1	error C2447: '{' : missing function header (old-style formal list?)	c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp	35	1	2
Error	2	error C2143: syntax error : missing ';' before '<<'	c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp	69	1	2


and also this (which i think i've solved)
Error 4 error C2228: left of '.begin_year' must have class/struct/union c:\users\lain\documents\visual studio 2013\projects\2\2\source.cpp 71 1 2
Last edited on
void getData(President curr_prez); expects a single President but you pass an array of President.
Also there should be no ; at the end.
Also inFile is not declared.
Here is an example to to read and write a struct to a file.
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
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h> // perror

using namespace std;

const int NUM_PRESIDENTS = 17; // will be 18 quite soon

struct President
{
  string first_name;
  string last_name;
  int begin_year;
  int end_year;
  string party_affil;
  President () : begin_year (0), end_year (0) {}
};

ostream& operator<<(ostream& os, President& p)
{
  os << "Name: " << p.first_name << " " << p.last_name << endl;
  os << "Entered Office: " << p.begin_year << endl;
  os << "Left Office: " << p.end_year << endl;
  os << "Party Affiliation: " << p.party_affil << endl;
  
  return os;
}

istream& operator>>(istream& is, President& p)
{
  is >> p.first_name >> p.last_name >> p.begin_year  
     >> p.end_year >> p.party_affil;

  return is;
}

void getData (President curr_prez[], int max_elem);
void outputPresident (President curr_prez[], int max_elem);

int main ()
{
  President curr_prez[NUM_PRESIDENTS];

  getData (curr_prez, NUM_PRESIDENTS);
  outputPresident (curr_prez, NUM_PRESIDENTS);

  return 0;
}

void getData (President curr_prez[], int max_elem)
{
  ifstream src ("prez_data.txt");
  if (!src)
  {
    perror("File error: ");
    return;
  }
  
  int i = 0;
  President p;

  while (src >> p && i < max_elem)
  {
    curr_prez[i] = p;
    i++;
  }
}
void outputPresident (President curr_prez[], int max_elem)
{
  ofstream dest("a2.txt");
  if (!dest)
  {
    perror ("File error: ");
    return;
  }
  for (int i = 0; i < max_elem; i++)
  {
    dest << curr_prez[i] << '\n';
  }
}


Input file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
John Doe 1800 1812 Rep
Jane Doe 1812 1816 Dem
Jill Doe 1816 1820 Independent
John Doe 1800 1812 Rep
Jane Doe 1812 1816 Dem
Jill Doe 1816 1820 Independent
John Doe 1800 1812 Rep
Jane Doe 1812 1816 Dem
Jill Doe 1816 1820 Independent
John Doe 1800 1812 Rep
Jane Doe 1812 1816 Dem
Jill Doe 1816 1820 Independent
John Doe 1800 1812 Rep
Jane Doe 1812 1816 Dem
Jill Doe 1816 1820 Independent
John Doe 1800 1812 Rep
Jane Doe 1812 1816 Dem


Output:
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
75
76
77
78
79
80
81
82
83
84
Name: John Doe
Entered Office: 1800
Left Office: 1812
Party Affiliation: Rep

Name: Jane Doe
Entered Office: 1812
Left Office: 1816
Party Affiliation: Dem

Name: Jill Doe
Entered Office: 1816
Left Office: 1820
Party Affiliation: Independent

Name: John Doe
Entered Office: 1800
Left Office: 1812
Party Affiliation: Rep

Name: Jane Doe
Entered Office: 1812
Left Office: 1816
Party Affiliation: Dem

Name: Jill Doe
Entered Office: 1816
Left Office: 1820
Party Affiliation: Independent

Name: John Doe
Entered Office: 1800
Left Office: 1812
Party Affiliation: Rep

Name: Jane Doe
Entered Office: 1812
Left Office: 1816
Party Affiliation: Dem

Name: Jill Doe
Entered Office: 1816
Left Office: 1820
Party Affiliation: Independent

Name: John Doe
Entered Office: 1800
Left Office: 1812
Party Affiliation: Rep

Name: Jane Doe
Entered Office: 1812
Left Office: 1816
Party Affiliation: Dem

Name: Jill Doe
Entered Office: 1816
Left Office: 1820
Party Affiliation: Independent

Name: John Doe
Entered Office: 1800
Left Office: 1812
Party Affiliation: Rep

Name: Jane Doe
Entered Office: 1812
Left Office: 1816
Party Affiliation: Dem

Name: Jill Doe
Entered Office: 1816
Left Office: 1820
Party Affiliation: Independent

Name: John Doe
Entered Office: 1800
Left Office: 1812
Party Affiliation: Rep

Name: Jane Doe
Entered Office: 1812
Left Office: 1816
Party Affiliation: Dem

Topic archived. No new replies allowed.