1. Where do I save the ACCOUNT1 & 2?
2. Or do I link them using Dev C++, if yes how?
3. I have compiles but I have one error but no program is excuting.
4. I know the files need to be opened but how? I am using Text Book " Extended Prelude to Programming and the examples or horible. Is there a better book for Pramming and Algorithms & Design?
// The two data files being read are: ACCOUNT1 and ACCOUNT2
// The output data file being written is: ACCOUNT3
//
// Each record in the files is as follows:
// AcctNo Name Balance
// (Int) (String) (Double)
#include // Standard Library - used by system("PAUSE");
#include // I/O stream - used by cin and cout
#include // I/O manipulation - used for formatting
#include // File stream - used by file input and output routines
using namespace std; // used by iostream and fstream
// Create a data structure that identifies how a bank record will look
typedef struct {
int AcctNo;
char Customer[40];
double Balance;
} BankRecord;
///////////////////////////////////////////////////////////////////////
///////////////// Here is the program /////////////////////////////////
///////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
//
// create variables of type BankRecord for use within the program
//
BankRecord DataFromFile1;
BankRecord DataFromFile2;
char buff[80];
//
// create links from the disk into our program and
// make sure that the program can open the files
//
ifstream file1("ACCOUNT1"); // ifstream opens the file for input
if (!file1)
{
cout << "Unable to open ACCOUNT1 for input" << endl;
return 1; // return non-zero indicates an error
}
ifstream file2("ACCOUNT2"); // ifstream opens the file for input
if (!file2)
{
cout << "Unable to open ACCOUNT2 for input" << endl;
return 2; // return non-zero indicates an error
}
ofstream file3("ACCOUNT3"); // ofstream opens the file for output
if (!file3)
{
cout << "Unable to open ACCOUNT3 for ouptut" << endl;
return 2; // return non-zero indicates an error
}
//
// Read the first record of each file
//
file1 >> DataFromFile1.AcctNo >> DataFromFile1.Customer >> DataFromFile1.Balance;
file2 >> DataFromFile2.AcctNo >> DataFromFile2.Customer >> DataFromFile2.Balance;
//
// While not end-of-file on both files, read a record from each file and write
// it to the output file. This code needs to be changed to correctly merge
// the files so that the output file is ordered by account number in
// increasing order. See the pseudo-code in the text book, then convert it
// to correct C++ code using the if-else statements. The rest of the program
// should be coded correctly.
//
while ( !file1.eof() && !file2.eof())
{
// write file1's data to file3 and read another record from file1
sprintf (buff, "%ld %-15.15s %8.2lf", DataFromFile1.AcctNo, DataFromFile1.Customer, DataFromFile1.Balance);
file3 << buff << endl;
file1 >> DataFromFile1.AcctNo >> DataFromFile1.Customer >> DataFromFile1.Balance;
// write file2's data to file3 and read another record from file1
sprintf (buff, "%ld %-15.15s %8.2lf", DataFromFile2.AcctNo, DataFromFile2.Customer, DataFromFile2.Balance);
file3 << buff << endl;
file2 >> DataFromFile2.AcctNo >> DataFromFile2.Customer >> DataFromFile2.Balance;
}
//
// We have finished the data from one of the files, just finish writing out the rest
// of the remaining data. This part of the program does not need to be modified.
//
while (!file1.eof()) // finish writing file1 to file3 if there is anything left
{
// write file1's data to file3 and read another record from file1
sprintf (buff, "%ld %-15.15s %8.2lf", DataFromFile1.AcctNo, DataFromFile1.Customer, DataFromFile1.Balance);
file3 << buff << endl;
file1 >> DataFromFile1.AcctNo >> DataFromFile1.Customer >> DataFromFile1.Balance;
}
while (!file2.eof()) // finish writing file2 to file3 if there is anything left
{
// write file2's data to file3 and read another record from file1
sprintf (buff, "%ld %-15.15s %8.2lf", DataFromFile2.AcctNo, DataFromFile2.Customer, DataFromFile2.Balance);
file3 << buff << endl;
file2 >> DataFromFile2.AcctNo >> DataFromFile2.Customer >> DataFromFile2.Balance;
}
//
// close the files
//
file1.close();
file2.close();
file3.close();
system ("PAUSE"); // "Press any key to continue..."
return 0;
}