0

I had this school assignment to build an atm machine. I got it to work but if I deposit or withdrawal money and view my account balance later its still not updated. Can someone tell me what I am doing wrong and any suggestion on how to make my code better, in general, would be appreciated.

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

void WelcomeMenu();
void Menu();
void userChoiceDeposite(double accountbalance);
void userChoiceWithdraw(double accountbalance);
void userChoiceView(double accountbalance);
void printReceipt();


int main ()
{
//clear receipt.txt from previous transactions
ofstream clearFile("Receipt.txt");
clearFile.close();


//Declaring variables
string userInput;
char userAction;

//prompt user for input, start the program or quit
cout <<"Enter start to continue, or Q/q to quit ";
getline (cin, userInput);
cout <<endl;

//check the input
if ((userInput == "q") || (userInput == "Q"))
{
    cout <<"Have a nice day!" <<endl;
    return 0;
}

else if (userInput == "start")
{
const int sizeLimit = 50;
string firstName[sizeLimit], lastName[sizeLimit], passWord[sizeLimit];
double accountBalance[sizeLimit];
int count = 0;

ifstream readFile("Accounts.txt");

 while (!readFile.eof())
 {
    readFile >> firstName[count];
    readFile >> lastName[count];
    readFile >> passWord[count];
    readFile >> accountBalance[count];
    count++;
 }
    //ask for username/pw
    string fName, lName, pwd;
    double accountbalance;
    bool exists = false;

 do
 {
    cout <<"Enter First Name: ";
    cin >> fName;
    cout <<"Enter Last Name: ";
    cin >>lName;
    cout <<"Enter Password: ";
    cin >> pwd;

    for (int i=0; i<sizeLimit; i++)
    {
        if (firstName[i] == fName)
        {
            if (lastName[i] == lName)
            {
                if (passWord[i] == pwd)
                {
                    exists = true;
                    accountbalance = accountBalance[i];
                }
            }
        }
    }
 }
 while (exists == false);
    //if correct display menu
    WelcomeMenu();
    cin >> userAction;
    //while login is valid (true)
    while (userAction != 'q')
    {


        if (userAction == 'D' || userAction == 'd')
        {
            userChoiceDeposite(accountbalance);
        }
        else if (userAction == 'W' || userAction == 'w')
        {
            userChoiceWithdraw(accountbalance);
        }
        else if (userAction == 'V' || userAction == 'v')
        {
            userChoiceView(accountbalance);
        }
        else if (userAction == 'P' || userAction == 'p')
        {
            printReceipt();
            return 0;
        }
        else
        {
            cout <<"Have a nice Day" <<endl;
            return 0;
        }
        Menu();
        cin >> userAction;


    }
}

else
{
    cout <<"Input Error..." <<endl;
}


return 0;
}


//welcome menu display function
void WelcomeMenu()
{

cout <<"Welcome to your account " <<endl;
cout <<endl;
cout <<"[D/d] Deposit Money" <<endl;
cout <<"[W/w] Withdraw Money" <<endl;
cout <<"[V/v] View Account Balance" <<endl;
cout <<"[Q/q] Quit" <<endl;

}
//function after action was taken (add print option)
void Menu()
{
cout <<endl;
cout <<"[D/d] Deposit Money" <<endl;
cout <<"[W/w] Withdraw Money" <<endl;
cout <<"[V/v] View Account Balance" <<endl;
cout <<"[P/p] Print Receipt and quit" <<endl;
cout <<"[Q/q] Quit" <<endl;

}
//deposit function
void userChoiceDeposite(double accountbalance)
{

int depositMoney;
cout <<"Enter the ammount of money you want to deposit. Max $10000"         <<endl;
cin >> depositMoney;

if ((depositMoney > 0) && (depositMoney <= 10000))
{
    accountbalance = accountbalance + depositMoney;
    ofstream saveAction("Receipt.txt", ios_base::app);
    saveAction << "You made a deposit of $" <<depositMoney <<endl;
    saveAction.close();

}
else
{
    cout <<"Incorrect ammount" <<endl;
    return userChoiceDeposite(1);
}

}
//withdraw function
void userChoiceWithdraw(double accountbalance)
{
int withdrawMoney;
cout <<"Enter the ammount of $ you want to withdraw:";
cin >> withdrawMoney;

if (accountbalance < withdrawMoney)
{
    cout <<"You dont have that much money"<<endl;
    return userChoiceWithdraw(1);
}
else
{
    accountbalance = accountbalance - withdrawMoney;
    ofstream saveAction("Receipt.txt", ios_base::app);
    saveAction << "You withdrew $" <<withdrawMoney <<endl;
    saveAction.close();
}
}
//view function
void userChoiceView(double accountbalance)
{
cout <<"Your account balance is $" <<accountbalance <<endl;
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "You viewed your account balance" <<endl;
saveAction.close();


}
//print receipt function
void printReceipt()
{
ofstream saveAction("Receipt.txt", ios_base::app);
saveAction << "" <<endl;
saveAction << "" <<endl;
saveAction << "" <<endl;
saveAction << "Thank you! Come again!" <<endl;
saveAction.close();
}
Blooddy
  • 13
  • 3
  • 1
    related/dupe: https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – NathanOliver Jul 19 '17 at 16:43
  • Also worth reading: [Why is iostream::eof inside a loop condition considered wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Consider as a replacement `while (readFile >> firstName[count] >> lastName[count] >> passWord[count] >> accountBalance[count]) { count++; }` This takes advantage of `>>` returning a reference to the input stream allowing chaining of the IO and [`operator bool`](http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool) – user4581301 Jul 19 '17 at 18:00
  • Possible duplicate of [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – perfect5th Jul 19 '17 at 20:15

1 Answers1

0

You are only changing the value that you pass into the function (thus not changing the accountbalance outside of the function). For your methods where you need to update the accountbalance you either need to:

  • Pass by reference
  • Create a global variable
  • use a return value to update the account balance

Try one of these and see how it goes.

Twtheo
  • 101
  • 1
  • 14