Small problem for vector . help !


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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>

using namespace std;

class Books{
private:
	string authorName;
	string title;
	int date , year , month;
public:
	Books();

	void setAuthorName( string );
	void setTitle( string );
	void setDate( int );
	void setMonth( int );
	void setYear ( int );

	int getAuthorName();
	int getTitle();
	int getDate();
	int getMonth();
	int getYear();

};

Books :: Books(){
	authorName = ""; 
	title = ""; 
	date = 0 ; 
	year = 0 ; 
	month = 0 ; 
}


void Books::setAuthorName( string Name ){
	authorName = Name;
}

void Books::setTitle( string theTitle ){
	title = theTitle;
}

void Books :: setDate( int thedate ){
	date = thedate;
}

void Books :: setMonth( int themonth ){
	month = themonth;
}

void Books :: setYear( int theyear ){
	year = theyear;
}


use a class to hold the data for each book. Store the entire database of books in a vector in which the vector element is a book object

how i store my books detail into the date and so on?
because while i in main declare like
vector<Books> theBooks;
i don't have theBooks.setAuthorName something like that
how i fill in the element?
You can use vectors similar to how you use arrays. You have two options to filling out book information.

1
2
3
4
//Style 1: 
Books newBook;
newBook.setAuthorName("Some_Author");
theBooks.push_back(newBook);


1
2
//Style 2: (used if you've already added the book to the vector)
theBooks[0].setAuthorName("Some_Author");


In short, you can access vector elements using the [] operator exactly the same way you access arrays. You can call functions that are part of that object also.
do you mean 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
42
43
44
45
46
47
bool match = 1;
					string tempName , tempTitle;
					int tempDate = 0 , tempMonth = 0 , tempYear = 0;
					Books FirstBook;
					vector<Books> theBooks;
					
					cout << "Enter Author Name  : ";
					getline( cin , tempName );
					FirstBook.setAuthorName( tempName );

					cout << "Enter Book Title   : ";
					getline( cin , tempTitle );
					FirstBook.setTitle( tempTitle );

					cout << "\n\tEnter Publication Date\n---------------------------------" << endl;
					//Skip validation for year since year cannot be validate
					cout << "Year  : ";
					cin  >> tempYear;
					//Do validation & input checking on month
					do{
						cout << "Month : ";
						cin  >> tempMonth;
					}while( tempMonth < 1 || tempMonth > 12 );
					//Do validation for every month and their date
					do{
						cout << "Date   : ";
						cin  >> tempDate;
						if( tempMonth == 2 ){
							if( tempDate < 1 || tempDate > 29 )
								match = false;
						}
						else if ( tempMonth == 1 || tempMonth == 3 || tempMonth == 5 || tempMonth == 7 || tempMonth == 8 
								|| tempMonth == 8 || tempMonth == 10 || tempMonth == 12 )
							if( tempDate < 1 || tempDate > 31 ){
								match = false;
						}
						else if ( tempMonth == 2 || tempMonth == 4 || tempMonth == 6 || tempMonth == 9 || tempMonth == 11 ){
							if( tempDate < 1 || tempDate > 30 )
								match = false;
						}
						cout << endl;
					}while( match );

					theBooks.push_back(FirstBook);

					cout << "Add Record Successfull " << endl;
					getch();


but if . For this situation i only to able have 1 book rite .
in order i have second book and third book. how should i declare them?
That's correct.

If you want to input an unlimited amount of books, you need to structure your program to loop, and create the book object inside the loop (so you create a new one every time it loops.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector<Books> bookVector;
while(true)
{
    Books tempBook;
    //fill out book information here

    bookVector.push_back(tempBook);

    char choice = ' ';
    cout << "Would you like to add another book? (Y/N) ";
    cin >> choice;

    if(choice == 'n' || choice == 'N')
        break; //exit while loop, finished adding books.
}
Last edited on
Here i change after you teach
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>

using namespace std;

class Books{
private:
	string authorName;
	string title;
	int date , year , month;
public:
	Books();

	void setAuthorName( string );
	void setTitle( string );
	void setDate( int );
	void setMonth( int );
	void setYear ( int );

	int getAuthorName();
	int getTitle();
	int getDate();
	int getMonth();
	int getYear();

};

Books :: Books(){
	authorName = ""; 
	title = ""; 
	date = 0 ; 
	year = 0 ; 
	month = 0 ; 
}


void Books::setAuthorName( string Name ){
	authorName = Name;
}

void Books::setTitle( string theTitle ){
	title = theTitle;
}

void Books :: setDate( int thedate ){
	date = thedate;
}

void Books :: setMonth( int themonth ){
	month = themonth;
}

void Books :: setYear( int theyear ){
	year = theyear;
}


void MainMenu(){
	
	int choice = 0;

	do{
		system( "cls" );
		cout << "----------------------------\n\tDatabase of Books\n---------------------------- " << endl
			<< "1.Add a book's information" << endl
			<< "2.Print an alphabetical list of the books sorted by title" << endl
			<< "3.Quit" << endl << endl
			<< "Enter Choice : ";

		cin  >> choice;
	
		switch( choice ){
		case 1:{
					vector<Books> BookVector;
					bool match = 1;
					string tempName , tempTitle;
					int tempDate = 0 , tempMonth = 0 , tempYear = 0;
					char choice = ' ';

					while( true ){
						system ( "cls" );
						Books tempBook;
					
						cout << "Enter Author Name  : ";
						getline( cin , tempName );
						tempBook.setAuthorName( tempName );

						cout << "Enter Book Title   : ";
						getline( cin , tempTitle );
						tempBook.setTitle( tempTitle );

						cout << "\n\tEnter Publication Date\n---------------------------------" << endl;
						//Skip validation for year since year cannot be validate
						cout << "Year  : ";
						cin  >> tempYear;
						//Do validation & input checking on month
						do{
							cout << "Month : ";
							cin  >> tempMonth;
						}while( tempMonth < 1 || tempMonth > 12 );
						//Do validation for every month and their date
						do{
							cout << "Date   : ";
							cin  >> tempDate;
							if( tempMonth == 2 ){
								if( tempDate < 1 || tempDate > 29 )
									match = false;
							}
							else if ( tempMonth == 1 || tempMonth == 3 || tempMonth == 5 || tempMonth == 7 || tempMonth == 8 
									|| tempMonth == 8 || tempMonth == 10 || tempMonth == 12 )
								if( tempDate < 1 || tempDate > 31 ){
									match = false;
							}
							else if ( tempMonth == 2 || tempMonth == 4 || tempMonth == 6 || tempMonth == 9 || tempMonth == 11 ){
								if( tempDate < 1 || tempDate > 30 )
									match = false;
							}
							cout << endl;
						}while( match );
						tempBook.setDate( tempDate );
						tempBook.setMonth( tempMonth );
						tempBook.setYear( tempYear );

						BookVector.push_back(tempBook);

						cout << "Add Record Successfull " << endl << endl;
						
						cout << "Would you like to add another book? (Y/N) ";
						cin  >> choice;

						if( choice == 'n' || choice == 'N')
							break; //exit while loop, finished adding books.
					}
					getch();
					break;
			   }
		case 2:
			break;
		case 3:
			getch();
			break;
		default:
			cout << "Invalid input ! Please re-enter ! " << endl;
			break;
		}
	}while( choice != 1 || choice != 2 || choice !=3 );
}


and am i fullfill for this requirement?
must use a class to hold the data for each book. Store the entire database of books in a vector in which the vector element is a book object.
If not mistaken it's correct right?
That certainly meets the requirements. Great job!
Topic archived. No new replies allowed.