Inputting data from .txt, outputting it to new .txt

Accounting packages and spreadsheets often represent numeric values with commas and values which represent dollar amounts may also have dollar signs, negative monetary values are often represented by enclosing the value in parentheses. If these files are to be used by programs expecting simply numeric values separated by single blank spaces the file needs to be modified to have the dollar signs and commas removed and the parentheses changed to negative values
with a minus sign instead. While this modification can be done by hand, if the files are large or the modification must be done to many files over time it is easier to write a program which accepts the original file and writes another file in the proper format.

You are to design and implement the program to modify the file described above, removing all dollar signs and comma, and converting values enclosed in parentheses to negative values with a minus sign, the parentheses are then removed as well. Where values are separated by tab characters, ‘\t’, or multiple spaces replace the tab character or multiple spaces with a single blank
space in the new file being created. The new file should keep the values on the same line as in the original file so there is no change in the number of lines between the original file and the new file.

That's my goal with this program^

And here is my code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
//Declare objects
char character;
string infile;
infile = "Input.txt";
ifstream fin(infile.c_str());
ofstream fout("HW4Results.txt");
int a,c,d;
double b,e;
if(fin.fail())
{
cerr<<"Error opening input file"<<endl;
exit(1);
}
if(fout.fail())
{
cerr<<"Error opening output file"<<endl;
exit(1);
}
fin>>a>>b>>c>>d>>e;

//Read first character from input file
fin.get(character);

while(!fin.eof())
{
//Convert characters
switch(character)
{
case 1:
if (character = '$')
{
fout<<"$";
}
break;
case 2:
if (character = ',')
{
fout<<" ";
}
break;
case 3:
if (character = '(')
{
fout<<"-";
}
break;
case 4:
if (character = ')')
{
fout<<"";
}
break;
case 5:
if (character = ' ')
{
while((character=fin.peek())!=EOF)
{
fout<<"";
}
fout<<" ";
}
break;
case 6:
if (character = '/t')
{
fout<<" ";
}
break;
}
//Read next character from Input file
fin.get(character);
}

//Print to file
fout.open("HW4Results.txt");
fout<<a<<b<<c<<d<<e<<endl;

//Close files
fin.close();
fout.close();
return 0;

Input text file looks like this:

$40 3.5,17 (81) 6.3




Now, I can't figure out what's wrong with it I get a "Input file is in a fail state" which leads me to believe someone is wrong with my switch statement. I can't figure out what that is however. Any help would be appreciated.
Last edited on
Well, since c is set to 0 before your switch statement, and c is never changed, you will always hit the default statement...other then that, your entire program probably won't work since fstream reads out strings, not ints/doubles. You will need to put data into a string(s) and then turn them into ints/doubles depending on the data. You could also handle the () , etc inside there too.
You will probably want to use stringstreams for that (search for that).
Did a search for stringstreams and tried to read up on them. I sort of understand how they are used, I'm just having trouble applying it to my code now
updated my code after reworking it a bit, seems to run ok but doesn't actually output anything still.
Topic archived. No new replies allowed.