String comparison?

F

fakeprogress

I am attempting to write a function (I shall call it findcode()) that
makes sure that a code read in from a file is an actual code, one found
within the library of books.

Here is what I have:
---
#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 );
processTransactions( lib );
printFull( lib );

return 0;
}

int findcode( Library &lib, std::string tcode ) {
for( Library::iterator itor = lib.begin( ); itor != lib.end( );
++itor ) {
Book &b = *itor;
if( ( tcode.compare( b.getCode ) ) == 0 )
return 1;
}

return -1;
}

// function printFull() goes here
// (code I have is functional, so I
// did not include it here to save space)

// this function is yet to be completed
// if you can offer any help on how I can
// do that, it would be greatly appreciated!
void processTransactions( Library &lib ) {
std::ifstream trans( "trans.txt" );

std::string action, code;
int copies;

for( Library::iterator itor = lib.begin(); itor != lib.end( );
++itor ) {
Book &b = *itor;
trans >> action;
trans >> code;
trans >> copies;
}

// findcode() would be called here somewhere...

// ...
}

// function readLibrary( ) goes here
// (again, I left it out because it's functional)

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

(The code P12, for example, is nonexistent in the library of books. The
function findcode() should recognize that and print an error message.)

However, my code does not work. I get the following error when I try to
compile the code in Dev-C++ 4.9.9.2:
C:\CIS\22\asn2\asn2.cpp no matching function for call to
::compare(<unknown type>)'

Obviously, I'm doing something wrong. Where is my mistake? =/

Thanks so much! =)
 
K

Kai-Uwe Bux

I am attempting to write a function (I shall call it findcode()) that
makes sure that a code read in from a file is an actual code, one found
within the library of books.

Here is what I have:
---
#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 );
processTransactions( lib );
printFull( lib );

return 0;
}

int findcode( Library &lib, std::string tcode ) {
for( Library::iterator itor = lib.begin( ); itor != lib.end( );
++itor ) {
Book &b = *itor;
if( ( tcode.compare( b.getCode ) ) == 0 )

try:

if( ( tcode.compare( b.getCode() ) ) == 0 )
return 1;
}

return -1;
}

// function printFull() goes here
// (code I have is functional, so I
// did not include it here to save space)

// this function is yet to be completed
// if you can offer any help on how I can
// do that, it would be greatly appreciated!
void processTransactions( Library &lib ) {
std::ifstream trans( "trans.txt" );

std::string action, code;
int copies;

for( Library::iterator itor = lib.begin(); itor != lib.end( );
++itor ) {
Book &b = *itor;
trans >> action;
trans >> code;
trans >> copies;
}

// findcode() would be called here somewhere...

// ...
}
[snip]


Best

Kai-Uwe Bux
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top