0

I am building an ATM sort of program where you can deposit and withdraw balance with the use of functions. I noticed at first that after i deposit first time, it works fine but after the second time using deposit it doesn't add it to the current balance but the balance before it.

For example:
(1st Attempt)
*Balance = 1000
*I deposit 500
*Balance is now = 1500

(2nd Attempt)
*I deposit 700
*Balance is now 1700

Instead of making it to 2200, it resets back to 1000 before went for my second attempt resulting in a result of 1700. Can anyone please explain what went wrong in the code? I am willing to not only get the correct code but to also learn how it was done.

This is for my training in c++.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int deposit(int x, int y)
{
    int newbal = x + y;
    return newbal;
}

int withdraw(int x, int y)
{
    int newbal = x - y;
    return newbal;
}
int main()
{
    int menu;
    int selection;
    double x = 1000, y;
    int trans;
    char user[20], pass[20], pktc[3];
    string newuser, newpass;

    do{
    system ("CLS");
    cout << "Welcome To Bank" << endl;
    cout << "[1] Register" << endl;
    cout << "[2] Login" << endl;
    cout << "[3] Exit" << endl;
    cout << "\n\nEnter command: " <<endl;
    cin >> menu;
    switch(menu)
    {

//----------------------------------------------------------------CASE 1------------------------------------------------------------------------------//
//----------------------------------------------------------------REGISTER----------------------------------------------------------------------------//

        case 1:
        system ("CLS");
        cout << "<-------REGISTER------->\n\n";
        cout << "Enter Name: ";
        cin >> user;
        newuser = user;
        cout << "Enter Password: ";
        cin >> pass;
        newpass = pass;
        cout <<"REGISTERED SUCCESSFULLY!" << endl;
        cout <<"\n\nPress Any key to contniue" << endl;
        cin >> pktc;
        system ("CLS");

        break;

//------------------------------------------------------------END OF REGISTER--------------------------------------------------------------------------//   



//----------------------------------------------------------------CASE 2------------------------------------------------------------------------------//
//-----------------------------------------------------------------LOGIN------------------------------------------------------------------------------//

        case 2:
        system ("CLS");
        cout << "<-------LOGIN------->\n\n";
        cout << "Enter Username: ";
        cin >> newuser;
        cout << "Enter Password: ";
        cin >> newpass;
        if(newuser != user || newpass != pass)
        {

//-------------------------------------------------------------FAILED LOGIN----------------------------------------------------------------------------//

            cout << "\nInvalid account" << endl;
            cout <<"\n\nPress Any key to contniue" << endl;
            cin >> pktc;
            system ("CLS");
        }
        else if (newuser == user || newpass == pass)
        {

//----------------------------------------------------------------CASE 2.1------------------------------------------------------------------------------//
//------------------------------------------------------------SUCCESFULL LOGIN--------------------------------------------------------------------------//

        system ("CLS");
        cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
        cout<<"\nWelcome To Banco De Nelio";
        cout<<"\nUSERNAME:  "<< user << endl;
        cout<<"\n\n TIP ATM MACHINE";
        cout<<"\n   1. Balance";
        cout<<"\n   2. Deposit";
        cout<<"\n   3. Withdraw";
        cout<<"\n   4. Exit";

do{

cout<<"\n\nChoose Transaction[1-3]:";
cin>>trans;

switch(trans)
    {

//----------------------------------------------------------------ATM CASE 1------------------------------------------------------------------------------//
//--------------------------------------------------------------CHECK BALANCE--------------------------------------------------------------------------//

        case 1:
        system ("CLS");
        cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
        cout<<"\nWelcome To Banco De Nelio";
        cout<<"\nUSERNAME:  "<< user << endl;
        cout<<"\n\n TIP ATM MACHINE";
        cout<<"\n   1. Balance";
        cout<<"\n   2. Deposit";
        cout<<"\n   3. Withdraw";
        cout<<"\n   4. Exit";
        cout<<"\n\nYour total balance is: "<< deposit(x, y) ;
        break;

//----------------------------------------------------------------ATM CASE 2------------------------------------------------------------------------------//
//--------------------------------------------------------------BEFORE DEPOSIT--------------------------------------------------------------------------//

        case 2:
        system ("CLS");
        cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
        cout<<"\nWelcome To Banco De Nelio";
        cout<<"\nUSERNAME:  "<< user << endl;
        cout<<"\n\n TIP ATM MACHINE";
        cout<<"\n   1. Balance";
        cout<<"\n   2. Deposit";
        cout<<"\n   3. Withdraw";
        cout<<"\n   4. Exit";
        cout<<"\n\nEnter the amount:" ; 
        cin>>y;

//--------------------------------------------------------------AFTER DEPOSIT--------------------------------------------------------------------------//

        system ("CLS");
        cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
        cout<<"\nWelcome To Banco De Nelio";
        cout<<"\nUSERNAME:  "<< user << endl;
        cout<<"\n\n TIP ATM MACHINE";
        cout<<"\n   1. Balance";
        cout<<"\n   2. Deposit";
        cout<<"\n   3. Withdraw";
        cout<<"\n   4. Exit";
        cout<<"\n\nYour total balance now is: " << deposit(x, y) <<endl;
        break;

//----------------------------------------------------------------ATM CASE 3------------------------------------------------------------------------------//
//--------------------------------------------------------------WITHDRAW BALANCE--------------------------------------------------------------------------//

        case 3:
        cout<<"\nEnter the amount:" ;
        cin>>y;
        if ( y > x)
        {
            cout<<"\nYou cannot withdraw " << y;
            cout<<" because the amount is higher than your balance" << endl;
            break;
        }
        else
            x = x - y;
            cout<<"\nYour Total Balance is now " << withdraw(x, y) << endl;
            break;

//----------------------------------------------------------------ATM CASE 4------------------------------------------------------------------------------//
//-------------------------------------------------------------------BACK--------------------------------------------------------------------------------//

        case 4:
            cout<<"\n\nThank You!" << endl;
            break;
        default:
            cout<<"\nYou did not enter any valid number" << endl;
            break;
        }
    }while (trans<=3);
        }
        break;

//----------------------------------------------------------------CASE 3------------------------------------------------------------------------------//
//-----------------------------------------------------------------EXIT-------------------------------------------------------------------------------//

        case 3:
        system ("CLS");
        cout << "Thank you for using me!\n";
        return 0;
//-------------------------------------------------------------END OF EXIT----------------------------------------------------------------------------//
    }

}while (menu<=3);
}

