473,386 Members | 1,679 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,386 software developers and data experts.

Converting C to C++

How would I go about converting this C code to C++?

/* LIBRARY is an array of structures */
/* This function compares 'tcode' with */
/* existing codes in the array. */
/* It returns the index of the code in */
/* the LIBRARY structure if it is found. */
int findcode( LIBRARY *b, int n, char *tcode ) {
int i;

for( i = 0; i < n; i++ )
if( strcmp( b[i].code, tcode ) == 0 )
return i;

return -1;
}
This is what I have:
(I just need to know if the code exists [ie, function returns 1] in the
class. Indeces are not necessary.)

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 );
};

typedef std::vector<Book> Library;

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;
}

However, I get this error:

37 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>)'


Something about the use of compare() in the C++ findcode()...

Help, please?

May 24 '06 #1
3 2156
posted:
How would I go about converting this C code to C++?


Copy-paste it into a new file, then go through it looking for
inconsistencies between C and C++. Here's a guide:

http://david.tribble.com/text/cdiffs.htm
As for, changing the "style" of it from "C style" to "C++ style"... well
maybe just get a good C++ book.
-Tomás
May 24 '06 #2
In article <11**********************@j73g2000cwa.googlegroups .com>,
fa**********@gmail.com wrote:
How would I go about converting this C code to C++?

/* LIBRARY is an array of structures */
/* This function compares 'tcode' with */
/* existing codes in the array. */
/* It returns the index of the code in */
/* the LIBRARY structure if it is found. */
int findcode( LIBRARY *b, int n, char *tcode ) {
int i;

for( i = 0; i < n; i++ )
if( strcmp( b[i].code, tcode ) == 0 )
return i;

return -1;
} (I just need to know if the code exists [ie, function returns 1] in the
class. Indeces are not necessary.)
// Libraries hold more than just books, but everything a
// library holds must have a code associated with it.
class Resource
{
string code;
public:
Resource( string code_ ): code( code_ ) { }
virtual ~Resource() { }
const string& getCode() const { return code; }
};

struct has_code : unary_function<Resource, int>
{
string code;
has_code( const string& code_ ): code( code_ ) { }
bool operator()( const Resource& resource ) const {
return resource.getCode() == code;
}
};

class Library
{
vector<Resource> resources;
public:
bool codeExists( const string& tcode ) const {
return find_if( resources.begin(), resources.end(),
has_code( tcode ) ) != resources.end();
}
};

class Book : public Resource
{
// add whatever here
};

This is what I have:
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 );
};

typedef std::vector<Book> Library;
I am inclined to make Library a full fledged class...
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;
}


bool findcode( const Library& lib, const string& tcode )
{
for (Library::const_iterator it = lib.begin(); it != lib.end(); ++it)
if ( it->getCode() == tcode )
return true;
return false;
}

(a) If all you need to know is if something with the code 'tcode'
exists, then return a 'bool' instead of an 'int'.

(b) You don't need to cast the '*itor' into a 'Book&'

(c) You don't need to use the 'compare' member-function, simply compare
them with operator==.

(d) 'b.getCode' needs parens after it... 'b.getCode()'
May 25 '06 #3
In article <1148511337.722910.174590
@j73g2000cwa.googlegroups.com>, fa**********@gmail.com
says...
How would I go about converting this C code to C++?

/* LIBRARY is an array of structures */
/* This function compares 'tcode' with */
/* existing codes in the array. */
/* It returns the index of the code in */
/* the LIBRARY structure if it is found. */
int findcode( LIBRARY *b, int n, char *tcode ) {
int i;

for( i = 0; i < n; i++ )
if( strcmp( b[i].code, tcode ) == 0 )
return i;

return -1;
}
First I'd want to know whether LIBRARY::code is the
primary (or perhaps, only) method of identifying an item
in the library. If it is, I'd probably do something like
this:

typedef std::multimap<std::string, item_data> lib_t;

lib_t library;

The key of the map is equivalent to LIBRARY.code in your
function above. Finding the data for a particular item
looks like:

lib_t::iterator pos = library.find(tcode);

This returns an iterator instead of an int, so instead of
library[pos] you use *pos to refer to the data associated
with an item in the library.
This is what I have:
(I just need to know if the code exists [ie, function returns 1] in the
class. Indeces are not necessary.)


In that case, you could use something like:

bool findcode(lib_t const &lib, std::string const &code)
{
return lib.find(code) != lib.end();
}

From the looks of things, you probably want to be able to
look things up by code, title, or author. In that case,
I'd probably do things a bit differently. One possibility
would be to look at the boost libraries -- if memory
serves, they have a pre-built class for a set or map with
multiple index fields.

If you prefer to use the standard library, I'd make
library a vector<item>, and then each index would be a
multimap<std::string, int>, taking the appropriate
string, and returning the index of the appropriate item
in the vector. Considering that you're likely to have
more than one book by a particular author, you might
prefer to look at using equal_range to get all the books
by an author at once.

One basic point: the fact that your C code used a linear
search in an array doesn't mean that you really want to
continue doing that. In fact, unless your library will
always be really tiny, you're probably better off doing
something else.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 25 '06 #4

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

Similar topics

4
by: mustafa | last post by:
Dear sir , I have built my application in visual basic 6.0 and crystal Report8.5 , Now i migrated my application to VB.net using the upgrade wizard.My visual basic form is upgraded to vb.net...
29
by: Armand Karlsen | last post by:
I have a website ( http://www.zen62775.zen.co.uk ) that I made HTML 4.01 Transitional and CSS compliant, and I'm thinking of converting it into XHTML to learn a little about it. Which XHTML variant...
8
by: prabha | last post by:
Hello Everybody, I have to conert the word doc to multiple html files,according to the templates in the word doc. I had converted the word to xml.Also through Exsl ,had finished the multiple...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
3
by: Mary | last post by:
Hi, Does anyone know of any software out there that would convert an application written in VBScript to either VB.NET or C#/C++ quite quickly for me, or will I have to re-write the application...
2
by: Map Reader | last post by:
Greetings, I am converting an old VB6 application to use .NET. One of the old controls loads icons from the disk and displays them. However, the transparent color turns to blue somewhere in the...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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,...

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.