struct help

Pages: 12
Hi! I'm learning structs and I'm creating some sort of a library program and I need some help. My program looks like this:
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
#include<iostream>
using namespace std;

int main()
{
	struct BOOK
	{
		string NAME[100];
		int YEAR;
		int PAGES;
		string FIRSTNAME[20];
		string LASTNAME[20];
		string LANGUAGE[20];
		bool READ;
		int SCORE;
	}
	
	struct BOOK lotr1;
	lotr1.NAME = "Fellowship of the ring";
	lotr1.YEAR = 1954;
	lotr1.PAGES = 531;
	lotr1.FIRSTNAME = "John Ronald Reuel";
	lotr1.LASTNAME = "Tolkien";
	lotr1.LANGUAGE = "eng";
	lotr1.READ = true;
	lotr1.SCORE = 10;

	struct BOOK hobbit;
	hobbit.NAME = "Hobbit";
	hobbit.YEAR = 1937;
	hobbit.PAGES = 310;
	hobbit.FIRSTNAME = "John Ronald Reuel";
	hobbit.LASTNAME = "Tolkien";
	hobbit.LANGUAGE = "eng";
	hobbit.READ = true;
	hobbit.SCORE = 10;

system("pause");
return 0;
}
	

Now I need the output to look something like this:
"Name of the book",language,author,year,pages,score.
e.g.
"Hobbit"[eng]; John Ronald Reuel; 1937; 310 pages; score 10/10;
Thanks!
Remember, structs are exactly like classes, but if no access level is specified, the components default to public instead of private. For instance, you could have something like

cout << hobbit.NAME << ";" << hobbit.language << ";" << hobbit.FIRSTNAME << " " << hobbit.LASTNAME << ";" << hobbit.YEAR << ";" << hobbit.PAGES << " pages;" << "score " << hobbit.SCORE << "/10" << ";"

Or, if you want cout << hobbit to look like that, just overload the << operator. Note that since I'm pretty sure NAME, FIRSTNAME, and LASTNAME are arrays of strings instead of strings of certain length (I'm guessing that's what you wanted), you may have to remove the []'s.

Oh, and sorry about this, but you may want to use std::cin.ignore() instead of system("pause").
Last edited on
Well, I tried that and just to see if it would work, i tried to output "Hobbit", but that didn't work.
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
#include<iostream>
#include<string.h>
using namespace std;

int main()
{
	struct BOOK
	{
		string NAME;
		int YEAR;
		int PAGES;
		string FIRSTNAME;
		string LASTNAME;
		string LANGUAGE;
		bool READ;
		int SCORE;
	}
	
	struct BOOK lotr1;
	lotr1.NAME = "Fellowship of the ring";
	lotr1.YEAR = 1954;
	lotr1.PAGES = 531;
	lotr1.FIRSTNAME = "John Ronald Reuel";
	lotr1.LASTNAME = "Tolkien";
	lotr1.LANGUAGE = "eng";
	lotr1.READ = true;
	lotr1.SCORE = 10;

	struct BOOK hobbit;
	hobbit.NAME = "Hobbit";
	hobbit.YEAR = 1937;
	hobbit.PAGES = 310;
	hobbit.FIRSTNAME = "John Ronald Reuel";
	hobbit.LASTNAME = "Tolkien";
	hobbit.LANGUAGE = "eng";
	hobbit.READ = true;
	hobbit.SCORE = 10;
	
	cout << hobbit.NAME;

std::cin.ignore();
return 0;
}
	
Fixed up the code for you
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
#include<iostream>
#include<string.h>
using namespace std;

int main()
{
    struct BOOK
	{
		string NAME;
		int YEAR;
		int PAGES;
		string FIRSTNAME;
		string LASTNAME;
		string LANGUAGE;
		bool READ;
		int SCORE;
	};
    BOOK lotr1, hobbit;
	lotr1.NAME = "Fellowship of the ring";
	lotr1.YEAR = 1954;
	lotr1.PAGES = 531;
	lotr1.FIRSTNAME = "John Ronald Reuel";
	lotr1.LASTNAME = "Tolkien";
	lotr1.LANGUAGE = "eng";
	lotr1.READ = true;
	lotr1.SCORE = 10;
	hobbit.NAME = "Hobbit";
	hobbit.YEAR = 1937;
	hobbit.PAGES = 310;
	hobbit.FIRSTNAME = "John Ronald Reuel";
	hobbit.LASTNAME = "Tolkien";
	hobbit.LANGUAGE = "eng";
	hobbit.READ = true;
	hobbit.SCORE = 10;

	cout << hobbit.NAME;

std::cin.ignore();
return 0;
}

