Removing commas from an input file before rewriting them to an output file.


Hello, Working on a hw assignment due in a couple days, program must read in integers from a file that may or may not have embedded commas in them, i.e 10 or 12,000. The program needs to grab the number from one file and create an output file with all the numbers with all the commas removed. for example, still giving 10 but also giving 12000. this is what I have so far, I'm still unclear about the while statement, also should I not be using the end of file function? is there an easier way to do that?
Thanks in advance.



#include<iostream>
#include <fstream>
#include <algorithm>
#include <string>

using namespace std;
int main()
{
double strContent, str1;
int n,s;

ifstream fin;//declare input stream
ofstream Dataout4; //declare output stream
string filename;

fin.open("F:\Datain4.txt");
Dataout4.open("F:\Dataout4.txt"); //output file
fin >> str1;
while (!fin.eof());
{
str1.erase(std::remove(str1.begin(), str1.end(), ','), str1.end());



}
fin.close();
Dataout4.close();
return 0;
} //end main
Last edited on
There are lots of ways to remove chars from a string, this is but one.
This has the added benefit to to print only numbers.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include <fstream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string num="12,000";
string newnum="";

for (int i =0; i < num.size(); i++)
{
if (isdigit(num[i])){newnum+=num[i];}
}
cout << newnum;

return 0;
}
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/istream/istream/ignore/
Topic archived. No new replies allowed.