# string::replace question

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I want to make a decryption program and therefore I need to read the ascii values from the input file.But there are .'s between the numbers(ie 95.51.27 ) I want to replace them with ''(also nothing) how can I do it ? Here is my code that doesn't work
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

int a = 'd';
string sifre = "";
string fincik = "";

ofstream fout ("pw.out");
ifstream fin ("pw.in");

if(!fin.is_open())
return 0;

while(!fin.eof())
{
fin >> fincik;
sifre += fincik;
}

sifre.replace(sifre.begin(),sifre.end(),'.',' ');
fout << sifre <<endl;

return 0;
}


I dont understand why there are so many string::replace functions and why is it so hard in c++. It was very easy in other languages I used to program.

##### Share on other sites
There are many replace functions becuase there are many different ways to replace something in a string. A quick check in some documentation or reference about the string class should reveal that there are no replace function like the one you want to call. All replace functions, as far as I could see, is about replacing a substring with another, and the different overloads represents different ways to specify the substrings.

You want the replace algorithm, which replaces elements in a container with other elements. It's in the algorithm header.
std::replace(sifre.begin(), sifre.end(), '.', ' ');

##### Share on other sites
Thank you :)

But why I get these errors ?
error C2039: 'replace' : is not a member of 'std'
error C2065: 'replace' : undeclared identifier

Is it because I'm using old version of visual c++?(6.0)
Or any header files are missing ?

##### Share on other sites
I said what header you need. Also, change compiler to something that at least was released AFTER the C++ standard was released. Later versions of Visual Studio are free, so just download it from Microsoft.

##### Share on other sites
Ah I didnt realize that algorithm was a header I just thought "this is the replacing algorithm"

Thank you again, it's now working :)

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999977
×