0

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");
         }
}
2
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by anuragcoder
0

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){
0

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

0

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 ?

0

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

0

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.

0

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 !

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.