Image

Imagedelusory wrote in Imagecpp eh

Constructor problem?

PROBLEM SOLVED! THANK YOU!


I'm writing a program that uses classes to maintain a library of books. However, when I read in the data, the number of copies on loan always comes up as 0 (zero), even if there is already a value there. (I'm using two constructors to deal with the "on loan" value: (1) if there is no value for "on loan" in library.txt, it is set to zero, and (2) if there is a value, that's the value that is read in and stored.)

Below is my program:

library.hpp
#ifndef LIBRARY_HPP
#define 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 nonloan );
            Book( const std::string &auth, const std::string &tit, const std::string &cd, int ncop );
            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 );
};

Book::Book( const std::string &auth, const std::string &tit, const std::string &cd, int ncop, int nonloan ) {
      author.assign( auth.begin( ), auth.end( ) );
      title.assign( tit.begin( ), tit.end( ) );
      code.assign( cd.begin( ), cd.end( ) );
      ncopies = ncop;
      onloan = nonloan;
      return;
}

Book::Book( const std::string &auth, const std::string &tit, const std::string &cd, int ncop ) {
      author.assign( auth.begin( ), auth.end( ) );
      title.assign( tit.begin( ), tit.end( ) );
      code.assign( cd.begin( ), cd.end( ) );
      ncopies = ncop;
      onloan = 0;
      return;
}

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

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

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

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

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

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

void Book :: nReturn( int qty ) {
      onloan -= qty;
      return;
}
#endif


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

typedef std::vector<Book> Library;

void readLibrary( Library &lib );
void printFull( Library &lib );

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

     return 0;
}

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

     // 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 author, title, code, ncopies_s, onloan_s;

          // get five lines
          getline( books, author );
          getline( books, title );
          getline( books, code );
          getline( books, ncopies_s );
          getline( books, onloan_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( !books )
               break;

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

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

          // convert loan_s to an int
          int onloan;
          iss.str( onloan_s );
          iss >> onloan;

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

     return;
}

void printFull( Library &lib ) {

     std::cout.width( 11 );
     std::cout << "AUTHOR\t";
     std::cout.width( 23 );
     std::cout << "TITLE\t";
     std::cout.width( 4 );
     std::cout << "CODE\t";
     std::cout.width( 2 );
     std::cout << "COPIES\t";
     std::cout.width( 3 );
     std::cout << "OUT\t";
     std::cout.width( 2 );
     std::cout << "IN\n";
     
     for( Library::iterator itor = lib.begin(); itor != lib.end( ); ++itor ) {
          Book &b = *itor;
          std::cout.width( 10 );
          std::cout << b.getAuthor( ) << "\t";
          std::cout.width( 22 );
          std::cout << b.getTitle( ) << "\t";
          std::cout.width( 4 );
          std::cout << b.getCode( ) << "\t";
          std::cout.width( 6 );
          std::cout << b.getNcopies( ) << "\t";
          std::cout.width( 3 );
          std::cout << b.getOnLoan( ) << "\t";
          std::cout.width( 2 );
          std::cout << ( b.getNcopies( ) - b.getOnLoan( ) ) << "\n";
     }

     return;
}


library.txt
Habermann
Operating Systems
H01
10
5
Golumbic
Graph Theory
G01
5
2
Jacobs
Database Logic
J01
3
 
Kanter
Management Information
K01
8
1
Kuo
Numerical Methods
K02
2
0
Hughs
Structured Programming
H02
4
4
Schroder
C
S01
7
 
Herzog
Computing Structures
H03
10
7
Hunter
Understanding C
H04
6
6
Holub
Compiler Design
H05
11
8
Galvin
Operating Systems
G02
15
2
Lane
Data Communications
L01
20
3
Kleinrock
Queueing Systems
K03
14
0
Kindred
Data Systems
K04
2
0
Mano
Computer Architecture
M01
2
2
Horowitz
Programming Languages
H06
16
10


output
    AUTHOR	                 TITLE	CODE	COPIES	OUT	IN
 Habermann	     Operating Systems	 H01	    10	  0	10
  Golumbic	          Graph Theory	 G01	     5	  0	 5
    Jacobs	        Database Logic	 J01	     3	  0	 3
    Kanter	Management Information	 K01	     8	  0	 8
       Kuo	     Numerical Methods	 K02	     2	  0	 2
     Hughs	Structured Programming	 H02	     4	  0	 4
  Schroder	                     C	 S01	     7	  0	 7
    Herzog	  Computing Structures	 H03	    10	  0	10
    Hunter	       Understanding C	 H04	     6	  0	 6
     Holub	       Compiler Design	 H05	    11	  0	11
    Galvin	     Operating Systems	 G02	    15	  0	15
      Lane	   Data Communications	 L01	    20	  0	20
 Kleinrock	      Queueing Systems	 K03	    14	  0	14
   Kindred	          Data Systems	 K04	     2	  0	 2
      Mano	 Computer Architecture	 M01	     2	  0	 2
  Horowitz	 Programming Languages	 H06	    16	  0	16


What am I doing wrong? =/

Thanks!