Guys, I've been stuck on this for a while now. This is actually one of my assignments, and I'm really lost. This is the assignment:
-------------------------------------------------------------------------------------------
You manage a .txt file containing the customer ID number and account balance of the members of a bank. Last week, a computing error caused each customer's account balance to get reversed. For example, if someone had an account balance of 132.64, this was reversed to become: 462.31. Write a program which will input the file and output a file containing the corrected account balances next to each customer ID number (a 9 digit number with no spaces).
Each line of the .txt file is of the form:
ID# Balance
ADDITIONAL REQUIREMENTS: Allow the user to specify both the input and output filenames in each problem. Program should perform the required checks for input and output files.
-------------------------------------------------------------------------------------------
Here is my code so far:
#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib> //to use exit(1)
using namespace std;
int main()
{
ifstream inFile, inSortFile;
ofstream outFile;
ifstream checkFile;
string inFileName, outFileName, ID, balance;
do
{
cout << "What is the name of the file you would like to read from? ";
cin >> inFileName;
inFile.open(inFileName.c_str());
if(inFile.fail()) cout << "The file called " << inFileName << " does not exist." << endl;
}while(inFile.fail());
cout << "The file was opened." << endl;
cout << "Where would you like to output the data: ";
cin >> outFileName;
checkFile.open(outFileName.c_str());
if(!checkFile.fail())
{ //opening outer if
cout << "A file with the name " << outFileName << " already exists." << endl
<< "Do you wish to overwrite the file? Enter n or N if no, anything else if yes: ";
char answer;
cin >> answer;
if(answer=='n'||answer=='N')
{ //opening inner if
cout << outFileName << " will not be overwritten.\n";
system("PAUSE");
exit(1);
} //closing inner if
} //closing outer if
checkFile.close();
outFile.open(outFileName.c_str());
cout << "The data was successfully output to " << outFileName << "." <<endl;
int count = 0;
while(inFile.good())
{ //opening while
inFile >> ID >> balance;
outFile << ID << " " << balance << endl;
} //closing while
system("pause");
return 0;
}
Right now pretty much all the code does is just copy the old file to the new one. But I want it to copy the first part of each line - ID#, but then reverse the other number, well what the assignment is asking me to do. I tried to store it as an array and then reverse it, but it's not working. That approach is probably wrong anyway because if it's a decimal, the point won't be put in the right place - 41.2 would become 2.14 instead of 21.4. PLEASE GUYS HELP! Any idea how to accomplish the task? Anything is appreciated, nothing too crazy though. I want to understand this and I'm still a beginner.