I am trying to make an ATM program in C++, I was able to make one 1 C# but decided to try and implemment it int C++.
I basicly have this .ini file

#constant format: ACCOUNT_NUMBER PIN BALANCE
123 123 123.12
456 456 456.45

And this code:

#include <iostream>
#include <sstream>
#include <vector> 
#include <fstream>
#include <string>

using namespace std;

#define DATABASE "AtmDatabase.ini"
#define TMPDATABASE "AtmDatabase.ini.tmp"
#define FILEERROR "Error opening file."
#define NOACCOUNT "Wrong account numbet / PIN combination."
#define INVALIDINPUT "Invalid input. Please try again."
#define MINAMOUNT 13
#define FUNDSERROR "You do not have enought funds."

class AtmMachine
{
public:
    int StartAtm();
    bool IsHeMember();
    void GetBalance();
    void AddBalance();
    void Withdraw();
    string ChangeBalance(string message);
    int SaveBalance();
    vector <string> Split(string lne, const char delim);
private:
    string accNumber;
    string Pin;
    string amount;
    string fileAccNumber;
    string filePin;
    string fileBalance;
    string line;
    double newBalance;
    char choice;
    vector <string> parts;
};


int AtmMachine::StartAtm()
{
    cout<<"*** -Welcome to the EEI ATM- ***"<<endl;
    cout<<"** -Please login below- **"<<endl;

    cout<<"\nAccount number: ";
    getline(cin, accNumber);
    cout<<"Pin: ";
    getline(cin, Pin);

    if(IsHeMember())
    {
        cout<<"* -Welcome user- *"<<endl;
        cout<<"* -Please choose an option below- *"<<endl;

        while(true)
        {
            cout<<"\n1 - Check balance."<<endl;
            cout<<"2 - Deposit."<<endl;
            cout<<"3 - Withdraw."<<endl;
            cout<<"0 - Logout."<<endl;
            cout<<"\nChoice: ";
            cin>>choice;

            switch (choice)
            {
            case '1':
                GetBalance();
                break;
            case '2':
                AddBalance();
                break;
            case '3':
                Withdraw();
                break;
            case '0':
                return 0;
            default:
                cout<<INVALIDINPUT<<endl;
                break;
            }
        }
    }
    else
    {
        cout<<NOACCOUNT<<endl;
    }
}

bool AtmMachine::IsHeMember()
{
    ifstream inFile(DATABASE);

    if(!inFile)
    {
        cout<<FILEERROR<<endl;
    }
    else
    {
        while(getline(inFile, line))
        {
            parts = Split(line, ' ');

            if(parts.at(1) == Pin)
            {
                fileAccNumber = parts.at(0);
                filePin = parts.at(1);
                fileBalance = parts.at(2);
                inFile.close();
                return true;
            }
        }
        inFile.close();
        return false;
    }
}

void AtmMachine::GetBalance()
{
    cout<<"Your balance is: "<<fileBalance<<endl;
}

void AtmMachine::AddBalance()
{
    amount = ChangeBalance("Enter the amount you would like to deposit: ");
    newBalance = atof(fileBalance.c_str())  + atof(amount.c_str());
    SaveBalance();
    GetBalance();
}

void AtmMachine::Withdraw()
{
    amount = ChangeBalance("Enter the amount you would like to withdraw: ");
    newBalance = (atof(fileBalance.c_str()) - MINAMOUNT) - atof(amount.c_str());

    if(newBalance >= 0)
    {
        ostringstream oss;

        oss << newBalance;
        fileBalance = oss.str();
        SaveBalance();
    }
    else
    {
        cout<<FUNDSERROR<<endl;
    }

    GetBalance();
}

string AtmMachine::ChangeBalance(string message)
{
    double input = 0;
    string sInput;
    cout<<message;
    cin>>input;

    ostringstream oss;
    oss << input;
    sInput = oss.str();

    return sInput;
}

int AtmMachine::SaveBalance() // write new balance to file
{
    ofstream outFile(TMPDATABASE);

    if(!outFile)
    {
        cout<<FILEERROR<<endl;
        return 1;
    }

    ifstream inFile(DATABASE);

    if(!inFile)
    {
        cout<<FILEERROR<<endl;
        return 1;
    }

    while(getline(inFile, line))
    {
        parts = Split(line, ' ');

        if(parts.at(0) == accNumber && parts.at(1) == Pin)
        {
            outFile<<accNumber<<" "<<Pin<<" "<<fileBalance<<endl;
        }

        else if(parts.at(0) != accNumber && parts.at(1) != Pin)
        {
            outFile<<parts.at(0).c_str()<<" "<<parts.at(1).c_str()<<" "<<parts.at(3).c_str()<<endl;
        }
    }

    inFile.close();
    outFile.close();

    remove(DATABASE);
    rename(TMPDATABASE, DATABASE);

    return 0;
}

vector <string> AtmMachine::Split(string lne, const char delim)
{
    vector <string> result;
    string part;

    istringstream iss(lne);

    while(getline(iss, part, delim))
        result.push_back(part);

    return result;
}

int main()
{
    AtmMachine atm;
    atm.StartAtm();

    cin.get();
    return 0;
}

I am able to check my balance for both accounts when I am logedin on that account. But when I try to withdraw or deposit money my program crashes. Why I dont know...
So if any one could help me identify the problem and fix it, I would appreciate it.

Thanks...

I thinks the problem is with this code segment:

int AtmMachine::SaveBalance() // write new balance to file
{
    ofstream outFile(TMPDATABASE);

    if(!outFile)
    {
        cout<<FILEERROR<<endl;
        return 1;
    }

    ifstream inFile(DATABASE);

    if(!inFile)
    {
        cout<<FILEERROR<<endl;
        return 1;
    }

    while(getline(inFile, line))
    {
        parts = Split(line, ' ');

        if(parts.at(0) == accNumber && parts.at(1) == Pin)
        {
            outFile<<accNumber<<" "<<Pin<<" "<<fileBalance<<endl;
        }

        else if(parts.at(0) != accNumber && parts.at(1) != Pin)
        {
            outFile<<parts.at(0).c_str()<<" "<<parts.at(1).c_str()<<" "<<parts.at(3).c_str()<<endl;
        }
    }

    inFile.close();
    outFile.close();

    remove(DATABASE);
    rename(TMPDATABASE, DATABASE);

    return 0;
}

Edited 3 Years Ago by MasterHacker110: Forgot to post some details.

It is a double: When it asks me how much money i would like to withdraw / deposit i type: 1256.50 then press enter and then it crashes.

I might see your problem. In the last code segment, you have a while loop that's constantly getting the line. Comment that part out and see if it crashes still. Also, using #define is bad because it does not obey scope, and isn't type safe. Use consts.

Edited 3 Years Ago by Terminator1337

outFile<<parts.at(0).c_str()<<" "<<parts.at(1).c_str()<<" "<<parts.at(3).c_str()<<endl;

That 3 should be a 2. You need to learn to use a debugger; it took literally 30 seconds.

Thanks for your help 'Moschops'. Do you know any good debuggers? I am using visual studio 2012 express, though it came out with one, also thought that I have been using it automaticly when I run the program, but I am very knew to vs 2012.

The debugger that comes with VS is fine for this. If you ran it in debug, it should have pointed out the problem line to you.

Edited 3 Years Ago by Moschops

This question has already been answered. Start a new discussion instead.