473,782 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::getAutho r( ) const {
return author;
}

const std::string &Book::getTitle ( ) const {
return title;
}

const std::string &Book::getCo de( ) const {
return code;
}

int Book::getNcopie s( ) 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<Boo k> Library;

int findcode( Library &lib, std::string code );
void printFull( Library &lib );
void processTransact ions( Library &lib );
void readLibrary( Library &lib );

int main( ) {
Library lib;

readLibrary( lib );
printFull( lib );
processTransact ions( lib );
printFull( lib );

return 0;
}

int findcode( Library &lib, std::string tcode ) {
for( Library::iterat or 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 processTransact ions( Library &lib ) {
std::ifstream trans( "trans.txt" );

std::string action, code;
int copies;

for( Library::iterat or 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_str ing<char, std::char_trait s<char>, std::allocator< char>
::compare(<unk nown type>)'


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

Thanks so much! =)

May 24 '06 #1
1 1919
fa**********@gm ail.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::getAutho r( ) const {
return author;
}

const std::string &Book::getTitle ( ) const {
return title;
}

const std::string &Book::getCo de( ) const {
return code;
}

int Book::getNcopie s( ) 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<Boo k> Library;

int findcode( Library &lib, std::string code );
void printFull( Library &lib );
void processTransact ions( Library &lib );
void readLibrary( Library &lib );

int main( ) {
Library lib;

readLibrary( lib );
printFull( lib );
processTransact ions( lib );
printFull( lib );

return 0;
}

int findcode( Library &lib, std::string tcode ) {
for( Library::iterat or 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 processTransact ions( Library &lib ) {
std::ifstream trans( "trans.txt" );

std::string action, code;
int copies;

for( Library::iterat or 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
23397
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 my depth with most of the posts, I will buy a good book and take some online tutorials) Anyway, I think I almost understand one of Mr Nielsen's posts on form validation. It all centers around whether I am interpreting an empty string correctly -...
2
2551
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 variables by placing them at the same address (my g++ compiler does this). Therefore the output of the given C program is compiler dependent. What is worse, the program does not do what its writer most likely intended, since, std::set's find()...
8
2793
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 should you provide), a -String object- or a -string-? Given the following benchmark: var t = (new Date()).getTime(); for (var ii = 0; ii < 100000; ++ii) { var x = (String('hi')).charAt(0); // typeof 'string'
51
8296
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 code? many thx!
46
5173
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 was told that wouldn't it be wise to first get the length of the strings..if it doesn't match then they are not the same..I agreed...then he said..but again that would be an overhead first measuring the length...and then doing a character by...
5
657
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") System.Console.WriteLine("String1 Equal.");
4
7188
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 involved? By "as expected" I mean that as long as the strings are instantiated using string literals, then == and Equals are the same. string s1 = "xxx";
4
24883
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 standard library to do this? I'm not interested in Boost until it becomes part of the standard.
26
3812
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 test whether a string has a value or not by the following syntax: if (thisString.Trim() == "") {
6
4344
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 computation 2) compare the string binary data to get the similarity or dissimilarity result. The problem is, i already done with the image (jpg) conversion to binary and also try the algorithm structure in C# language, but i having a problem to...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.