Classes: Reading in data? Using constructors?

F

fakeprogress

For a homework assignment in my Data Structures/C++ class, I have to
create the interface and implementation for a class called Book, create
objects within the class, and process transactions that manipulate (and
report on) members of the class.

Interface consists of:
- 5 private variables
char author[20];
char title[25];
char code[4];
int ncopies; // number of copies owned
int onloan; // number of copies currently on loan

- 5 public methods + 1 other method
char *getAuthor; // returns (a pointer to) the author's name
char *getTitle; // returns (a pointer to) the title
char *getCode; // returns (a pointer to) the code of the book
int getNcopies; // returns the number of copies owned by the
library
int getOnLoan; // returns the number of copies currently on
loan

void Borrow( int ); // fixed number of copies are borrowed

- 2 required constructors + 1 optional constructor
Book( auth, tit, cd, ncop ) // creates an object of type Book
with the info specified & assumes number of copies on loan is 0
Book( auth, tit, cd, ncop, nonloan ) // creates an object of
type Book with the info specified

Book( ) // allows you to declare objects of type Book without
initializing any values


* There is an initial file of information (called initbooks) that
contains the information for the initial library. This information is
stored on a file (and is not to be keyed in individually). Write a
function readLibrary that will read the input file and create a book
for each line of information read. As each data line is read, you
should allocate a new record (possibly from within an available array)
and initialize the data.

Each data line contains an author, title, code, and number of copies.
Some data lines contain information regarding the number of copies
currently on loan. You should call the appropriate constructor and
intialize the book to the type of data input.


* There is a file of transactions (called trans) containing
transactions to be processed. Each transaction contains the following:
transaction type: b (borrow) or r (return)
account number: book code
amount: number of copies to be borrowed/returned
The appropriate object (book) corresponding to the book code should be
located. The balance should be updated by the amount input. The update
should be an increase (in the case of a deposit) or a decrease (in the
case of a withdrawal).

Write a function processTransactions that will read the file trans and
process the transactions (and create a log).

* Write a function printFull. This function will print the inventory of
books held, including the author, title, code, number of copies in
holding, and number of copies available. The sequence of this report
should be in the sequence of the actual array at the time that it is
printed.

* Write a main function that will initialize the system and declare
whatever variables are needed. You should create an array of objects of
type Book (max size = 25). The main function should call the following
functions:
readLibrary
printFull
processTransactions
printFull

--------------------

Now. I am NOT asking anyone to do this assignment for me. I needed to
type all that out so that one would know exactly what I'm talking about
and be of greater assistance :)

(My professor's lesson on classes & objects left much to be desired.
Turning to the textbook was no help, either, because he's actually one
of the author's of the book... and he pretty much teaches directly from
the text. I looked up the textbook on ACCU's book reviews... and
couldn't find it. So. Yeah. Not the greatest textbook.)

My class interface and the implementations of subsequent methods:
--------------------------------------------------------
class Book {
private:
char author[20];
char title[25];
char code[4];
int ncopies;
int onloan;
public:
Book( char *auth, char *tit, char *cd, int ncop );
Book( char *auth, char *tit, char *cd, int ncop, int
nonloan );
Book( );
~Book( );
char *getAuthor( );
char *getTitle( );
char *getCode( );
int getNcopies( );
int getOnLoan( );
void Borrow( int );
void nReturn( int );
};

char *Book :: getAuthor( ) {
return author;
}

char *Book :: getTitle( ) {
return title;
}

char *Book :: getCode( ) {
return code;
}

int Book :: getNcopies( ) {
return ncopies;
}

int Book :: getOnLoan( ) {
return onloan;
}

void Book :: Borrow( int n ) {
onloan += n;
return;
}

Book :: Book( char *auth, char *tit, char *cd, int ncop ) {
strcpy( author, auth );
strcpy( title, tit );
strcpy( code, cd );
ncopies = ncop;
onloan = 0;
}

Book :: Book( char *auth, char *tit, char *cd, int ncop, int nonloan )
{
strcpy( author, auth );
strcpy( title, tit );
strcpy( code, cd );
ncopies = ncop;
onloan = nonloan;
}

Book :: Book( ) {
strcpy( author, "" );
strcpy( title, "" );
strcpy( code, "" );
ncopies = 0;
onloan = 0;
}

Book :: ~Book( ) {

}
--------------------------------------------------------


Now. Onto my question:
For some reason, I CANNOT figure out how to read in data. And how do I
use the constructors? I'm really confused.

If I declare:
#define NBOOKS 40
Book library[NBOOKS];

then my readLibrary will look like this:
--------------------------------------------------------
int readLibrary( Book library ) {
int i, j;
char boundary[] = "STOP";
j = 0; /* j counts number of records in library */
for( i = 0; i < NBOOKS; i++ ) {
fscanf( lib, "%s %s %s %d %d", &library.author,
&library.title, &library.code, &library.ncopies,
&library.onloan );
if( strcmp( library.author, boundary ) != 0 )
j++;
else
break;
}

return j;
}
--------------------------------------------------------

When I compile, I get the error:
no match for 'operator[]' in 'library'

So it's not right. What am I doing wrong? How do I make it work?

Also. If I use new and delete, how do incorporate that in there?


I know my questions are incredibly silly... but I don't feel like I'm
actually learning anything in this Data Structures class. I seem to
leave with more questions than answers :/


Thank you for any and all suggestions you can offer :)
 