i am not sure if the problem here is the function or a conflict in switch statement. Thanks In Advance

EDIT Please register first :)

sweetdish
  • 39
  • 5
  • 1
    Where do you set the balance with the return of `deposit`? – NathanOliver Sep 26 '18 at 14:49
  • 1
    You are *printing* the new balance and hoping somebody or something will magically cause it to be *remembered*. It won't happen because there's no magic. – n. m. could be an AI Sep 26 '18 at 14:50
  • This is not a [mcve] at least not **Minimal**. So much `cin` and `cout` for showing that your `deposit()` is called inside `cout` without storing the result anywhere... – Scheff's Cat Sep 26 '18 at 14:50
  • In the function instead of manually calculating it, I used function then called it in my ATM Case 2. Is that correct? – sweetdish Sep 26 '18 at 14:51
  • No. In `deposit()` you're simply returning the `newbal` and printing `newbal` with cout. Instead, you should be setting `newbal` to `x`, so when the next time it runs, the balance is updated. – Chris Sep 26 '18 at 14:53
  • 1. Remove all the `//----------------` comments 2. For each case create a function, for example `void atm_case_4(void) { cout <<"\n\nThank You!" << endl; }` and call the function in that case. 3. That way the functions names are really comments and the code is well organized. 4. Fix the code with modifying x in the deposit case and leaving y as zero. `cin >> y; x += y; y = 0;` or better `cin >> y; x = deposit(x, y); y = 0;`. Or make withdraw and deposit take a reference to x and change it in them. – KamilCuk Sep 26 '18 at 14:54
  • @sweetdish You have two functions, where each of them are two lines. Then you have a gigantic `main` function full of comments and unnecessary "menu" code, and it is difficult to see where you're calling these 2 tiny functions. How about cutting your `main` function to 10 lines or less, and just call those functions? – PaulMcKenzie Sep 26 '18 at 14:54
  • 3
    Side note: I would get in a habit of giving your variables some meaningful names. Variables like `x` and `y` don't really have any substance to them unless they're actually x/y coordinates. – Chris Sep 26 '18 at 14:55
  • Oh okay, i really made a mistake. I can really shorten up my codes by using void functions. Thanks, I know that you can call a function to calculate but not print text. I'll shorten it. Also I have remove all the commends and took ur advice – sweetdish Sep 26 '18 at 15:00

1 Answers1

2

Its really hard to spot the important pieces, but basically your problem can be boiled down to something like

int add(int x,int y) { return a+b; }
int sub(int x,int y) { return a-b; }

int main() {
    int initial = 0;
    // add 10 then subtract 5
    std::cout << add(initial,10) << '\n';  // prints 10
    std::cout << sub(initial,5) << '\n';   // prints -5
}

When what you actually want is something like

int main() { 
    int initial = 0;
    // add 10 then subtract 5 and update initial
    initial = add(initial,10);
    std::cout << initial << '\n';
    initial = sub(initial,5); 
    std::cout << initial << '\n';
}

Your methods correctly calculates the new amount, but when you call those methods you ignore the return value when instead you want to update some variable.

Some other suggestions (in random order):

  • use meaningful names. x and y do not convey any meaning, compare int deposit(int current_balance, int amount) to int deposit(int x,int y)
  • use functions to split your code into smaller pieces (eg it would be a good idea to seperate input, output and logic). Whenever you find yourself writing a comment describing what a block of code is doing, this block of code is a good candidate for a function that gets a proper name instead of a comment
  • dont use std::endl when you want to end a line (std::endl ends the line and flushes the stream, which is most of the time not what you want), use \n instead
  • dont use using namespace std;. It wont do much harm in your current code, but read here why you never should do it in a header, and better dont get used to bad habits from the start.
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185