Help: read/wirte to struct

I'm having issues declaring curr_prez as a struct placeholder, and im geting several errors with syntax because of it, how would i go about assigning the placeholder to the data reading it into the file

im also having several error c2228 left ' ' must have class/struct/union
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
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

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

struct President
{
	string first_name;
	string last_name;
	int begin_year;
	int end_year;
	string party_affil;
};

int main()
{
	President curr_prez;
		curr_prez.first_name;
		curr_prez.last_name;
		curr_prez.begin_year;
		curr_prez.end_year;
		curr_prez.party_affil;

		getData(curr_prez)

	return 0;
}

void getData(President curr_prez)
{
	ifstream inFile;
	inFile.open("prez_data.txt")
	for (int p = 0; p > 14; p++)
	{
		inFile >> curr_prezz.first_name;
		inFile >> curr_prezz.last_name;
		inFile >> curr_prezz.begin_year;
		inFile >> curr_prezz.end_year;
		inFile >> curr_prezz.party_affil;
		outputPresident
	}
	inFile.close();
}

void outputPresident(President curr_prez)
{

	ofstream outFile;
	outFile.open("a2.txt" ios::app);

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


Last edited on
Posting the actual error messages would be extremely helpful. Here is what the online compiler has to say:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
7:23: error: variable or field 'getData' declared void 
7:14: error: 'President' was not declared in this scope 
8:31: error: variable or field 'outputPresident' declared void 
8:22: error: 'President' was not declared in this scope 
In function 'int main()': 
22:23: warning: statement has no effect [-Wunused-value] 
23:22: warning: statement has no effect [-Wunused-value] 
24:23: warning: statement has no effect [-Wunused-value] 
25:21: warning: statement has no effect [-Wunused-value] 
26:24: warning: statement has no effect [-Wunused-value] 
28:20: error: 'getData' was not declared in this scope 
In function 'void getData(President)':
37:2: error: expected ';' before 'for' 
37:18: error: 'p' was not declared in this scope 
In function 'void outputPresident(President)': 
53:24: error: expected ')' before 'ios' 
55:22: error: 'curr_prezz' was not declared in this scope 


Don't forget that in C++ the source files are processed from the top down. If you try to use something that has not been declared before you try to use it the compiler won't be able to see the item. For example your function prototypes try to use your structure before the structure was defined or declared.

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
45	IntelliSense: type name is not allowed	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	30	11	Project2
	48	IntelliSense: expected a '{'	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	61	2	Project2
	47	IntelliSense: expected a ';'	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	47	2	Project2
	46	IntelliSense: expected a ')'	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	30	21	Project2
Error	24	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	62	1	Project2
Error	26	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	64	1	Project2
Error	29	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	65	1	Project2
Error	32	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	66	1	Project2
Error	35	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	67	1	Project2
Error	38	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	68	1	Project2
Error	41	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	69	1	Project2
Error	21	error C3646: 'outFile' : unknown override specifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	61	1	Project2
Error	20	error C3646: 'ofstream' : unknown override specifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	61	1	Project2
Error	22	error C2365: 'outputPresident' : redefinition; previous definition was 'data variable'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	61	1	Project2
Error	8	error C2365: 'getData' : redefinition; previous definition was 'data variable'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	36	1	Project2
Error	5	error C2275: 'President' : illegal use of this type as an expression	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	30	1	Project2
Error	18	error C2228: left of '.party_affil' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	53	1	Project2
Error	12	error C2228: left of '.last_name' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	50	1	Project2
Error	16	error C2228: left of '.end_year' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	52	1	Project2
Error	14	error C2228: left of '.begin_year' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	51	1	Project2
Error	4	error C2182: 'outputPresident' : illegal use of type 'void'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	13	1	Project2
Error	2	error C2182: 'getData' : illegal use of type 'void'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	12	1	Project2
Error	6	error C2146: syntax error : missing ')' before identifier 'curr_prez'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	30	1	Project2
Error	9	error C2143: syntax error : missing ';' before 'for'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	47	1	Project2
Error	25	error C2143: syntax error : missing ';' before '<<'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	64	1	Project2
Error	28	error C2143: syntax error : missing ';' before '<<'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	65	1	Project2
Error	31	error C2143: syntax error : missing ';' before '<<'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	66	1	Project2
Error	34	error C2143: syntax error : missing ';' before '<<'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	67	1	Project2
Error	37	error C2143: syntax error : missing ';' before '<<'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	68	1	Project2
Error	19	error C2143: syntax error : missing ';' before '}'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	55	1	Project2
Error	44	error C2143: syntax error : missing ';' before '}'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	70	1	Project2
Error	23	error C2143: syntax error : missing ';' before '.'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	62	1	Project2
Error	40	error C2143: syntax error : missing ';' before '.'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	69	1	Project2
Error	27	error C2086: 'int outFile' : redefinition	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	64	1	Project2
Error	30	error C2086: 'int outFile' : redefinition	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	65	1	Project2
Error	33	error C2086: 'int outFile' : redefinition	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	66	1	Project2
Error	36	error C2086: 'int outFile' : redefinition	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	67	1	Project2
Error	39	error C2086: 'int outFile' : redefinition	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	68	1	Project2
Error	42	error C2086: 'int outFile' : redefinition	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	69	1	Project2
Error	1	error C2065: 'President' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	12	1	Project2
Error	3	error C2065: 'President' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	13	1	Project2
Error	10	error C2065: 'first_name' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	49	1	Project2
Error	11	error C2065: 'curr_prezz' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	50	1	Project2
Error	13	error C2065: 'curr_prezz' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	51	1	Project2
Error	15	error C2065: 'curr_prezz' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	52	1	Project2
Error	17	error C2065: 'curr_prezz' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	53	1	Project2
Error	43	error C2059: syntax error : '}'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	70	1	Project2
Error	7	error C2059: syntax error : ')'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	30	1	Project2
Last edited on
put typedef struct President President; at the top
You also have several missing semicolons and misspellings (like curr_prezz instead of curr_prez)
what wouldthe typedef struct accomplish
It would be easier to just move the function prototypes to after the structure definition.

jlb this is what i've changed so far

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
#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;
};

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