J

Jonathan Mcdougall

For a homework assignment in my Data Structures/C++ class, I have to
create the interface and implementation for a class called Book, create
objects within the class, and process transactions that manipulate (and
report on) members of the class.

This seems to be quite of an assignment for you, and seeing the code
you provided, you don't seem to be ready for it. However, I'll do my
best.

Note that your teacher seems to teach C more than C++. Specifically:

1) c-style strings are used instead of std::string
2) arrays are used instead of standard containers
3) problems with const-correctness

Since your class is in C++, I'll convert that program in C++.
Interface consists of:
- 5 private variables
char author[20];
char title[25];
char code[4];

These should be

std::string author;
std::string title;
std::string code;
int ncopies; // number of copies owned
int onloan; // number of copies currently on loan

- 5 public methods + 1 other method
char *getAuthor; // returns (a pointer to) the author's name
char *getTitle; // returns (a pointer to) the title
char *getCode; // returns (a pointer to) the code of the book
int getNcopies; // returns the number of copies owned by the
library
int getOnLoan; // returns the number of copies currently on
loan

These should be

const std::string& getAuthor() const;
const std::string& getTitle() const;
const std::string& getCode() const;
int getNcopies() const;
int getOnLoad() const;
void Borrow( int ); // fixed number of copies are borrowed

- 2 required constructors + 1 optional constructor
Book( auth, tit, cd, ncop ) // creates an object of type Book
with the info specified & assumes number of copies on loan is 0
Book( auth, tit, cd, ncop, nonloan ) // creates an object of
type Book with the info specified

These can be merged by giving nonload a default value (such as 0).
Book( ) // allows you to declare objects of type Book without
initializing any values

And what does that give? Not all classes should have default
constructors. A book with no title or author or code is not a book.
Scrap that one.
* There is an initial file of information (called initbooks) that
contains the information for the initial library. This information is
stored on a file (and is not to be keyed in individually). Write a
function readLibrary that will read the input file and create a book
for each line of information read. As each data line is read, you
should allocate a new record (possibly from within an available array)
and initialize the data.

Each data line contains an author, title, code, and number of copies.
Some data lines contain information regarding the number of copies
currently on loan. You should call the appropriate constructor and
intialize the book to the type of data input.

There don't seem to be a format for that file. You'll have to devise a
simple one, such as a line separated list. Each book element is on a
line so you'll have five lines per book. That'll make it easier to read
strings containing spaces or commas (such as a book title).
* There is a file of transactions (called trans) containing
transactions to be processed. Each transaction contains the following:
transaction type: b (borrow) or r (return)
account number: book code
amount: number of copies to be borrowed/returned
The appropriate object (book) corresponding to the book code should be
located. The balance should be updated by the amount input. The update
should be an increase (in the case of a deposit) or a decrease (in the
case of a withdrawal).

Write a function processTransactions that will read the file trans and
process the transactions (and create a log).

So your file could contain

b 1234 2 // borrowed two copies of book 1234

