Find and replace

Pages: 12
Mar 2, 2012 at 12:07pm
Hi im working on a program that lets the user input whatever they want and then the program finds the instance of a certain word, in this case "shit" and replaces it with crap, but its not working how i want, what am i doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

using namespace std;


int main()
{
    string input;

    cin >> input;

    input.find("shit");
    input.replace("crap");

    cout << input << endl;
}

I get these errors:

C:\Users\Chay Hawk\Desktop\C++ Testing\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\C++ Testing\main.cpp|14|error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::replace(const char [5])'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1256|note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1278|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.tcc|391|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1321|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1344|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1362|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1380|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1401|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1422|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, typename _Alloc::rebind<_CharT>::other::size_type, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1458|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, _CharT*, _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1468|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, const _CharT*, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1479|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_i|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1489|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::const_pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__no|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1513|note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, std::initializer_list<_CharT>) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
||=== Build finished: 1 errors, 0 warnings ===|
Mar 2, 2012 at 1:08pm
The compiler reports that he can not find replace function with one parameter of type const char *.

I think you should also specify what will be replaced as argument to the function.
Mar 2, 2012 at 1:33pm
I understand that but what im asking is how do i fix this, what specifically do i need to do? I already know how to do it if say my string was string input = "this is shit"; I could find and replavce that word but i want to know how to find a word from whatever the user typed in and replace it.
Mar 2, 2012 at 3:34pm
bump
Mar 2, 2012 at 3:36pm
Last edited on Mar 2, 2012 at 3:37pm
Mar 2, 2012 at 4:26pm
Ok, that didnt help me much. I know what to do basically, but the replace functon needs to know where the word starts and it has to be able to find the instance of that word which is unknown s how does that work?
Mar 2, 2012 at 4:46pm
If the word it's looking for is unknown, it's impossible.

If the word it's looking for is provided by an outside agency, such as a user, then start by fetching that word from the user.
Mar 2, 2012 at 4:50pm
Yes the program is being designed so that if the user enters a certain word, such as shit, then it will replace it as **** when the user presses enter. Hre is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;


int main()
{
    string input;

    cin >> input;
    cout << input << endl;

    input.find("shit");
    input.replace(, 4, "****");

    cout << input << endl;
}
Mar 2, 2012 at 4:53pm
Yet you appear top have ignored the user's input in your find call.
Mar 2, 2012 at 8:07pm
Like this input.find(cin, "shit");
or input.find(input, "shit");?

Still get errors? what do you mean by ignored the users input?
Mar 3, 2012 at 11:32am
bump
Mar 3, 2012 at 12:38pm
Ignore that, I misread.
Mar 3, 2012 at 1:14pm
Ok so what do i do?
Mar 3, 2012 at 1:46pm
http://cplusplus.com/reference/string/string/replace/

Do something like this:
input.replace(input.find("shit"),4,"crap");
Mar 3, 2012 at 1:50pm
See if you can figure this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    string input;

    getline (cin, input);

    string wordToFind("shit");
    string newWord ("crap");
    
    size_t pos;

    pos = input.find(wordToFind);

    if (pos == string::npos)
    {
        cout << "** Error - Search string not found in input **" << endl;

    }
    else
    {

        input.replace(input.begin()+pos, input.begin()+ pos+ wordToFind.size(), newWord) ;
        cout << input << endl;
    }
Mar 3, 2012 at 1:58pm
Ok sweet it works... sorta. It runs and everything and when i type in something and press enter the console crashes and says it needs to close. Why is it doing that? i get no compiler errors or anything?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;


int main()
{
    string input;

    cin >> input;

    input.find("shit");
    input.replace(input.find("shit"),4,"****");

    cout << input << endl;
}
Mar 3, 2012 at 2:06pm
What are we talking about now - are we takling about my post or your post??
Mar 3, 2012 at 2:23pm
Mine, i didnt see yours when i posted. yours works, but i sorta dont understand some of, it, i'll have to take a better look at it. There isnt any way to get my simpler version to work??
Last edited on Mar 3, 2012 at 2:25pm
Mar 3, 2012 at 4:40pm
input = input.replace(input.find("shit"), 4, "****"); replace returns a reference.
Mar 3, 2012 at 5:08pm
It still crashes. It says:
'This aplication has requested the Runtime to terminate it in an unusual way. Please contact the applications support team for more information."
Pages: 12