in C Write a program that reads a file containing data value
(in C++)
Write a program that reads a file containing data values computed by an accounting software package. While the file contains only numerical information, the values may contain embedded commas and dollar signs, as in $3,200, and negative values are enclosed in parentheses as in (200.56). Write a program to generate a new file that contains the values with the commas and dollar signs removed, and with a leading minus sign instead of the parentheses. Do not change the number of values per line in the file.
I came up with this:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int input=1;
ifstream infile(\"d:\\\\input.txt\");
if (infile.fail())
{
cerr << \"error opening input.txt\";
exit(-1);
}
ofstream output;
output.open(\"d:\\\\output.txt\");
if (output.fail())
{
cerr << \"error opening output.txt\";
exit(-1);
}
while (!infile.eof())
{
char COMMA = \',\';
char CLOSING_P = \')\';
char OPENING_P = \'(\';
char DOLLAR = \'$\';
char NEGATIVE = \'=\';
switch (input)
{
case\',\':
output << \' \';
break;
case \')\':
output << \' \';
break;
case\'(\':
output << \'-\';
break;
case\'$\':
output << \' \';
break;
default:
output << input;
}
}
return 0;
}
Its definitely not working as my output folder only returns an endless amoung of 1\'s. If you could please modify my code here to make it operate correctly and add some comments to let me know where I\'ve gone wrong it would be greatly appreciated.
Solution
Hope this helps you any issues please comment
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
using namespace std;
const char NEWLINE = \'\ \';
const char TAB = \'\\t\';
int main()
{
/* Declare variables. */
char c;
string inname, outname;
ifstream oldfile;
ofstream newfile;
// Prompt user for file name.
cout << \"enter name of input file \";
cin >> inname;
// Open the files for reading and writing respectively.
oldfile.open(inname.c_str());
if(oldfile.fail())
{
cerr << \"error opening file \" << inname << endl;
exit(1);
}
outname = \"new\" + inname;
newfile.open(outname.c_str());
/* Now change the format. */
oldfile.get(c);
while (!oldfile.eof())
{
switch (c)
{
case \')\':
case \'$\':
case \',\':
break;
case \'(\': c = \'-\';
/* fall thru */
default: newfile << c;
break;
}
oldfile.get(c);
}
/* Close files and exit program. */
newfile.close();
oldfile.close();
return 0;
}

