Jump to content

Bank software, mess with pointers in and outside methods

it's very childish prototype for my homework in university. I need to create a software that will be able to deposit and withdraw some amount of money (my exercise has much more to do but only here i cannot help myself). What is my problem, when i had all objects alloceted normaly(not in vector) i had no problems, but after some time i decided to store my objects in a bucket(vector called accounts).I adjusted my code (this is 1/3 of it but rest works perfectly), and now i don't now why im getting weird numbers, like sometimes method does not have access to some varibles.

 

 

 

 


  #include <iostream>
    #include "md5.h"
    #include "md5.cpp"
    #include <string>
    #include <cstdlib>
    #include <ctime>
    #include <iomanip>
    #include <cstdio>
    #include <sstream>
    #include <vector>
    using namespace std;
    void clear(){ // buffor cleaning
    cin.clear();
    fflush(stdin);
    }
    string capitalize (string s){ 
    bool cap = true;
        for(unsigned int i = 0; i <= s.length(); i++){
            if (isalpha(s[i]) && cap == true){
                s[i] = toupper(s[i]);
                cap = false;
            }else if (isspace(s[i])){  
                cap = true;
            }
        }
        return s;
    }
    string validate(string label, int mode=1, int length=3){
    string varible,year,month,day,tmp;
    tmp.resize(length);
    cout<<label<<": ";
    int j=0,a=0,b=0;
    if(mode!=3)getline(cin,varible);
    switch(mode){ 
    case 1:// multitool for validating input, here is validated text
    if(varible.length()<length) {
    cout<<"Prosze podac minimum "<<length;
    if(length>=2&&length<=4) cout<<" litery"<<endl;
    else cout<<" liter"<<endl;
    varible = validate(label,mode,length);
    }
    break;
    case 2://here numers like PIN
    for(int i = 0; i<varible.length(); i++){
    if(isdigit(varible[i])){
    tmp[j] = varible[i];
    j++;
    }
    }
    varible = tmp;
    if(j<length) {
    cout<<"Prosze podac "<<length<<" cyfry"<<endl;
    varible = validate(label,mode,length);
    }
    break;
    case 3://date
    int y,m,d;
    cout<<endl;
    cout<<"  Rok (RRRR): ";
    cin>>year;
    cout<<"  Miesiac (MM): ";
    cin>>month;
    cout<<"  Dzien (DD): ";
    cin>>day;
    y = atoi(year.c_str());
    m = atoi(month.c_str());
    d = atoi(day.c_str());
    varible = year+"-"+month+"-"+day;
    if(!(y>=1900 & &d>=0 && d<=31 && m>=0 && m<=12)){
    cout<<"Podano nieprawidlowe dane, sprobuj ponownie"<<endl;
    varible = validate(label,mode,length);
    }
    break;
    }
    clear();
    return capitalize(varible);
    }
   
   
    class account{
    private:
    string city,address,birth,country,PIN;
    double balance;
    public:
    static vector <account> accounts;
    static int count;
    account &self = *this; // w c++ nie znalazlem obiektu self a jedynie this->self., problem jest taki ze nie moge wykonac operacji np this->self.+=100 a self+=100 juz tak
    // zmiena klasy - do zliczania
    string acc_number,firstname,lastname;
    account(string firstname, string lastname, string city, string address, string birth, string country, string PIN,int mode = 0){
    count++;
    setLastname(lastname);
    setFirstname(firstname);
    setCity(city);
    setAddress(address);
    setBirth(birth);
    setCountry(country);
    setPIN(PIN);
    setAcc_number();
    setBalance(0);
    }
    //getters
    string getFirstname(){return firstname;}
    string getLastname(){return lastname;}
    string getCity(){return city;}
    string getAddress(){return address;}
    string getBirth(){return birth;}
    string getCountry(){return country;}
    string getPIN(){return PIN;}
    string getAcc_number(){return acc_number;}
    double getBalance(){return balance;}
   
    //setters
    void setFirstname(string firstname){self.firstname = firstname;}
    void setLastname(string lastname){self.lastname = lastname;}
    void setCity(string city){self.city = city;}
    void setAddress(string address){self.address = address;}
    void setBirth(string birth){self.birth = birth;}
    void setCountry(string country){self.country = country;}
    void setPIN(string PIN){self.PIN = md5(PIN);}
    void setBalance(double balance){self.balance = balance;}
    void setAcc_number(){
    double tmp=0;
    string tmp2="";
    for(int i=0;i<1;i++){
    tmp = rand()%99+100;
    tmp2 = tmp2 + to_string(tmp);
    tmp2.erase(tmp2.length()-7);
    
    }
    int roznica = 0;
    roznica = 3-tmp2.length();
    if(tmp2.length()<3){
    for(int j = 0;j<roznica;j++){
    tmp2.insert(tmp2.length(),"0");
    }
    }else if(tmp2.length()>3){
    tmp2.erase(tmp2.length()+roznica);
    }
    self.acc_number = tmp2;
    }
    
    bool auth(string PIN){ // is PIN correct
    if(getPIN() == md5(PIN)) return true;
    else return false;
    }
   
    bool deposit(double toDeposit, string PIN){
    if(auth(PIN)){
    setBalance(this->self.getBalance()+toDeposit);
    return true;
    }else return false;
    }
   
    string printBalance(){ // it formats my balance, it outputs money with spaces every three digits
    if((this->self.getBalance())>=1000){
    int space=0;
    string tmp = to_string(this->self.getBalance());
    tmp.erase(tmp.length()-4);
    for(int i = 0;i<tmp.length()-3;i++) {
    if(!(i%3) && i!=0)space++;
    }
    string tmp2 = tmp;
    tmp2.erase(tmp2.length()-3);
    tmp2.resize(tmp2.length()+space);
    int licznik;
    if(space%2)licznik=0;
    else licznik =-1;
    for(int j = tmp2.length()-1;j>0;j--){
    if(!(licznik%3) && isdigit(tmp2[j]) && licznik!=0 && j!=tmp2.length()-1){
    for(int c=tmp2.length()-1;c>j;c--){
    tmp2[c]=tmp2[c-1];
    }
    tmp2[j]=' ';
    }
    licznik++;
    }
    tmp.erase(0,tmp.length()-3);
    tmp2 = tmp2+tmp;
    return tmp2 = tmp2+" zl";
    
    }else{
    string tmp = to_string(this->self.getBalance());
    tmp.erase(tmp.length()-4);
    return tmp = tmp+" zl";
    }
    }
    static void dbPrint(int limit = 0){ // prints all allocated object
    int max = 0;
    if(limit<accounts.size() && limit != 0) max = limit;
    else if(limit>accounts.size() || limit == 0) max = accounts.size();
    for(int i = 1;i<max;i++){
    cout<<i<<". "<<accounts[i].firstname<<" - "<<accounts[i].lastname<<" "<<accounts[i].acc_number<<endl;
    }
    }
    static account &dbFind(string acc_number){ // returns specific account
    for(int i = 0;i<accounts.size();i++){
    if(accounts[i].acc_number == acc_number) return accounts[i];
    }
    return accounts[0]; // if it does not exist it returns firs special object but i deleted few lines of code and it is normal now, don't bother with that
    }
    static bool dbCheck(string searchacc_number){// checks if account exists
    for(int i = 0;i<accounts.size();i++){
    if(accounts[i].acc_number == searchacc_number) return true;
    }
    return false;
    }
    static void example(){ // example accounts
    accounts.push_back(account("Error","Error","Error", "Error", "Error", "Error", "9999",1)); // here is that special one
    accounts.push_back(account("Marta","Janiszewska","Krakow", "ul.M.Dabrowskiej 13/60", "2014-12-12", "Poland", "1234"));
    accounts.push_back(account("Mirosława","Kowalska","Warszawa", "ul.Górczewska 29", "1963-06-14", "Poland", "1234"));
    accounts.push_back(account("Inga","Slater","Irvine","3293 In Avenue","1975-09-29","Polska","2258"));
    accounts.push_back(account("Jaden","Moody","Oil City","Ap #502-9214 Tempus Road","1979-07-27","Polska","4958"));
    accounts.push_back(account("Arden","Hendricks","Waco","P.O. Box 625, 7841 Et Street","1977-03-08","Polska","4259"));
    accounts.push_back(account("Alfreda","Castillo","Richmond","P.O. Box 546, 7307 Pretium Road","1982-02-28","Polska","8773"));
    accounts.push_back(account("Amena","Holloway","College Park","Ap #871-6626 Donec Rd.","1979-01-18","Polska","7402"));
    accounts.push_back(account("Whoopi","Trevino","Chesapeake","885-6984 Nunc Rd.","1985-05-31","Polska","4594"));
    accounts.push_back(account("Simon","Hansen","Tallahassee","639-7206 Non, St.","1996-02-14","Polska","6138"));
    }
    };
    
    
    int account::count = 0;
    vector <account> account::accounts;
    int main(){
    srand(time(NULL));
    system("CLS");
    account::example();
    account::dbPrint(13);
    string myAcc_number = validate("Account number",2,3);
    account &me = account::dbFind(myAcc_number); // here i assign my account to me
    me.deposit(200000,"1234"); // here im putting in 200 000 cash
    cout<<"ME = "<<me.printBalance()<<endl; // output is 200 000.00 zl
    me.deposit(20000,"1234");
    cout<<"ME = "<<me.printBalance()<<endl;// output is 20 000.00 zl 
    me.deposit(20000,"1234");
    cout<<"ME = "<<me.printBalance()<<endl;// output is 20 000.00 zl
    cout<<"ME = "<<me.printBalance()<<endl;// output is 0.00 zl
    cout<<endl;
    system("PAUSE");
   
    }

 

 

In my opinion problem is here, sometimes it does not read current balance correctly and it puts only amount of money "toDeposit" but i don't now how to fix this.

 

    


bool deposit(double toDeposit, string PIN){
        if(auth(PIN)){
        setBalance(this->self.getBalance()+toDeposit);
        return true;
        }else return false;
        }

 

Oh and when i delete few "printBalance" i get something like this

 

 

 

  


  me.deposit(200000,"1234"); // here im putting in 200 000 cash
    me.deposit(20000,"1234");
    me.deposit(20000,"1234");
    cout<<"ME = "<<me.printBalance()<<endl;// output is 240 000.00 zl
    cout<<"ME = "<<me.printBalance()<<endl;// output is 0.00 zl

 

I added:

 

  


  cout<<"getBalance = "<<getBalance()<<endl;
    cout<<"getBalance = "<<self.getBalance()<<endl;
    cout<<"getBalance = "<<this->getBalance()<<endl;
    cout<<"getBalance = "<<this->self.getBalance()<<endl<<endl<<endl;

at begin of deposit method and i got right numbers only with self. and this->self.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×