That's easy to parse.
* Write a function printFull. This function will print the inventory of
books held, including the author, title, code, number of copies in
holding, and number of copies available. The sequence of this report
should be in the sequence of the actual array at the time that it is
printed.

That should be simple.
* Write a main function that will initialize the system and declare
whatever variables are needed. You should create an array of objects of
type Book (max size = 25). The main function should call the following
functions:
readLibrary
printFull
processTransactions
printFull
Ok.

Now. I am NOT asking anyone to do this assignment for me.

I hope you're not :)
My class interface and the implementations of subsequent methods:
--------------------------------------------------------

# include <string>
# include said:
class Book {
private:
char author[20];
char title[25];
char code[4];
int ncopies;
int onloan;

std::string author;
std::string title;
std::string code;
int ncopies;
int onload;
public:
Book( char *auth, char *tit, char *cd, int ncop );
Book( char *auth, char *tit, char *cd, int ncop, int
nonloan );

Book(
const std::string& auth, const std::string& tit, const std::string&
cd,
int ncop, int nonload=0);

Forget that one.
~Book( );

You don't need a destructor.
char *getAuthor( );
char *getTitle( );
char *getCode( );
int getNcopies( );
int getOnLoan( );

Make these

const std::string& getAuthor() const;
const std::string& getTitle() const;
const std::string& getCode() const;
int getNcopies() const;
int getOnLoan() const;
void Borrow( int );
void nReturn( int );

Take the habit of mentionning the parameters name, even if the compiler
doesn't use them.

void Borrow(int qty);
void nReturn(int qty);
};

char *Book :: getAuthor( ) {
return author;
}

const std::string& Book::getAuthor() const
{
return author;
}

And do the same for the other member functions.
Now. Onto my question:
Good.

For some reason, I CANNOT figure out how to read in data. And how do I
use the constructors? I'm really confused.

If I declare:
#define NBOOKS 40
Book library[NBOOKS];

Don't! I won't start a discussion concerning arrays, they are evil. Use
a vector:

typedef std::vector<Book> Library;

