-1
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   char str[80];
   char find[10];
   char change[10];
   char* pstr;
   char* temp = new char[80];
   cin.getline(str, 80);
   cin.getline(find,10);
   cin.getline(change,10);

   pstr =strstr(str,find);

     int a = str.find(pstr);
     int len;
     len = strlen(find);
     str.replace(a,len,change);



   delete []temp;
   cout<< str<< endl;
   return 0;
}   

The error message is:

Main.cpp: In function ‘int main()’:
Main.cpp:18:15: error: request for member ‘find’ in ‘str’, which is of non-class type ‘char [80]’
   int a = str.find(pstr);
               ^
Main.cpp:21:7: error: request for member ‘replace’ in ‘str’, which is of non-class type ‘char [80]’
   str.replace(a,len,change);
       ^

For example,

what is your nam / nam / name

the output is

'what is your name'
what is matter?
YSC
  • 38,212
  • 9
  • 96
  • 149
jinj
  • 9
  • 1
  • 2
    You use arrays for C-style null-terminated byte strings, not [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) C++ objects. Arrays aren't "object" in that way, and therefore doesn't have member functions. It seems you're mixing your C and C++. – Some programmer dude Nov 12 '18 at 09:52
  • Why do you use char[80], then sometimes allocate it, sometimes use the stack, and then use them as objects? Seems like you are not sure of what you are doing. – Matthieu Brucher Nov 12 '18 at 09:54

3 Answers3

4

The statement str.find(pstr); means Call the member function find of the object str with pstr as argument. If you are not sure to completely understand this sentence, I'd suggest you to find a good C++ book.

The thing is, str has type char[80], which is not an object of class-type with an available member function find. It's a C-style array, not suitable for OOP.

What you need is std::string:

#include <string>

std::string str;
/* set str */
auto pos = str.find(pstr);

Here is the doc of std::string::find().

YSC
  • 38,212
  • 9
  • 96
  • 149
0

Use std::string instead of char[N].

The char[80] type is a fundamental type and doesn't have functions.

For the getline, you will need to change it to:

std::getline(std::cin, str);
darune
  • 10,480
  • 2
  • 24
  • 62
-1
int main()
{
   char str[80];
   char find[10];
   char change[10];
   char* pstr;
   char* temp = new char[80];
   cin.getline(str, 80);
   cin.getline(find,10);
   cin.getline(change,10);

   pstr = strstr(str,find);

   std::string newStr = str;
     int a = newStr.find(pstr);
     int len;
     len = strlen(find);
     newStr.replace(a,len,change);

  delete []temp;
   cout<< newStr << endl;
   return 0;
}
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Levon
  • 11
  • 4
  • 11
    Two things: please note the markdown usage of code; you used in-line code formatting for each separate line, as opposed to a block of code (backticks on each row vs 4 spaces in front of each row, check my revision) second: Explain **WHY** this code works. Remember that you're not only answering the OP, but the post also has to be readable for future readers, so please, [edit] the answer to explain in text and/or comments in the code why this code answers the problem. – Adriaan Nov 12 '18 at 10:02