Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is supposed to take the account number, name, and balance of someon

ID: 3671142 • Letter: T

Question

This program is supposed to take the account number, name, and balance of someone's account and then add the transaction amount from the account with the matching number as it. When I try and run the program the new master file only has "-858993460 -1.85119e+62" in it and no other information. Any help would be appreciated.

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

void createOldMaster() // Used to process the Old Master File information
{
   ofstream inOldMaster;
   char fname[15]; // Using first and last name char to deal with spaces in names
   char lname[15];
   int mastAccNumber; // Master account number
   double mastBalance; // Master account balance

   inOldMaster.open("oldmast.dat");
   if (!inOldMaster.is_open())
   {
       cout << "Unable to open the oldmaster file.";
       exit(1);
   }
   cout << "Please enter the following details of the personal accounts for the Master File."<<endl;
   while (true)
   {
       cout << " Account Number (enter 0 to end): ";
       cin >> mastAccNumber;
  
       if (mastAccNumber == 0) // Used to exit the createOldMaster function
           break;
       else
       {
           cout << "Name: ";
           cin >> fname>>lname;
           cout << "Balance: ";
           cin >> mastBalance;

           inOldMaster << mastAccNumber << " " << fname<<" "<<lname << " " << mastBalance << endl;
       }
   }
}

void createTransactions() // Used to process the transaction information
{
   ofstream inTransaction;
   int transAccNumber; // Transaction Account Number
   double transBalance; // Amount changed in transactions
   inTransaction.open("trans.dat");

   if (!inTransaction.is_open())
   {
       cout << "Unable to open the transactions file.";
       exit(1);
   }
   cout << "Please enter the following details of the personal accounts for the Transaction File."<<endl;
   while (true)
   {
       cout << " Account Number (enter 0 to end): ";
       cin >> transAccNumber;

       if (transAccNumber == 0) // Used to exit the createTransactions function
           break;
       else
       {
           cout << "Transaction Amount: ";
           cin >> transBalance;
           inTransaction << transAccNumber << " " << transBalance << endl;
       }
   }
}

int main()
{
   ifstream inTransaction, inOldMaster, inNewMaster;
   ofstream outNewMaster;
   outNewMaster.open("newmast.dat"); // Opens up the new master file for info to go in it
   char fname[15];
   char lname[15];
   char ch;
   int mastAccNumber, transAccNumber;
   double mastBalance, transBalance;

   createOldMaster(); // Using the OldMaster function
   createTransactions(); // Using the transactions function

  

   if (!outNewMaster.is_open())
   {
       cout << "Unable to open the new master file.";
       exit(1);
   }
   do
   {
       inOldMaster >> mastAccNumber >> fname>>lname >> mastBalance;
       inTransaction >> transAccNumber >> transBalance;
       if (inOldMaster.bad() || inTransaction.bad()) // Break loop if files are unable to be read
       {
           break;
       }
       if (mastAccNumber == transAccNumber) // If the master account number and the transaction account number match
       {
           mastBalance += transBalance; // Adds transaction amount to the balance
           outNewMaster << mastAccNumber << " " << fname<<lname << " " << mastBalance << endl; // Outputs the new information to the newMaster file
       }
       else if (mastAccNumber != transAccNumber) // In case the master account number and the transaction account numbers don't match
       {
           outNewMaster << mastAccNumber << " " << fname<<lname << " " << mastBalance << endl;

           cout << "Unmatched transaction record for account number " << transAccNumber << endl;
       }

   } while (inOldMaster.good() && inTransaction.good());
   outNewMaster.close();
   while (inNewMaster.get(ch))
       cout << ch;

  
   cout << " Successfully created and copied data to new master file in newmast.dat."<<endl;
   system("pause");
   return 0;
}

Explanation / Answer

// you had not closed the ofstream objects.

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

void createOldMaster() // Used to process the Old Master File information
{
   ofstream inOldMaster;
   char fname[15]; // Using first and last name char to deal with spaces in names
   char lname[15];
   int mastAccNumber; // Master account number
   double mastBalance; // Master account balance
   inOldMaster.open("oldmast.dat");
   if (!inOldMaster.is_open())
   {
       cout << "Unable to open the oldmaster file.";
       exit(1);
   }
   cout << "Please enter the following details of the personal accounts for the Master File."<<endl;
   while (true)
   {
       cout << " Account Number (enter 0 to end): ";
       cin >> mastAccNumber;
  
       if (mastAccNumber == 0) // Used to exit the createOldMaster function
           break;
       else
       {
           cout << "Name: ";
           cin >> fname>>lname;
           cout << "Balance: ";
           cin >> mastBalance;
           inOldMaster << mastAccNumber << " " << fname<<" "<<lname << " " << mastBalance << endl;
       }
   }
   inOldMaster.close();
}
void createTransactions() // Used to process the transaction information
{
   ofstream inTransaction;
   int transAccNumber; // Transaction Account Number
   double transBalance; // Amount changed in transactions
   inTransaction.open("trans.dat");
   if (!inTransaction.is_open())
   {
       cout << "Unable to open the transactions file.";
       exit(1);
   }
   cout << "Please enter the following details of the personal accounts for the Transaction File."<<endl;
   while (true)
   {
       cout << " Account Number (enter 0 to end): ";
       cin >> transAccNumber;
       if (transAccNumber == 0) // Used to exit the createTransactions function
           break;
       else
       {
           cout << "Transaction Amount: ";
           cin >> transBalance;
           inTransaction << transAccNumber << " " << transBalance << endl;
       }
   }
   inTransaction.close();
}
int main()
{
   ifstream inTransaction, inOldMaster, inNewMaster;
   ofstream outNewMaster;
   outNewMaster.open("newmast.dat"); // Opens up the new master file for info to go in it
   char fname[15];
   char lname[15];
   char ch;
   int mastAccNumber, transAccNumber;
   double mastBalance, transBalance;
   createOldMaster(); // Using the OldMaster function
   createTransactions(); // Using the transactions function
  
   if (!outNewMaster.is_open())
   {
       cout << "Unable to open the new master file.";
       exit(1);
   }
   do
   {
       inOldMaster >> mastAccNumber >> fname>>lname >> mastBalance;
       inTransaction >> transAccNumber >> transBalance;
       if (inOldMaster.bad() || inTransaction.bad()) // Break loop if files are unable to be read
       {
           break;
       }
       if (mastAccNumber == transAccNumber) // If the master account number and the transaction account number match
       {
           mastBalance += transBalance; // Adds transaction amount to the balance
           outNewMaster << mastAccNumber << " " << fname<<lname << " " << mastBalance << endl; // Outputs the new information to the newMaster file
       }
       else if (mastAccNumber != transAccNumber) // In case the master account number and the transaction account numbers don't match
       {
           outNewMaster << mastAccNumber << " " << fname<<lname << " " << mastBalance << endl;
           cout << "Unmatched transaction record for account number " << transAccNumber << endl;
       }
   } while (inOldMaster.good() && inTransaction.good());
   outNewMaster.close();
   while (inNewMaster.get(ch))
       cout << ch;
  
   cout << " Successfully created and copied data to new master file in newmast.dat."<<endl;
   system("pause");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote