Dont know how to extract the data from file using class constructors

The txt File is like this
ABC0001 deposit 200
ABC0001 deposit 50
ABC0002 deposit 300
ABC0003 deposit 150
ABC0001 deposit 100
ABC0002 withdraw 200
ABC0003 deposit 100
ABC0001 withdraw 75
ABC0004 deposit 1000
ABC0001 withdraw 75
ABC0003 withdraw 1000
ABC0003 withdraw 100
ABC0001 close
ABC0002 close
ABC0003 close
ABC0002 deposit 25

It would be awesome if anyone can help

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

// ======================
// Global Declarations
// ======================
const int Num_Accounts = 16;
class BankAccount
{
private:


string name;
string accountNum;
double balance;

bool accountIsOpen;
public:
BankAccount()
{
name = "";
accountNum = "xxxxxxxx";
balance = 0.00;
accountIsOpen = false;
}

// Initialization constructor (includes parameters)
BankAccount(string p_name, string p_accountNum, double p_balance)
{
name = p_name;
accountNum = p_accountNum;
balance = p_balance;
accountIsOpen = true;
}

// "Getter" (Accessor) methods

string getName() const
{
return name;
}

string getAccountNumber() const
{
return accountNum;
}

double getBalance() const
{
return balance;
}

// "Setter" (Modifier) methods
void setName(string p_newName)
{
name = p_newName;
}

// General Methods

double deposit(double p_amount)
{
if (p_amount > 0)
balance = balance + p_amount;
return balance;
}

double withdraw(double p_amount)
{
if (p_amount > 0 && p_amount <= balance)
balance = balance - p_amount;
return balance;
}


bool IsOpen()const
{
return accountIsOpen;
}

void displayBankAccount()
{
cout << "Name: " << name <<endl<< "Account Number: " << accountNum << endl<< "Opening Balance: " << balance<<endl<<endl;
}

void close()
{
accountIsOpen = false;
}



};

// ======================
// Main Function
// ======================
int main()
{
BankAccount Customer1("Anne Jowsey", "ABC0001", 200);
BankAccount Customer2("Salvatore Sciandra", "ABC0002", 500);
BankAccount Customer3("Debra Sorrentino", "ABC0003", 1000);

BankAccount Accounts[3];
Accounts[0] = Customer1;
Accounts[1] = Customer2;
Accounts[2] = Customer3;
for (int i = 0; i < 3; i++)
{
cout << Accounts[i].getName()<<" "<<Accounts[i].getAccountNumber()<<" "<<Accounts[i].getBalance()<<endl;
}

// Open transactions.txt (assumed to be in project folder)
// Validate that the file opened successfully
// Use an EOF-controlled loop to process all transactions in the file
// For each record,
// Read the input record from the file: account number, transaction type, and amount (if appropriate)
// Search the array for the specified account (reject invalid account numbers)
// If found, perform the indicated transaction on the account (make sure the account is open)
// Display the requested transaction to the screen (inlucing error messages such as invalid account, or illegal transaction)
// Display the updated bank account after each transaction
// end loop
// Close the input file and exit
ifstream Infile;
Infile.open("transactions.txt");
while(!Infile.eof())
{

}


system("pause");
return 0;
}


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <vector>
#include <fstream>

struct transaction
{
    std::string account_number ;
    std::string type ; // an enum would be better
    double amount ;
};

// http://www.mochima.com/tutorials/vectors.html
std::vector<transaction> read_transactions( const std::string& file_name )
{
    std::vector<transaction> result ;

    std::ifstream file(file_name) ; // open the file

    transaction trans ;
    while( file >> trans.account_number >> trans.type >> trans.amount )
        result.push_back(trans) ;

    return result ;
}
Topic archived. No new replies allowed.