string::replace question

Started by
3 comments, last by Eralp 16 years, 2 months ago
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.
Advertisement
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(), '.', ' ');

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 ?
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.
Ah I didnt realize that algorithm was a header I just thought "this is the replacing algorithm"

Thank you again, it's now working :)

This topic is closed to new replies.

Advertisement