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
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
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
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.
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h> // perror
usingnamespace std;
constint 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
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