I need to access a file and then display the content but for some reason I cannot display the full name. I made the variables fname
and lname
for the first and last name. For now I just wanted to read the first line correctly. I've gone over a lot of help material but have not understood how to figure it out. A push in the right direction or reading material would be awesome. Also anything that you think I could do better. Below is my code.
**Dat file**
Matilda Patel 453456 1232 -4.00
Fernando Diaz 323468 1234 250.0
Vai vu 432657 1240 987.56
Howard Chen 234129 1236 194.56
Vai vu 432657 1240 -888987.56
Sugata Misra 987654 1238 10004.8
Fernando Diaz 323468 1234 8474.0
Lily Zhaou 786534 1242 001.98
**main.cpp**
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include "BankAccount.h"
using namespace std;
const int SIZE = 8;
int main()
{
string accountName;
string fname;
string lname;
int accountID;
int accountNumber;
double accountBalance;
BankAccount accountArray[SIZE];
string line;
int i = 0;
int number = 0;
ifstream inputFile;
//istringstream ss(line);
inputFile.open("BankData.dat");
getline(inputFile, line);
istringstream instream(line);
instream >> fname >> lname >> accountID >> accountNumber >> accountBalance;
BankAccount buddy(fname, lname, accountID, accountNumber, accountBalance);
cout << buddy.toString() << endl;
}
**BankAccount.h**
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
class BankAccount {
private:
string fname, lname;
string accountName;
int accountID;
int accountNumber;
double accountBalance;
int getID();
void addReward(double amount);
public:
//Default constructor
BankAccount();
//Regular constructor
BankAccount(string fname, string lname, int accountID, int accountNumber, double accountBalance);
double getAccountBalance();
string getAccountName();
int getAccountNumber();
void setAccoutBalance(double amount);
//BankAccount equals(BankAccount other);
bool withdraw(double amount);
void deposit(double amount);
string toString();
};
BankAccount.cpp
#include <iostream>
#include <sstream>
#include "BankAccount.h"
using namespace std;
double minBalance = 9.99;
double rewardsAmount = 1000.00;
double rewardsRate = 0.04;
BankAccount::BankAccount() {
accountName = "";
accountBalance = 0;
accountNumber = 0;
accountID = 0;
};
BankAccount::BankAccount(string fname, string lname, int accountID, int accountNumber, double accountBalance) {
this->accountBalance = accountBalance;
this->accountID = accountID;
this->accountNumber = accountNumber;
this->accountName = accountName;
};
double BankAccount::getAccountBalance() {
return accountBalance;
}
string BankAccount::getAccountName() {
return accountName;
}
int BankAccount::getAccountNumber() {
return accountNumber;
}
void BankAccount::setAccoutBalance(double amount) {
amount += accountBalance;
}
bool BankAccount::withdraw(double amount) {
if (accountBalance - amount <= minBalance) {
return false;
}
else {
accountBalance -= amount;
return true;
}
}
void BankAccount::deposit(double amount) {
accountBalance += amount;
if (amount > rewardsAmount) {
addReward(amount);
}
}
void BankAccount::addReward(double amount) {
accountBalance += (rewardsRate * amount);
}
string BankAccount::toString() {
return "Account Name: " + fname +" "+ fname +"\n" + "Account Number: " + to_string(accountNumber) + "\n" + "Account Balance: " + to_string(accountBalance) + "\n\n";
}
int BankAccount::getID() {
return accountID;
}
//BankAccount BankAccount::equals() {
//
//}