String comparison?

Discussion in 'C++' started by fakeprogress@gmail.com, May 24, 2006.

  1. Guest

    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
    `std::basic_string<char, std::char_traits<char>, std::allocator<char>
    >::compare(<unknown type>)'


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

    Thanks so much! =)
     
    , May 24, 2006
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    wrote:

    > 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
     
    Kai-Uwe Bux, May 25, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    2
    Views:
    27,938
    Phil Hanna
    Sep 20, 2003
  2. grz02

    Java String comparison

    grz02, Feb 23, 2005, in forum: Java
    Replies:
    40
    Views:
    15,846
    nooobody
    Mar 5, 2005
  3. Replies:
    21
    Views:
    1,452
    Alex Vinokur
    Aug 18, 2007
  4. Smithers
    Replies:
    12
    Views:
    1,207
    Ben Voigt [C++ MVP]
    Jul 7, 2009
  5. Deepu
    Replies:
    1
    Views:
    266
    ccc31807
    Feb 7, 2011
Loading...

Share This Page