LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-20-2004, 03:33 AM   #1
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
Unhappy c++ strings-strcmp function......


#include <string> //header file
void bubbleSort1(string dat[],int size)
{
for(int pass=1;pass<size;pass++)
{
int i=0;
for(;i<size-pass
{
//string compare
if((strcmp(dat[i],dat[i+1])) )
{
string temp=dat[i];
dat[i]=dat[i+1];
dat[i+1]=temp;
}
i++;
}//for
}//outer for loop
}//bubbleSort


by using the header file(string) and all the 'strcmp' seems to give out errors while compiling the program.( i have only typed part of the program)
plz help me out
 
Old 08-20-2004, 04:40 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
“string” (now called “std::string”) is a C++ object, not just a pointer to a character array as in a null-terminated C string as expected by C's strcmp function.

Instead of using the C-style “strcmp” function, you probably want to use the C++ std::string's compare method:

Code:
if (dat[i].compare(dat[i+1])
You can also extract the C-style strings to pass to strcmp if you like — but this is much messier:

Code:
#include <string.h> //C header file
…
if (strcmp(dat[i].c_str(),dat[i+1].c_str())
By the way, your C++ compiler is out of date; according to the current version of the C++ standard, <string> should put its contents into the namespace “std”. If you upgrade your C++ compiler (or give the code to anyone else) and it stops compiling, then you can fix this by putting the line
Code:
using std;
after the #include lines, or by replacing the standard classes with their full names
Code:
void bubbleSort1(std::string dat[],int size)
Hope that helps,

—Robert J. Lee
 
Old 08-20-2004, 05:41 AM   #3
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Original Poster
Rep: Reputation: 15
>>>>>By the way, your C++ compiler is out of date
wut does this mean??
is that why i am getting error messages for the above coding...???
 
Old 08-20-2004, 08:56 AM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,991

Rep: Reputation: 76
Sorry; I have confused the issue here.

What you are doing is entirely correct; you don't need to rush off and upgrade your compiler.

However, a couple of years ago the standard for the C++ language was changed. It was considered far too messy to have all of your classes in the same “namespace”, as it prevented you from producing two different classes with the same name. To fix this, everything in the standard libraries was moved to the “std” namespace — so “string” becomes “std::string”, and so on.

A namespace is just a logical group of functions and objects; if an object is put in a namespace, then it has to be imported from that namespace.

If your compiler doesn't throw an error at the declaration “void bubbleSort1(string dat[],int size)”, then your compiler is too old to support namespaces. This isn't a problem, but it's something you should be aware of. Otherwise, replace it with “void bubbleSort1(std::string dat[],int size)” and it will work.

Either way, you should use the int string::compare(string &) method rather than the strcmp(char *, char *) function. Otherwise, you are passing a string object (from <string>) to an strcmp function (from <string.h>, which you haven't included), that expects a char* primative, not an object.


You can also use the < > <= and >= operators with strings:

Code:
if (dat[i] == dat[i+1])
 
Old 09-12-2004, 08:28 AM   #5
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Original Poster
Rep: Reputation: 15
sorry about the delay to get back.
>>#include <string.h> //C header file

if (strcmp(dat[i].c_str(),dat[i+1].c_str())

This coding helped me solve the problem.
Thank you very much!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C]How to compare two strings w/o using strcmp() kponenation Programming 22 11-23-2005 09:29 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM
Need help with C function/strings Scrag Programming 2 12-12-2004 01:54 PM
strcmp and some problems I have [strange ones] zeppelin Programming 8 04-03-2004 07:02 PM
strcmp function does not seemed to work Linh Programming 4 06-12-2003 07:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration