Find and replace

Pages: 12
Mar 3, 2012 at 5:34pm
Replace cin >> input; with getline(cin,input);
Mar 3, 2012 at 5:47pm
still gives me that same error.
Mar 3, 2012 at 6:57pm
This works fine for me with the string "this is a test string"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;


int main()
{
    string input;

    getline(cin, input);
    cout << input << endl;

    input = input.replace(input.find("test"), 4, "****");

    cout << input << endl;
}
Mar 3, 2012 at 8:33pm
ok it works when i type that in but what if the user just types fnsdjkfndskfnsfsjkd or just whatever they want, then it crashes, how do i avoid crashes? I was thinking something like this but i got an error:

1
2
3
if(input.find != "test"){
        return 0;
    }


error:

C:\Users\Chay Hawk\Desktop\C++ Testing\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\C++ Testing\main.cpp|16|warning: comparison with string literal results in unspecified behaviour|
C:\Users\Chay Hawk\Desktop\C++ Testing\main.cpp|16|error: invalid operands of types '<unresolved overloaded function type>' and 'const char [5]' to binary 'operator!='|
||=== Build finished: 1 errors, 1 warnings ===|
Last edited on Mar 3, 2012 at 8:35pm
Mar 3, 2012 at 8:59pm
Now you know why I put this bit in the code I posted.


1
2
3
4
5
6
7
8
9
size_t pos;

    pos = input.find(wordToFind);

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

    }


the find function returns a value string::npos when it does NOT find the substring
you were searching for.
You should test for this condition BEFORE attempting to make the substitition.
Mar 3, 2012 at 9:27pm
Ok, i did this and it worked when i just typed in random crap:

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

using namespace std;


int main()
{
    string input;

    getline(cin, input);
    cout << input << endl;

    if(input != "test"){
        return 0;
    }

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

    cout << input << endl;
}


So yeah, that worked. I like your way too, what is size_t though? ive never heard of that function before??
Mar 3, 2012 at 9:39pm
size_t is not a function.
it is a typedef for unsigned int
Mar 3, 2012 at 9:42pm
So what does size_t actually do?
Mar 3, 2012 at 10:44pm
Also what if i have like 10 or 15 words that i want found and changed?? i cant use a switch statement with strings and i dont really like nesting too many if statements, so what can i do??
Mar 4, 2012 at 12:02pm
bump
Mar 4, 2012 at 2:01pm
I wouldn't worry too much about changing 10 or 15 words since you still haven't managed to get it to change one reliably.

The only way to reliably replace a substring using find and replace is to check the return value of find to see if the substring was found. Limiting your input to the substring you want to replace really isn't an option.
Topic archived. No new replies allowed.
Pages: 12