mercredi 2 octobre 2019

Why is acccountBalance showing zeros after the decimal?

I have a program that displays a text file in a format using the tostring method. I understand tostring is six characters or digits after the decimal so I had to create ostringstream to use with accountBalance. However now the accountBalance is showing only zeros. Apart from that the program gathers the count of the vector and then erases the duplicates and inserts other accounts. Why is this behavior happening?

main.cpp
#include <iostream>
#include <vector>
#include <iomanip>
#include "math.h"
#include <cmath>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdio>
#include "BankAccount.h"

using namespace std;
bool removeDuplicates(vector<BankAccount> &newBank);
int largest(vector<BankAccount> newBank);
int smallest(vector<BankAccount> newBank);

int main (){
  vector<BankAccount> newBank;
  string line;
  int count = 0;
  string fname, lname;
  int accountID;
  int accountNumber;
  int accountBalance;


  //Open file
  ifstream inputFile;
  inputFile.open("BankData.dat");
  while(getline(inputFile, line) && count < 9){
    stringstream instream(line);
    instream>>fname>>lname>>accountID>>accountNumber>>accountBalance;
    BankAccount bankVector(fname, lname, accountID, accountNumber, accountBalance);
    newBank.push_back(bankVector);
  }

  cout<<"FAVORITE BANK - CUSTOMER DETAILS " << endl;
  cout<<"--------------------------------"<<endl;

  //Print vector
  for(unsigned i = 0; i < newBank.size(); i++){
    cout<<newBank[i].toString();
  }

  cout << "Largest Balance: "<<endl;
    cout << newBank[largest(newBank)].toString();

    cout << "Smallest Balance :"<<endl;
    cout << newBank[smallest(newBank)].toString();

  cout << "Using the static count, there are "<<BankAccount::count<<" accounts"<<endl;

  cout << "Using vector size, there are "<<newBank.size()<<" accounts"<<endl;


  if(removeDuplicates(newBank)){
    for(unsigned i =0 ; i< newBank.size(); i++){
      i++;
    }
  }
cout<<endl;
  cout << "Duplicate Accounts Found : Reprinting List"<<endl;
  cout<<endl;

  cout<<"FAVORITE BANK - CUSTOMER DETAILS"<<endl;
  cout<<"--------------------------------"<<endl;

  for(unsigned i = 0; i < newBank.size(); i++){
    cout<<newBank[i].toString();
  }



  cout<<"Using the static count, there are "<<BankAccount::count<<" accounts"<<endl;
  cout<<"Using vector size, there are "<<newBank.size()<<" accounts"<<endl;
cout<<endl;
  cout<<"Inserted Three New Accounts: Reprinting List " <<endl;
cout<<endl;
  BankAccount accountOne("Amy", "Machado", 387623,  1244,1023.67);
  BankAccount accountTwo("Tak", "Phen",  981243, 1262, 6423.03);
  BankAccount accountThree("Celia"," Beatle", 465281, 1276, 3.56   );
  newBank.insert(newBank.begin() + 2, accountOne);
   newBank.insert(newBank.begin() + 4, accountTwo);
newBank.insert(newBank.begin() + 6, accountThree);
cout<<"FAVORITE BANK - CUSTOMER DETAILS"<<endl;
cout<<"--------------------------------"<<endl;
for(unsigned i = 0; i < newBank.size(); i++){
    cout<<newBank[i].toString();
  }


}


int largest(vector<BankAccount> newBank) {

    double maxBalance = 0;
    double curBalance;
    int index = 0;
    for (int i = 0; i < 8; i++) {
        curBalance = newBank[i].getAccountBalance();
        if (curBalance > maxBalance) {
            maxBalance = curBalance;
            index = i;
        }
    }
    return index;
}

int smallest(vector<BankAccount> newBank) {

    double minBalance = newBank[0].getAccountBalance();
    double curBalance;
    int index = 0;
    for (int i = 0; i < 8; i++) {
        curBalance = newBank[i].getAccountBalance();
        if (curBalance < minBalance) {
            minBalance = curBalance;
            index = i;
        }
    }
    return index;
}

bool removeDuplicates(vector<BankAccount> &newBank){
   bool duplicate = false;

       for(unsigned int i=0;i<newBank.size()-1;i++)

       {

             for(unsigned int j=i+1;j<newBank.size();j++)

                    if(newBank[i].equals(newBank[j]))

                    {

                           duplicate = true;

                           newBank.erase(newBank.begin()+j);

                    }

       }

       return duplicate;
}
Circle.cpp
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>
#include "BankAccount.h"
using namespace std;

double minBalance = 9.99;
double rewardsAmount = 1000.00;
double rewardsRate = 0.04;
int BankAccount::count = 0;

BankAccount::BankAccount() {

    accountName = "";
    accountBalance = 0;
    accountNumber = 0;
    accountID = 0;
    fname = "";
    lname = "";
  count++;
};

BankAccount::BankAccount(string fname, string lname, int accountID, int accountNumber, double accountBalance) {

    this->accountBalance = accountBalance;
    this->accountID = accountID;
    this->accountNumber = accountNumber;
    //this->accountName = accountName;
    this->fname = fname;
    this->lname = lname;
  count++;

};

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() {
  cout<<fixed<<setprecision(2);
  ostringstream ss;
  ss<<fixed<<setprecision(2);
  ss<<accountBalance;

return "Account Name: "+fname+" "+lname+"\n"+"Account Number: "+to_string(accountNumber)+"\n"+ "Account Balance: " +ss.str()+"\n\n";
}


int BankAccount::getID() {
    return accountID;

}


bool BankAccount::equals(BankAccount other) {
    /*if (other.getAccountNumber() == accountNumber){
        BankAccount account("XXXX  XXXX","", 0, 0, 0);
        return account;
    }
    else{
        return other;
    }


       return(getID() == other.getID());
  }*/
 return(getID() == other.getID());

}
circle.h
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include "math.h"

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);
    bool equals(BankAccount other);
  static int count;

    bool withdraw(double amount);
    void deposit(double amount);
    string toString();

};

Aucun commentaire:

Enregistrer un commentaire