The first thing I changed was the "struct" in front of both the book declarations also I added a semicolon to the struct definition
Suggestion: use #include <string> instead of #include <string.h> . string.h is different and obsolete.

In fact, string.h doesn't even have a string class.
Last edited on
Okay, I added 16 books to the list, it's going to be a long program.
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<iostream>
#include<string>
using namespace std;

int main()
{
	struct BOOK
	{
		string NAME;
		int YEAR;
		int PAGES;
		string FIRSTNAME;
		string LASTNAME;
		string LANGUAGE;
		bool READ;
		int SCORE;
	};
	
	BOOK lotr1, hobbit, EG, LS, HP, Time, GOT, EotW, GH, SR, Dragon, Dune, Game, Eragon, Eldest, Brisingr;
	lotr1.NAME = "Fellowship of the ring";
	lotr1.YEAR = 1954;
	lotr1.PAGES = 531;
	lotr1.FIRSTNAME = "John Ronald Reuel";
	lotr1.LASTNAME = "Tolkien";
	lotr1.LANGUAGE = "eng";
	lotr1.READ = true;
	lotr1.SCORE = 10;
	hobbit.NAME = "Hobbit";
	hobbit.YEAR = 1937;
	hobbit.PAGES = 310;
	hobbit.FIRSTNAME = "John Ronald Reuel";
	hobbit.LASTNAME = "Tolkien";
	hobbit.LANGUAGE = "eng";
	hobbit.READ = true;
	hobbit.SCORE = 10;
	EG.NAME = "Ender's Game";
	EG.YEAR = 1985;
	EG.PAGES = 357;
	EG.FIRSTNAME = "Orson Scott";
	EG.LASTNAME = "Card";
	EG.LANGUAGE = "eng";
	EG.READ = true;
	EG.SCORE = 10;
	LS.NAME = "Lost Symbol";
	LS.YEAR = 2009;
	LS.PAGES = 639;
	LS.FIRSTNAME = "Dan";
	LS.LASTNAME = "Brown";
	LS.LANGUAGE = "eng";
	LS.READ = true;
	LS.SCORE = 8;
	HP.NAME = "Harry Potter and the Philosopher's Stone";
	HP.YEAR = 1997;
	HP.PAGES = 309;
	HP.FIRSTNAME = "Joanne";
	HP.LASTNAME = "Rowling";
	HP.LANGUAGE = "eng";
	HP.READ = true;
	HP.SCORE = 9;
	Time.NAME = "A brief history of time";
	Time.YEAR = 1988;
	Time.PAGES = 256;
	Time.FIRSTNAME = "Stephen";
	Time.LASTNAME = "Hawking";
	Time.LANGUAGE = "eng";
	Time.READ = true;
	Time.SCORE = 10;
	GOT.NAME = "Game of thrones";
	GOT.YEAR = 1996;
	GOT.PAGES = 672;
	GOT.FIRSTNAME = "George Raymond Richard";
	GOT.LASTNAME = "Martin";
	GOT.LANGUAGE = "eng";
	GOT.READ = true;
	GOT.SCORE = 8;
	EotW.NAME = "Eye of the world";
	EotW.YEAR = 1990;
	EotW.PAGES = 688;
	EotW.FIRSTNAME = "Robert";
	EotW.LASTNAME = "Jordan";
	EotW.LANGUAGE = "eng";
	EotW.READ = true;
	EotW.SCORE = 10;
	GH.NAME = "The Great Hunt";
	GH.YEAR = 1990;
	GH.PAGES = 660;
	GH.FIRSTNAME = "Robert";
	GH.LASTNAME = "Jordan";
	GH.LANGUAGE = "eng";
	GH.READ = true;
	GH.SCORE = 8;
	Dragon.NAME = "The Dragon Reborn";
	Dragon.YEAR = 1991;
	Dragon.PAGES = 628;
	Dragon.FIRSTNAME = "Robert";
	Dragon.LASTNAME = "Jordan";
	Dragon.LANGUAGE = "eng";
	Dragon.READ = true;
	Dragon.SCORE = 9;
	SR.NAME = "The Shadow Rising";
	SR.YEAR = 1992;
	SR.PAGES = 1001;
	SR.FIRSTNAME = "Robert";
	SR.LASTNAME = "Jordan";
	SR.LANGUAGE = "eng";
	SR.READ = true;
	SR.SCORE = 10;
	Dune.NAME = "Dune";
	Dune.YEAR = 1965;
	Dune.PAGES = 412;
	Dune.FIRSTNAME = "Frank";
	Dune.LASTNAME = "Herbert";
	Dune.LANGUAGE = "eng";
	Dune.READ = true;
	Dune.SCORE = 9;
	Game.NAME = "Beginning C++ through game programming, third edition";
	Game.YEAR = 2011;
	Game.PAGES = 410;
	Game.FIRSTNAME = "Michael";
	Game.LASTNAME = "Dawson";
	Game.LANGUAGE = "eng";
	Game.READ = true;
	Game.SCORE = 10;
	Eragon.NAME = "Eragon";
	Eragon.YEAR = 2002;
	Eragon.PAGES = 509;
	Eragon.FIRSTNAME = "Christopher";
	Eragon.LASTNAME = "Paolini";
	Eragon.LANGUAGE = "eng";
	Eragon.READ = true;
	Eragon.SCORE = 7;
	Eldest.NAME = "Eldest";
	Eldest.YEAR = 2005;
	Eldest.PAGES = 668;
	Eldest.FIRSTNAME = "Christopher";
	Eldest.LASTNAME = "Paolini";
	Eldest.LANGUAGE = "eng";
	Eldest.READ = true;
	Eldest.SCORE = 6;
	Brisingr.NAME = "Brisingr";
	Brisingr.YEAR = 2008;
	Brisingr.PAGES = 748;
	Brisingr.FIRSTNAME = "Christopher";
	Brisingr.LASTNAME = "Paolini";
	Brisingr.LANGUAGE = "eng";
	Brisingr.READ = true;
	Brisingr.SCORE = 7;

	FILE *Library;
	Library = fopen("BOOK", "r")
cin.ignore();
return 0;
}
	

