Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
String comparison?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="fakeprogress, post: 2560786"] 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 Obviously, I'm doing something wrong. Where is my mistake? =/ Thanks so much! =) [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
String comparison?
Top