int main()
{
Library lib;
readLibrary(lib);
then my readLibrary will look like this:

Change that to

int readLibrary(Library& lib)
{
int i, j;
char boundary[] = "STOP";
j = 0; /* j counts number of records in library */
for( i = 0; i < NBOOKS; i++ ) {
fscanf( lib, "%s %s %s %d %d", &library.author,
&library.title, &library.code, &library.ncopies,
&library.onloan );
if( strcmp( library.author, boundary ) != 0 )
j++;
else
break;
}

return j;
}


Now now now, how about some C++ here? As I said, putting book values on
different lines will make it easier.

// data example, books.dat
Bjarne Stroustrup
The C++ Programming Language, 3rd Edition
0001
10
2


# include <fstream>
# include <sstream>

int readLibrary(Library& lib)
{
// open the file
std::ifstream ifs("books.dat");

// we'll detect EOF inside
while (true)
{
// string values from the data file
// copies and load have a _s suffix (string)
// because they are temporary objects
// they will be converted to ints later on
std::string title, author, code, copies_s, loan_s;

// get five lines
getline(ifs, title);
getline(ifs, author);
getline(ifs, code);
getline(ifs, copies_s);
getline(ifs, loan_s);

// if we were at the end of file,
// or if there was any problems,
// one of the calls to getline() failed.
// we check it here
if (!ifs)
break;

// istringstream is useful for
// converting strings to integers
std::istringstream iss;

// convert copies_s to an int
int copies = 0;
iss.str(copies_s);
iss >> copies;

// convert loan_s to an int
int loan = 0;
iss.str(loan_s);
iss >> loan;

// create the book and add it
// to the library
lib.push_back(Book(title, author, code, copies, load));
}
}

As an example, here's how to output all the books in the library

void f(Library& lib)
{
for (Library::iterator itor=lib.begin(); itor!=lib.end(); ++itor)
{
Book& b = *itor;

std::cout
<< "Title: " << b.title() << "\n"
<< "Author: " << b.author() << "\n";
}
}


Jonathan
 
F

fakeprogress

6 C:\CIS\22\asn2\asn2.cpp In file included from C:\CIS\22\asn2\asn2.cpp

31 C:\CIS\22\asn2\library.hpp prototype for `const std::string
Book::getNcopies() const' does not match any in class `Book'
13 C:\CIS\22\asn2\library.hpp int Book::getNcopies() const
 
F

fakeprogress

Upon following your suggestions, Jonathan Mcdougall, my program will
not compile. I get a lot of errors:

6 C:\CIS\22\asn2\asn2.cpp In file included from C:\CIS\22\asn2\asn2.cpp
31 C:\CIS\22\asn2\library.hpp prototype for `const std::string
Book::getNcopies() const' does not match any in class `Book'
13 C:\CIS\22\asn2\library.hpp int Book::getNcopies() const
31 C:\CIS\22\asn2\library.hpp `const std::string Book::getNcopies()
const' and `int Book::getNcopies() const' cannot be overloaded
C:\CIS\22\asn2\library.hpp In member function `const std::string
Book::getNcopies() const':
32 C:\CIS\22\asn2\library.hpp initializing argument 1 of
`std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'
32 C:\CIS\22\asn2\library.hpp At global scope:
35 C:\CIS\22\asn2\library.hpp prototype for `const std::string
Book::getOnLoan() const' does not match any in class `Book'
14 C:\CIS\22\asn2\library.hpp int Book::getOnLoan() const
35 C:\CIS\22\asn2\library.hpp `const std::string Book::getOnLoan()
const' and `int Book::getOnLoan() const' cannot be overloaded
C:\CIS\22\asn2\library.hpp In member function `const std::string
Book::getOnLoan() const':
36 C:\CIS\22\asn2\library.hpp invalid conversion from `const int' to
`const char*'
36 C:\CIS\22\asn2\library.hpp initializing argument 1 of
`std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'
C:\CIS\22\asn2\asn2.cpp In function `int main()':
12 C:\CIS\22\asn2\asn2.cpp `readLibrary' undeclared (first use this
function)
(Each undeclared identifier is reported only once for each function
it appears in.)
C:\CIS\22\asn2\asn2.cpp In function `void readLibrary(Library&)':
17 C:\CIS\22\asn2\asn2.cpp `void readLibrary(Library&)' used prior to
declaration
59 C:\CIS\22\asn2\asn2.cpp `load' undeclared (first use this function)
C:\CIS\22\asn2\library.hpp In function `void printFull(Library&)':
4 C:\CIS\22\asn2\library.hpp `std::string Book::title' is private
69 C:\CIS\22\asn2\asn2.cpp within this context
69 C:\CIS\22\asn2\asn2.cpp no match for call to `(std::string) ()'
3 C:\CIS\22\asn2\library.hpp `std::string Book::author' is private
70 C:\CIS\22\asn2\asn2.cpp within this context
70 C:\CIS\22\asn2\asn2.cpp no match for call to `(std::string) ()'

My code is:
library.hpp
----------------
class Book {
private:
std::string author;
std::string title;
std::string code;
int ncopies;
int onloan;
public:
Book( const std::string &auth, const std::string &tit,
const std::string &cd, int ncop, int nonload = 0 );
const std::string &getAuthor( ) const;
const std::string &getTitle( ) const;
const std::string &getCode( ) const;
int getNcopies( ) const;
int getOnLoan( ) const;
void Borrow( int qty );
void nReturn( int qty );
};

const std::string &Book::getAuthor( ) const {
return author;
}

const std::string &Book::getTitle( ) const {
return title;
}

const std::string &Book::getCode( ) const {
return code;
}

const std::string Book::getNcopies( ) const {
return ncopies;
}

const std::string Book::getOnLoan( ) const {
return onloan;
}
----------------

asn2.cpp
----------------
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include "library.hpp"

typedef std::vector<Book> Library;

int main( ) {
Library lib;
readLibrary( lib );

return 0;
}

void readLibrary( Library &lib ) {
// open the file
std::ifstream ifs("books.dat");

// we'll detect EOF inside
while( true ) {
// string values from the data file
// copies and load have a _s suffix (string)
// because they are temporary objects
// they will be converted to ints later on
std::string title, author, code, copies_s, loan_s;

// get five lines
getline(ifs, title);
getline(ifs, author);
getline(ifs, code);
getline(ifs, copies_s);
getline(ifs, loan_s);

// if we were at the end of file,
// or if there was any problems,
// one of the calls to getline() failed.
// we check it here
if (!ifs)
break;

// istringstream is useful for
// converting strings to integers
std::istringstream iss;

// convert copies_s to an int
int copies = 0;
iss.str(copies_s);
iss >> copies;

// convert loan_s to an int
int loan = 0;
iss.str(loan_s);
iss >> loan;

// create the book and add it
// to the library
lib.push_back(Book(title, author, code, copies, load));
}

return;
}

