0

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() {
//
//}
Arturo
  • 1
  • 3
  • It's unrelated but you should read [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). `using namespace std;` is very bad practice but to use it in a header file is even worse. – Thomas Sablik Sep 21 '19 at 23:06
  • Thank you for that. I changed it in my code. :) – Arturo Sep 21 '19 at 23:09
  • 2
    Your constructor never sets `fname` and `lname`. – David G Sep 21 '19 at 23:11
  • 1
    You should enable and read the warnings. My compiler says: `prog.cc:45:33: warning: unused parameter 'fname' [-Wunused-parameter]` and `prog.cc:45:47: warning: unused parameter 'lname' [-Wunused-parameter]`. – Thomas Sablik Sep 21 '19 at 23:14
  • @ThomasSablik I will enable them. I usually get warnings but this is a new Visual Studio. – Arturo Sep 21 '19 at 23:22
  • The warnings would have shown you the problem with exact position in code and variable names. – Thomas Sablik Sep 21 '19 at 23:27

1 Answers1

0

You need to set fname and lname in your constructor and your toString method is displaying twice fname instead of fname and lname.

BankAccount::BankAccount(string fname, string lname, int accountID, int accountNumber, double accountBalance) {
    this->fname = fname;
    this->lname = lname;
    this->accountBalance = accountBalance;
    this->accountID = accountID;
    this->accountNumber = accountNumber;
    this->accountName = accountName;

};
string BankAccount::toString() {

    return "Account Name: " + fname +" "+ lname +"\n" + "Account Number: " + to_string(accountNumber) + "\n" + "Account Balance: " + to_string(accountBalance) + "\n\n";

}