int main()
{
	President curr_prez[13];
	ifstream in;
	ofstream out;

		getData(President curr_prez);
		inFile.close();
		outFile.close();
	return 0;
}

void getData(President curr_prez) //takes data from prez_data
{
	
	{
		curr_prez.first_name = "testfname";
		curr_prez.last_name = "testlname";
		curr_prez.begin_year = 0;
		curr_prez.end_year = 0;
		curr_prez.party_affil = "testpaffil";
	}
	ifstream inFile;
	inFile.open("prez_data.txt");
	for (int p = 0; p > 6; p++)
	{
		inFile >> first_name;
		inFile >> curr_prezz.last_name;
		inFile >> curr_prezz.begin_year;
		inFile >> curr_prezz.end_year;
		inFile >> curr_prezz.party_affil;
		outputPresident(President curr_prez);
	}
	inFile.close();
}

void outputPresident(President curr_prez) //outputs data to a2.txt
{
	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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
	24	IntelliSense: type name is not allowed	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	30	11	Project2
	30	IntelliSense: type name is not allowed	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	55	19	Project2
	27	IntelliSense: identifier "outFile" is undefined	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	32	3	Project2
	26	IntelliSense: identifier "inFile" is undefined	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	31	3	Project2
	28	IntelliSense: identifier "first_name" is undefined	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	50	13	Project2
	29	IntelliSense: identifier "curr_prezz" is undefined	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	51	13	Project2
	25	IntelliSense: expected a ')'	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	30	21	Project2
	31	IntelliSense: expected a ')'	c:\Users\Lain\Documents\Visual Studio 2013\Projects\Project2\Project2\prez.cpp	55	29	Project2
Error	23	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\project2\project2\prez.cpp	66	1	Project2
Error	1	error C2275: 'President' : illegal use of this type as an expression	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	30	1	Project2
Error	17	error C2275: 'President' : illegal use of this type as an expression	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	55	1	Project2
Error	16	error C2228: left of '.party_affil' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	54	1	Project2
Error	10	error C2228: left of '.last_name' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	51	1	Project2
Error	14	error C2228: left of '.end_year' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	53	1	Project2
Error	5	error C2228: left of '.close' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	31	1	Project2
Error	7	error C2228: left of '.close' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	32	1	Project2
Error	12	error C2228: left of '.begin_year' must have class/struct/union	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	52	1	Project2
Error	2	error C2146: syntax error : missing ')' before identifier 'curr_prez'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	30	1	Project2
Error	18	error C2146: syntax error : missing ')' before identifier 'curr_prez'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	55	1	Project2
Error	22	error C2143: syntax error : missing ';' before '<<'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	65	1	Project2
Error	20	error C2143: syntax error : missing ')' before 'std::_Iosb<int>::app'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	63	1	Project2
Error	6	error C2065: 'outFile' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	32	1	Project2
Error	4	error C2065: 'inFile' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	31	1	Project2
Error	8	error C2065: 'first_name' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	50	1	Project2
Error	9	error C2065: 'curr_prezz' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	51	1	Project2
Error	11	error C2065: 'curr_prezz' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	52	1	Project2
Error	13	error C2065: 'curr_prezz' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	53	1	Project2
Error	15	error C2065: 'curr_prezz' : undeclared identifier	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	54	1	Project2
Error	3	error C2059: syntax error : ')'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	30	1	Project2
Error	19	error C2059: syntax error : ')'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	55	1	Project2
Error	21	error C2059: syntax error : ')'	c:\users\lain\documents\visual studio 2013\projects\project2\project2\prez.cpp	63	1	Project2
Last edited on
Those error messages appear to be IntellinonSense errors and really shouldn't be relied on. You should compile your code and cut and paste the actual compile messages.

Let's start by examining main():
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	President curr_prez[13];
	ifstream in;
	ofstream out;

		getData(President curr_prez);
		inFile.close();
		outFile.close();
	return 0;
}


Why the array of curr_prez[]? When are there ever more than one current president?

Next why are you creating an ifstream and ofstream instance? You never use these variables so why define them?

Next that is not how you call a function. Perhaps you need to review your textbook for information on how to call a function. (Hint you don't need the variable type.) Also remember that the variable used must match the variable type the function is expecting, your function prototype says you will be passing a single instance, not the array you're attempting to pass.

Lastly where did you define variables with the names of inFile and outFile?

Edit: And it looks like you waited way too long before you compiled your code. You should compile early and often this will reduce the number of error messages you need to deal with and you should know about where to start looking for the problems.
Last edited on
curr_prez refers to the president that's reading from the txt file through the code then written to the other file. there are 13 total, each one will be read seperately through the 4 loop.


to output something like this for each
1
2
3
4
Name: xxxxx xxxx
Entered Office: xxxxx
Left Office xxxxx
Party Affiliation: xxxxxxx

i never defined a variable for infile and outfile
Last edited on
Okay, but why are you even defining any President in main()? You never really use the variable in main() since you pass the uninitialized variable to the function by value, you might as well define getData() without any parameters.

Also getData() doesn't seem to require an array, it seems to be doing the processing as it reads the file using only one instance of that variable.

Topic archived. No new replies allowed.