void printFull( Library &lib ) {
for (Library::iterator itor=lib.begin(); itor!=lib.end(); ++itor)
{
Book& b = *itor;
std::cout
<< "Title: " << b.title( ) << "\n"
<< "Author: " << b.author( ) << "\n";
}

return;
}
 
J

Jonathan Mcdougall

6 C:\CIS\22\asn2\asn2.cpp In file included from C:\CIS\22\asn2\asn2.cpp

31 C:\CIS\22\asn2\library.hpp prototype for `const std::string
Book::getNcopies() const' does not match any in class `Book'
13 C:\CIS\22\asn2\library.hpp int Book::getNcopies() const

Ah.. well.. several things here.

1) quote the message you are answering to
2) people here expect you to work a bit before asking some questions,
don`t just dump me an error message, that's not very motivating
3) the error is exactly as stated:
const std::string Book::getNcopies() const
does not match any in class `Book'
int Book::getNcopies() const

As you can see, the return value of the declaration is different.
Change it.


Jonathan
 
J

Jonathan Mcdougall

Upon following your suggestions, Jonathan Mcdougall, my program will
not compile. I get a lot of errors:

You`re serious? You should to learn to understand these messages and
try to fix them. That's your job, not mine. Most of them are obvious,
which shows that you didn't even *look* at them.
const std::string Book::getNcopies( ) const {
return ncopies;
}

const std::string Book::getOnLoan( ) const {
return onloan;
}

These should return an int, as you did in the class definition.
typedef std::vector<Book> Library;

You must declare readLibrary() here.
int main( ) {
Library lib;
readLibrary( lib );

return 0;
}

void readLibrary( Library &lib ) {
lib.push_back(Book(title, author, code, copies, load));

This should be "loan", not "load".
}

return;
}

void printFull( Library &lib ) {
for (Library::iterator itor=lib.begin(); itor!=lib.end(); ++itor)
{
Book& b = *itor;
std::cout
<< "Title: " << b.title( ) << "\n"
<< "Author: " << b.author( ) << "\n";

These member functions do not exist in your class, change them.


Jonathan
 
F

fakeprogress

Jonathan said:
1) quote the message you are answering to
I'm sorry. I was going to but I pressed the Post Message button by
accident... Sorry!
2) people here expect you to work a bit before asking some questions,
don`t just dump me an error message, that's not very motivating
Again... the Post Message button was clicked by accident.
3) the error is exactly as stated:
const std::string Book::getNcopies() const
does not match any in class `Book'
int Book::getNcopies() const

As you can see, the return value of the declaration is different.
Change it.
Right. Thanks.

I changed all that and now I get these errors:
C:\CIS\22\asn2\library.hpp In function `void printFull(Library&)':
4 C:\CIS\22\asn2\library.hpp `std::string Book::title' is private
75 C:\CIS\22\asn2\asn2.cpp within this context
75 C:\CIS\22\asn2\asn2.cpp no match for call to `(std::string) ()'
3 C:\CIS\22\asn2\library.hpp `std::string Book::author' is private
76 C:\CIS\22\asn2\asn2.cpp within this context
76 C:\CIS\22\asn2\asn2.cpp no match for call to `(std::string) ()'

According to the professor's instructors, they have to be private... so
what I can do?

Thank you so much for all your help, Jonathan :D It's really
appreciated :)
 
F

fakeprogress

Jonathan said:
(e-mail address removed) wrote:

These member functions do not exist in your class, change them.

Gah. I'm so stupid. I changed them to getTitle and getAuthor.

Again, thank you, thank you, thank you...! And another THANK YOU for
putting up with my silly requests and inability to read erros o_O
(Sorry about that! Really! I just saw a large log and got frightened. I
know, I know! *hangs head in shame*)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top