473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

String comparison?

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 '06 #1
1 1904
fa**********@gmail.com 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
May 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: David Graham | last post by:
Hi I have been busy going through the last weeks postings in an attempt to absorb javascript syntax (I guess it's not possible to just absorb this stuff in a passive way - I'm getting way out of...
2
by: Neil Zanella | last post by:
Hello, Consider the following program. There are two C style string stack variables and one C style string heap variable. The compiler may or may not optimize the space taken up by the two stack...
8
by: Grant Wagner | last post by:
I'm a bit confused by String() (typeof 'string') vs new String() (typeof 'object'). When you need to access a method or property of a -String-, what type is JavaScript expecting (or rather, what...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
5
by: MaSTeR | last post by:
Can anyone provide a practical short example of why in C# I shouldn't compare two strings with == ? If I write this in JAVA String string1 = "Widget"; if (string1 == "Widget") ...
4
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings...
4
by: Jim Langston | last post by:
Is there any builtin lowercase std::string compare? Right now I'm doing this: if ( _stricmp( AmmoTypeText.c_str(), "GunBullet" ) == 0 ) AmmoType = Item_Ammo_GunBullet; Is there anything the...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.