I have made the following code to find a string in some lines of text
But it always mentions the error:

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

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
int main(){
    cout << "Please enter some text" <<endl;
    string text;
    string ch;
    getline(cin, text, '~');
    istringstream iss(text);
    string x;
    iss >> x;
    
    ch = text;
    
    system("PAUSE");
    cout << "-----------------"<<endl;
    string need;
    cin >> need;
    if (strcmp(x, need ) == 0){
                     cout << "Exact.." << endl;
                     system("PAUSE");}
    else{
         cout << "ErR."<<endl;
         system("PAUSE");
         }
}

Recommended Answers

All 10 Replies

strcmp is defined in <string.h> or <cstring> .
Even if you include it. the function only takes const char* as arguments.

So if you wish to use it. you can do this

if (strcmp(x.c_str(), need.c_str() ) == 0){

at your if loop .

Or if you are checking for equivallence just use the == operator.

if (x == need){

Hey, but here x is a text content with many lines but need is just one word
Can you please explain the use of istringstream....

I'll still try the options out

Both of them ain't workin.............

Firstly Post down what exactly do you have to do.

And secondly.

>>Both of them ain't workin............

Do you mean that the code doesn't compile or its not performing what exactly you want to do ?

Firstly:
My program has to take multiple line input.
The delimiter is ~
Then the program should take in the word i want to search.
Finally,
if string is found, then replies exact
else error....

secondly:
They compile and run. But they don't seem to do as shown above

Help appreciated.

Anurag

Hey anurag.
you do realise that you are only testing the first word of the text itself with this code. Dont you?

There is no looping mechanism in here.

yeah.......thanks
I'm confused
:s

Please hint a looping mech
im out of all of them

Hmmm.

Your basic algorithm will be ,

while you find the word or reach the end.
Take in the word.
Compare word with the variable needed to find.
if word = find.
then break out of the loop.
or continue executing !

Thanks,, Sky diploma.....
I owe you this one, dude.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.