Now for starters, I need the program to first show the first 5 books in a readable form. For example, I launch the program and the first line is:
"Fellowship of the ring" [eng]; John Ronald Reuel Tolkien; 1954; 531 pages; score: 10.
The next thing I need to do is, when the user presses "n", the 5 next books are shown, when "p" is pressed, the 5 previous books are shown and when "e" is pressed, it exits the program.
Thank you so much for your help and sorry for my english, it might not be so good.
bump
@starter

I had to change the program a bit, by adding an array for the Books, called Library. This way, you can access any of the books by a number. Hopefully, this is along the lines you were wanting..

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Book Library.cpp : main project file.

#include<iostream>
#include<string>

using namespace std;

int main()
{
	struct BOOK
	{
		string NAME;
		int YEAR;
		int PAGES;
		string FIRSTNAME;
		string LASTNAME;
		string LANGUAGE;
		bool READ;
		int SCORE;
	}Library[16];  // Change to a higher number if adding more books	
	Library[0].NAME = "Fellowship of the ring";
	Library[0].YEAR = 1954;
	Library[0].PAGES = 531;
	Library[0].FIRSTNAME = "John Ronald Reuel";
	Library[0].LASTNAME = "Tolkien";
	Library[0].LANGUAGE = "eng";
	Library[0].READ = true;
	Library[0].SCORE = 10;
	Library[1].NAME = "Hobbit";
	Library[1].YEAR = 1937;
	Library[1].PAGES = 310;
	Library[1].FIRSTNAME = "John Ronald Reuel";
	Library[1].LASTNAME = "Tolkien";
	Library[1].LANGUAGE = "eng";
	Library[1].READ = true;
	Library[1].SCORE = 10;
	Library[2].NAME = "Ender's Game";
	Library[2].YEAR = 1985;
	Library[2].PAGES = 357;
	Library[2].FIRSTNAME = "Orson Scott";
	Library[2].LASTNAME = "Card";
	Library[2].LANGUAGE = "eng";
	Library[2].READ = true;
	Library[2].SCORE = 10;
	Library[3].NAME = "Lost Symbol";
	Library[3].YEAR = 2009;
	Library[3].PAGES = 639;
	Library[3].FIRSTNAME = "Dan";
	Library[3].LASTNAME = "Brown";
	Library[3].LANGUAGE = "eng";
	Library[3].READ = true;
	Library[3].SCORE = 8;
	Library[4].NAME = "Harry Potter and the Philosopher's Stone";
	Library[4].YEAR = 1997;
	Library[4].PAGES = 309;
	Library[4].FIRSTNAME = "Joanne";
	Library[4].LASTNAME = "Rowling";
	Library[4].LANGUAGE = "eng";
	Library[4].READ = true;
	Library[4].SCORE = 9;
	Library[5].NAME = "A brief history of time";
	Library[5].YEAR = 1988;
	Library[5].PAGES = 256;
	Library[5].FIRSTNAME = "Stephen";
	Library[5].LASTNAME = "Hawking";
	Library[5].LANGUAGE = "eng";
	Library[5].READ = true;
	Library[5].SCORE = 10;
	Library[6].NAME = "Game of thrones";
	Library[6].YEAR = 1996;
	Library[6].PAGES = 672;
	Library[6].FIRSTNAME = "George Raymond Richard";
	Library[6].LASTNAME = "Martin";
	Library[6].LANGUAGE = "eng";
	Library[6].READ = true;
	Library[6].SCORE = 8;
	Library[7].NAME = "Eye of the World";
	Library[7].YEAR = 1990;
	Library[7].PAGES = 688;
	Library[7].FIRSTNAME = "Robert";
	Library[7].LASTNAME = "Jordan";
	Library[7].LANGUAGE = "eng";
	Library[7].READ = true;
	Library[7].SCORE = 10;
	Library[8].NAME = "The Great Hunt";
	Library[8].YEAR = 1990;
	Library[8].PAGES = 660;
	Library[8].FIRSTNAME = "Robert";
	Library[8].LASTNAME = "Jordan";
	Library[8].LANGUAGE = "eng";
	Library[8].READ = false;
	Library[8].SCORE = 8;
	Library[9].NAME = "The Dragon Reborn";
	Library[9].YEAR = 1991;
	Library[9].PAGES = 628;
	Library[9].FIRSTNAME = "Robert";
	Library[9].LASTNAME = "Jordan";
	Library[9].LANGUAGE = "eng";
	Library[9].READ = true;
	Library[9].SCORE = 9;
	Library[10].NAME = "The Shadow Rising";
	Library[10].YEAR = 1992;
	Library[10].PAGES = 1001;
	Library[10].FIRSTNAME = "Robert";
	Library[10].LASTNAME = "Jordan";
	Library[10].LANGUAGE = "eng";
	Library[10].READ = true;
	Library[10].SCORE = 10;
	Library[11].NAME = "Dune";
	Library[11].YEAR = 1965;
	Library[11].PAGES = 412;
	Library[11].FIRSTNAME = "Frank";
	Library[11].LASTNAME = "Herbert";
	Library[11].LANGUAGE = "eng";
	Library[11].READ = true;
	Library[11].SCORE = 9;
	Library[12].NAME = "Beginning C++ through game programming, third edition";
	Library[12].YEAR = 2011;
	Library[12].PAGES = 410;
	Library[12].FIRSTNAME = "Michael";
	Library[12].LASTNAME = "Dawson";
	Library[12].LANGUAGE = "eng";
	Library[12].READ = true;
	Library[12].SCORE = 10;
	Library[13].NAME = "Eragon";
	Library[13].YEAR = 2002;
	Library[13].PAGES = 509;
	Library[13].FIRSTNAME = "Christopher";
	Library[13].LASTNAME = "Paolini";
	Library[13].LANGUAGE = "eng";
	Library[13].READ = true;
	Library[13].SCORE = 7;
	Library[14].NAME = "Eldest";
	Library[14].YEAR = 2005;
	Library[14].PAGES = 668;
	Library[14].FIRSTNAME = "Christopher";
	Library[14].LASTNAME = "Paolini";
	Library[14].LANGUAGE = "eng";
	Library[14].READ = true;
	Library[14].SCORE = 6;
	Library[15].NAME = "Brisingr";
	Library[15].YEAR = 2008;
	Library[15].PAGES = 748;
	Library[15].FIRSTNAME = "Christopher";
	Library[15].LASTNAME = "Paolini";
	Library[15].LANGUAGE = "eng";
	Library[15].READ = true;
	Library[15].SCORE = 7;

	//FILE *Library;
	//Library = fopen("BOOK", "r");

	for(int i=0;i<=16;i++)
	{
		cout << Library[i].NAME<< " " << Library[i].YEAR << " " << Library[i].PAGES << " " << Library[i].FIRSTNAME << " " << Library[i].LASTNAME << " " << Library[i].LANGUAGE << " " << Library[i].SCORE;
		if (Library[i].READ == true)
			cout << " READ\n";
		else
			cout << " NOT READ\n";
		if((i+1)%5==0)
		{
			cout << "\n\nPress any key for next 5 titles..\n";
			cin.ignore();
		}
	}
return 0;
}
1
2
3
4
5
6
7
8
	Library[15].NAME = "Brisingr";
	Library[15].YEAR = 2008;
	Library[15].PAGES = 748;
	Library[15].FIRSTNAME = "Christopher";
	Library[15].LASTNAME = "Paolini";
	Library[15].LANGUAGE = "eng";
	Library[15].READ = true;
	Library[15].SCORE = 7;


What's wrong with a nice constructor to make this a little easier?
Sorry moorecm.. I can only give advice about things I know a bit about. I don't know how to construct a constructor, or even what one looks like. So, please, show me, starter and anyone else reading this thread, how to make one. I would be very interested in learning it. Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct BOOK
{
	string NAME; // you don't need this to be an array; removed [100]
	int YEAR;
	int PAGES;
	string FIRSTNAME; // you don't need this to be an array; removed [20]
	string LASTNAME; // you don't need this to be an array; removed [20]
	string LANGUAGE; // you don't need this to be an array; removed [20]
	bool READ;
	int SCORE;

	// constructor
	BOOK( const string & name, int year, int pages, string firstname, string lastname, string language, bool read, int score )
	: NAME(name), YEAR( year ), PAGES( pages ), FIRSTNAME( firstname ), LASTNAME( lastname ), LANGUAGE( language ), READ( read ), SCORE( score ) {}
}

// Then, to construct a single BOOK object:
BOOK book( "Brisingr", 2008, 748, "Christopher", "Paolini", "eng", true, 7 );

The trick here is that you'll want to store them in a container rather than the array.
Last edited on
Okay, but then how can they be listed using the a loop? And my apologizes to starter, as I'm not trying to hi-jack his thread. I just figured we both can learn about this, and (s)he could also benefit.

No problem, ask all you want :)
1
2
3
4
5
6
7
8
9
10
std::vector< BOOK > container;

container.push_back( BOOK( "Brisingr", 2008, 748, "Christopher", "Paolini", "eng", true, 7 ) );
//...

for( std::vector< BOOK >::iterator i = container.begin(); i != container.end(); ++i )
{
    BOOK & book = *i;
    // process book
}
@moorecm

I tried, but didn't succeed, in adding this vector. Of course, I know I'm doing it wrong, but it's the way I interpret your examples. I get 61 errors. Here are some of the errors :
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
1>.\Book Library.cpp(29) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(29) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(29) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(30) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(30) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(30) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(31) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(31) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(31) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(32) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(32) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(32) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(33) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(33) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(34) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(34) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(34) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(35) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(35) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(36) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(36) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(36) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(37) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(37) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(38) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(38) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(39) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(39) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(39) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(40) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(40) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(40) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'
1>.\Book Library.cpp(41) : error C2143: syntax error : missing ';' before '.'
1>.\Book Library.cpp(41) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Book Library.cpp(41) : error C2371: 'container' : redefinition; different basic types
1>        .\Book Library.cpp(27) : see declaration of 'container'



1>Book Library - 61 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Here's how I set up the program. This is my foray int using these vectors, so, please bear with me.

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
// Book Library.cpp : main project file.

