944,044 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 15543
  • C++ RSS
Apr 8th, 2006
0

no matching function for call to 'strcmp(std::string&, std::string&)'

Expand Post »
Hello,
I am one of those new assignment people, BUT I have tried to resolve this for a while now. Cant find much on google or other C++ sites.

I am trying to compare string lengths of 3 words and then display them in order of length.

My code is (I get stuck just before my first if statement):

#include <iostream>
#include <string.h>

using namespace std;

int main ()
{
int x,y,z;
string word1 = "word1 string";
string word2 = "word2 string";
string word3 = "word3 string";


cout<<"Please enter 3 words/n";
cin>>word1>>word2>>word3;

x = strcmp(word1, word2); ***THIS LINE IS THE PROBLEM***
{

if (strcmp (word1, word2)< 0)
wordA = word1
wordB = word2
}
if (strcmp (word1, word2) > 0)
{
wordA =word2
wordB =word1
}

y = strcmp (wordA, word3)
if (y < 0)
wordsml = wordA

z = strcmp (wordB, word3)
if (z < 0)
wordm = wordB

if (z > 0)
wordlge = word3

cout<<"The order of words from smallest to largest is "<<word sml<<" and "<<wordm<<" and "<<wordlge<,endl;

system("pause");
return 0

}

I know I have done something wrong in my setout, but the explanation given to me is not very clear to me and I dont know how to resolve : ******no matching function for call to 'strcmp(std::string&, std::string&)' ****** Can someone head me in the right direction?



Thanks!
Reputation Points: 10
Solved Threads: 0
Light Poster
newgurl is offline Offline
30 posts
since Apr 2006
Apr 8th, 2006
0

Re: no matching function for call to 'strcmp(std::string&, std::string&)'

strcmp takes C-style strings, not C++ string objects. The string class has overloaded the relational operators, so you can use < and > to compare them. But if you have your heart set on strcmp, you can do this:
C++ Syntax (Toggle Plain Text)
  1. x = strcmp(word1.c_str(), word2.c_str());
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 9th, 2006
0

Re: no matching function for call to 'strcmp(std::string&, std::string&)'

and u can use stricmp() instead of strcmp() to ignore upper case n lower case ..... actually this is not good way to compare string sizes....... bcoz:
Quote ...
int strcmp (const char str1[], const char str2[]);
compares str1 and str2 if they have same character.
try
Quote ...
int strlen (const char str[]);
function..... find both length seperately using this function, then compare.... good luck!
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Going Insane
Next Thread in C++ Forum Timeline: I have C++ course





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC