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 :)