Hi guys,
I'm working on my second homework. It basically read in a text file of books list and determine which book is valid and which in invalid. Once I determined these lists, I need to print out the valid books, invalid call numbers, and update the book using another text file.
Here is my code:
for the printBooks function, how do I print out using reference paramater? I have trouble understanding the concept :( Any help would be appreciated.
btw...I also have book.cpp and book.h
and here is an example of a string inside the text file
the function printBooks is expected to print out the books using the reference, first the param which the function takes should be a kind of const.
Second why should the prinBooks function call setValid, and why should setValid return a boolean ? and what is the value of cNum which you are provding the setValid function ?
you are just doing all others than printing the data out :-)
If you arr tring to print out valid or invalid Books, so you should have some method in Books which tell you whether a Book is valid or not, e.g getValid , and not setValid( setValid is to set whether a Book is valid or not )
a begin point for you may be as following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void printBooks(const vector<Book>& Books)
{
for( unsignedint i = 0 ; i < Books.size() ; i++ ){
if(!Books[i].getValid() )
{
//do whatever you wish for invalid books
}
else{
// do whatever you wish for valid books
}
}
}
@Vortex47: Yes, I will need to use ofstream to print the list into a text file but first i need to determine which book is valid and error books. I use cout so I dont have to open the text file every time I run the program
@therockon7thow: I have setValid because in my book.cpp file, I have a private function called getValid. In order to access to that function i have to create a void setValid function in public so I can use it in main.cpp
btw...thanks for the help I will try to work on it
and one more question how do I use overloaded operator? do I use it inside main.cpp? or in the book.cpp sorry i have trouble understanding these
Once you implement the overloaded operator( <<, == , != ) some where in a source file, than you can use them everywhere with object of your class , for example
in your main you may have some thing like following ;
int main(int argc, char* argv[]){
vector<Book> Books;
readInput(Books);
printBooks(Books);
// a dummy example
for(unsignedint i = 0 ; i < Books.size() - 1; i++)
{
// use == operator
if( Books[i] == Books[ i + 1 ] ){
//do some dummy things :-)
}
if(Books[i] != Books[ i + 1 ]){
// do some other dummy things :-)
// you may print the current object out using your overloaded operator <<
cout<< Books[i] << endl;
}
}
return 0;
}