Sign in to follow this  
Eralp

string::replace question

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this