#include<iostream>
#include<string>
#include <vector>

using namespace std;

struct BOOK
{
	string NAME; // you don't need this to be an array; removed [100]
	int YEAR;
	int PAGES;
	string FIRSTNAME;
        string LASTNAME;
	string LANGUAGE;
	bool READ;
	int SCORE;

	// constructor
	BOOK( const string & name, int year, int pages, string firstname, string lastname, string language, bool read, int score )
		: NAME(name), YEAR( year ), PAGES( pages ), FIRSTNAME( firstname ), LASTNAME( lastname ), LANGUAGE( language ), READ( read ), SCORE( score ) {};
};

// Then, to construct a single BOOK object:
vector< BOOK > container;

container.push_back( BOOK( "Eragon", 2002, 509, "Christopher", "Paolini", "eng", true, 7 ) );
container.push_back( BOOK( "Eldest", 2005, 668, "Christopher", "Paolini", "eng", true, 6 ) );
container.push_back( BOOK( "Brisingr", 2008, 748, "Christopher", "Paolini", "eng", true, 7 ) );
container.push_back( BOOK( "Fellowship Of The Ring", 1954, 531, "Christopher", "Tolkien", "eng", true, 10 ) );
container.push_back( BOOK( "The Hobbit", 1937, 310, "Christopher", "Tolkien", "eng", true, 10 ) );
container.push_back( BOOK( "Ender's Game", 1985, 357, "Orson Scott", "Card", "eng", true, 10 ) ); 
container.push_back( BOOK( "Lost Symbol", 2009, 639, "Dan", "Brown", "eng", false, 8 ) );
container.push_back( BOOK( "Harry Potter and the Philosopher's Stone", 1997, 309, "Joanne", "Rowling", "eng", false, 8 ) );
container.push_back( BOOK( "A brief history of time", 1988, 256, "Stephen", "Hawking", "eng", true, 10 ) );
container.push_back( BOOK( "Game of thrones", 1996, 672, "George Raymond Richard", "Martin", "eng", true, 8 ) );
container.push_back( BOOK( "Eye of the World", 1990, 688, "Robert", "Jordan", "eng", true, 10 ) );
container.push_back( BOOK( "The Great Hunt", 1990, 660, "Robert", "Jordan", "eng", false, 8 ) );
container.push_back( BOOK( "The Dragon Reborn", 1991, 628, "Robert", "Jordan", "eng", true, 9 ) );
container.push_back( BOOK( "The Shadow Rising", 1992, 1001, "Robert", "Jordan", "eng", true, 10 ) );
container.push_back( BOOK( "Dune", 1965, 412, "Frank", "Herbert", "eng", true, 9 ) );
container.push_back( BOOK( "Beginning C++ through game programming, 3rd Ed.", 2011, 410, "Michael", "Dawson", "eng", true, 10 ) );


int main()
{



	//FILE *Library;
	//Library = fopen("BOOK", "r");
   
    // process book
	for( std::vector< BOOK >::iterator i = container.begin(); i != container.end(); ++i )
	{
		 BOOK & book = *i;
		 cout << book.NAME<< " " <<  book.YEAR << " " <<  Book.PAGES << " " <<  Book.FIRSTNAME << " " <<  Book.LASTNAME << " " <<  Book.LANGUAGE << " " <<  Book.SCORE;
		if ( Book.READ == true)
			cout << " READ\n";
		else
			cout << " NOT READ\n";
		if((i+1)%5==0)
		{
			cout << "\n\nPress any key for next 5 titles..\n";
			cin.ignore();
		}
	}
return 0;
}
There should not be a semicolon at the end of line 22.

Also, line 63 is not going to work because i is not an integer. Let's use integers as indices for the vector to make that work:

