Processing transactions...?
If this looks familiar, it's because I posted here yesterday o_O
CODE
#include <iostream> #include <string> #include <vector> #include <fstream> #include <sstream>
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;
}typedef std::vector<Book> Library;
int findcode( Library &lib, std::string code );
void printFull( Library &lib );
void processTransactions( Library &lib );
void readLibrary( Library &lib );
int main( ) {
Library lib;
readLibrary( lib );
printFull( lib );
std::cout << "\n";
processTransactions( lib );
printFull( lib );
return 0;
}// make sure that code exists
int findcode( Library &lib, std::string tcode ) {
for( Library::iterator itor = lib.begin( ); itor != lib.end( ); ++itor ) {
Book &b = *itor;
if( b.getCode( ) == tcode )
return 1;
}
return -1;
}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;
}void processTransactions( Library &lib ) {
// string values from the data file
// copies has a _s suffix (string)
// because it's a temporary object
// it will be converted to int later on
std::string action, code, copies_s;
// used to conver copies_s to int
int copies = 0;
// open the file
std::ifstream trans( "trans.txt" );
std::cout << "Processing transactions...\n\n";
for( Library::iterator itor = lib.begin( ); itor != lib.end( ); ++itor ) {
Book &b = *itor;
// we'll detect EOF inside
while( trans ) {
// get three lines
getline( trans, action );
getline( trans, code );
getline( trans, copies_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( !trans )
break;
// istringstream is useful for
// converting strings to integers
std::istringstream iss;
// convert copies_s to an int
iss.str( copies_s );
iss >> copies;
}
if( findcode( lib, code ) == -1 )
std::cout << "Invalid book code: Could not update info for book " << code << ".\n";
else if( ( action == "b" ) && ( b.getNcopies( ) - b.getOnLoan( ) ) >= copies ) {
b.Borrow( copies );
std::cout << copies << " copies of " << code << " were borrowed. Current availability: " << b.getNcopies( ) - b.getOnLoan( ) << ".\n";
}
else if( ( action == "b" ) && ( b.getNcopies( ) - b.getOnLoan( ) ) < copies )
std::cout << copies << " copies of " << code << " were not borrowed. Insufficient on hand.\n";
else if( ( action == "r" ) && copies <= b.getOnLoan( ) ) {
b.nReturn( copies );
std::cout << copies << " copies of " << code << " were returned. Current availability: " << b.getNcopies( ) - b.getOnLoan( ) << ".\n";
}
else if( ( action == "r" ) && copies > b.getOnLoan( ) )
std::cout << copies << " copies of " << code << " were not returned. Exceed total holdings.\n";
else if( ( action != "b" ) && ( action != "r" ) )
std::cout << "Invalid action type '" << action << ".'\n";
}
std::cout << "\n";
return;
}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 loan 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;
// iss is hitting EOF state when it
// reads ncopies_s and runs out of data.
// clear() is used to clear the EOF state
// before setting the new string buffer
iss.clear( );
// 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;
}This here is the input:
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
trans.txt
b H01 3 b S01 2 b H04 3 r G01 1 r M01 1 b P12 5 b J01 2 b K04 2 r H04 4 b H04 3 r M01 5 b G02 10 r G01 3
OUTPUT
AUTHOR TITLE CODE COPIES OUT IN
Habermann Operating Systems H01 10 5 5
Golumbic Graph Theory G01 5 2 3
Jacobs Database Logic J01 3 2 1
Kanter Management Information K01 8 1 7
Kuo Numerical Methods K02 2 0 2
Hughs Structured Programming H02 4 4 0
Schroder C S01 7 4 3
Herzog Computing Structures H03 10 7 3
Hunter Understanding C H04 6 6 0
Holub Compiler Design H05 11 8 3
Galvin Operating Systems G02 15 2 13
Lane Data Communications L01 20 3 17
Kleinrock Queueing Systems K03 14 0 14
Kindred Data Systems K04 2 0 2
Mano Computer Architecture M01 2 2 0
Horowitz Programming Languages H06 16 10 6
Processing transactions...
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
Invalid action type '.'
AUTHOR TITLE CODE COPIES OUT IN
Habermann Operating Systems H01 10 5 5
Golumbic Graph Theory G01 5 2 3
Jacobs Database Logic J01 3 2 1
Kanter Management Information K01 8 1 7
Kuo Numerical Methods K02 2 0 2
Hughs Structured Programming H02 4 4 0
Schroder C S01 7 4 3
Herzog Computing Structures H03 10 7 3
Hunter Understanding C H04 6 6 0
Holub Compiler Design H05 11 8 3
Galvin Operating Systems G02 15 2 13
Lane Data Communications L01 20 3 17
Kleinrock Queueing Systems K03 14 0 14
Kindred Data Systems K04 2 0 2
Mano Computer Architecture M01 2 2 0
Horowitz Programming Languages H06 16 10 6
findcode( ) function, obviously. What's not obvious to me is what the actual problem is o_O Help me? Please?