Need help on reference parameter

Feb 27, 2012 at 6:57am
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:

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
#include "std_lib_facilities.h"
#include "Book.h"
#include <iostream>
#include <fstream>
#include <vector>
#include "BadCallNumException.h"
using namespace std;

void readInput(vector<Book>&Books);
void printBooks(vector<Book>&Books);
//void printInvalidCallNum(vector<Book>&Books);
//void processTrans(vector<Book>&Books);
//void searchBooks(vector<Book>&Books);

int main()
{	
	vector<Book> Books;
	readInput(Books);
	printBooks(Books);

	//printInvalidCallNum(Books);
	//processTrans(Books);
	//searchBooks(Books);
	
	
	keep_window_open();
	return 0;
}

void readInput(vector<Book> &Books)
{
	string callNum;
	string title;
	string author;
	int date;
	ifstream fin ("input2.txt");
	if (!fin)
		cout<<"Cannot open input file"<<endl;

	while(fin>>callNum>>title>>author>>date)
	{
	
			Book List(callNum, title, author, date);
				List.setCallNum(callNum);
				List.setTitle(title);
				List.setAuthor(author);
				List.setDate(date);
				Books.push_back(List);
	}
	fin.close();
} 
//Print out valid book;
void printBooks(vector<Book>&Books)
{
	
	Book List;
	string cNum;
	if(!List.setValid(cNum))
	{
		vector<Book> errorBooks;
		errorBooks.push_back(List);
		for (int i=0; i<errorBooks.size(); i++)
		{ 
			cout<<errorBooks[i]<<endl;
		}
	}
} 
void printInvalidCallNum(vector<Book>&Books)
{}
void processTrans(vector<Book>&Books)
{}
void searchBooks(vector<Book>&Books)
{}


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

QA76.1 Mindstorms Seymour_Pappert 1980
Q76.2 Karel_the_Robot Rich_Pattis 1992 //error
QA76.3 Programming Byarne_Stroustrup 2011
72QA.4 Error_Book No_Name 2012 //error
QA76.5 A_C.S._Tapestry Owen_Astrachan 1990
QA76.7 I_Was_Right_On_Time Buck_O'Neill 1955
QA768 Principles_of_Parallel_Programming Calvin_Lin 2009 //error
QAo55.6 Embedded_Software Daniel_Lewis 2011 //error
QA76.7.1 Core_Java_I Cay_Horstman 2005
QA76.7.2 Core_Java_II Cay_Horstman 2006


The call numbers will always be two letters followed by a series of digits and at least one decimal point so there are total of 4 error books
Feb 27, 2012 at 8:11am
Can't you just use ofstream to print out Books? Or whatever you wanted to print out. Or am I just stupid?
Feb 27, 2012 at 10:05am
Hi

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( unsigned int 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
           }
     }
}


hope it helps
Last edited on Feb 27, 2012 at 10:07am
Feb 27, 2012 at 3:31pm
@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
Feb 27, 2012 at 3:45pm
Here is my book.cpp and book.h file
I've changed the name of setValid to Valid in book class

Book.h
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
#pragma once
#include <string>
using namespace std;
class Book
{
public:
	Book(void);
	Book(string callNum, string title, string author, int date);
	~Book(void);
	string getCallNum();
	string getTitle();
	string getAuthor();
	int getDate();
	void setCallNum(string callNum);
	void setTitle(string title);
	void setAuthor(string author);
	void setDate( int date);
	bool Valid(string cNum);


	friend ostream& operator <<(ostream &out, Book &cBook); 
	friend bool operator ==(const Book&, const Book&);
	friend bool operator != (const Book&, const Book&) ;
private:
	string callNumber;
	string Title;
	string Author;
	int copyrightDate;
	bool isValid(string callNum);
	
};


Book.cpp
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
#include "Book.h"
#include <iostream>
using namespace std;

Book::Book(void){
}
Book::~Book(void){
}
Book::Book(string callNum, string title, string author, int date): 
callNumber(callNum), Title(title), Author(author) , copyrightDate(date)
{
}
string Book::getCallNum()
{
	return callNumber;
}
void Book::setCallNum(string callNum)
{
	callNumber=callNum;
}
string Book::getTitle()
{
	return Title;
}
void Book::setTitle(string title)
{
	Title = title;
}
string Book::getAuthor()
{
	return Author;
}
void Book::setAuthor( string author)
{
	author=Author;
}
int Book::getDate()
{
	return copyrightDate;
}
void Book::setDate( int date)
{
	copyrightDate = date;
}

bool Book::isValid(string cNum)
{

	if (!isalpha(cNum.at(0)))
		return false;
	else if (!isalpha(cNum.at(1)))
		return false;
	else if (isalpha(cNum.at(2)))
		return false;
	else if (isdigit(cNum.at(4)))
		return false;
	else
		return true;
}
bool Book::Valid(string cNum)
{
	return isValid(cNum);

}
Feb 27, 2012 at 4:48pm
remove the Valid method and make the isValid a public method, and make your method simple, e.g. some thing like


1
2
3
4
5
6
bool Book::isValid()
{
     if (!isalpha(callNumber.at(0) ) || !isalpha(callNumber.at(1) ) || isalpha(callNumber.at(2)) || isdigit(callNumber.at(4)) )
          return false;
   return true;
}



and modify your printBooks or your overloaded operator<< in a way that just call isValid for each Books to be printed or edited.

hope it helps
Last edited on Feb 27, 2012 at 4:53pm
Feb 27, 2012 at 5:10pm
Yes, that would be simpler but my instructor wanted the isValid function as a private function. :(

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
Feb 27, 2012 at 7:52pm

@ken2012 who wrote
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 ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

int main(int argc, char* argv[]){

        vector<Book> Books;
	readInput(Books);
	printBooks(Books);

	// a dummy example
      for(unsigned int 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;
}



hope it helps
Feb 27, 2012 at 8:37pm
:) ok...that answered my question. nice explaination and very helpful. Your example is way better than the example in the book lol
Thanks a lot :)

Topic archived. No new replies allowed.