1
2
3
4
for( size_t i = 0; i < container.size(); ++i )
{
    BOOK & book = container[i];
//... 


@moorecm

Thanks. I did get it compiled and running, but only after I moved vector < BOOK > container; and the following push_back's into int main(). Now, I feel a bit more comfortable in my understanding of using vector
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
// Book Library.cpp : main project file.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct BOOK
{
	string NAME;
	int YEAR;
	int PAGES;
	string FIRSTNAME;
	string LASTNAME;
	string LANGUAGE;
	bool READ;
	int SCORE;

	// constructor
	BOOK( const string name, int year, int pages, string firstname, string lastname, string language, bool read, int score )
		: NAME(name), YEAR( year ), PAGES( pages ), FIRSTNAME( firstname ), LASTNAME( lastname ), LANGUAGE( language ), READ( read ), SCORE( score ) {}
};

int main()
{
vector <BOOK> container;
// Then, construct BOOK objects:

container.push_back( BOOK( "Eragon", 2002, 509, "Christopher", "Paolini", "eng", true, 7 ) );
container.push_back( BOOK( "Eldest", 2005, 668, "Christopher", "Paolini", "eng", true, 6 ) );
container.push_back( BOOK( "Brisingr", 2008, 748, "Christopher", "Paolini", "eng", true, 7 ) );
container.push_back( BOOK( "Fellowship Of The Ring", 1954, 531, "John Ronald Reuel", "Tolkien", "eng", true, 10 ) );
container.push_back( BOOK( "The Hobbit", 1937, 310, "John Ronald Reuel", "Tolkien", "eng", true, 10 ) );
container.push_back( BOOK( "Ender's Game", 1985, 357, "Orson Scott", "Card", "eng", true, 10 ) ); 
container.push_back( BOOK( "The Lost Symbol", 2009, 639, "Dan", "Brown", "eng", false, 8 ) );
container.push_back( BOOK( "Harry Potter and the Philosopher's Stone", 1997, 309, "Joanne", "Rowling", "eng", false, 8 ) );
container.push_back( BOOK( "A Brief History of Time", 1988, 256, "Stephen", "Hawking", "eng", true, 10 ) );
container.push_back( BOOK( "Game of Thrones", 1996, 672, "George Raymond Richard", "Martin", "eng", true, 8 ) );
container.push_back( BOOK( "Eye of the World", 1990, 688, "Robert", "Jordan", "eng", true, 10 ) );
container.push_back( BOOK( "The Great Hunt", 1990, 660, "Robert", "Jordan", "eng", false, 8 ) );
container.push_back( BOOK( "The Dragon Reborn", 1991, 628, "Robert", "Jordan", "eng", true, 9 ) );
container.push_back( BOOK( "The Shadow Rising", 1992, 1001, "Robert", "Jordan", "eng", true, 10 ) );
container.push_back( BOOK( "Dune", 1965, 412, "Frank", "Herbert", "eng", true, 9 ) );
container.push_back( BOOK( "Beginning C++ Through Game Programming, 3rd Edition", 2011, 410, "Michael", "Dawson", "eng", true, 10 ) );

	for( size_t i = 0; i < container.size(); ++i )
	{
		 BOOK &book = container[i];
		 cout << "Book Name : " << book.NAME<< "\nPublished : " <<  book.YEAR << "\nPages : " <<  book.PAGES << "\nAuthor : " <<  book.FIRSTNAME << " " <<  book.LASTNAME << "\nLanguage : " <<  book.LANGUAGE << "\nRank : " <<  book.SCORE << "/10";
		cout << "\nOn Shelf : ";
		if ( book.READ == true)
			cout << "READ\n\n";
		else
			cout << "NOT READ\n\n";
		if((i+1)%5==0)
		{
			cout << "Press any key for next set of titles..\n";
			cin.ignore();
		}	
	}
return 0;
}
Last edited on
and the following push_back's into int main()


I completely missed that part. You can have global variables outside of main but you can't just write code out there like that. Good work!
Okay, I found out that I need to do the same program using file i/o. Such as ofstream and ifstream. Because I haven't learned vectors and containers yet, have trouble understanding it all. But using file i/o seems still a bit more complicated than using vectors. And btw thank you all for the big help :)
There's an article floating around here that was written on various tokenization techniques...let me try to find it for you.

Here it is!
http://cplusplus.com/faq/sequences/strings/split/
Last edited on
Pages: 12