Switch Selection

Write a separate main() function that has 2 int selections to execute the program. The main() must create objects and assign account holder’s name, account number and initial balance before proceeding. If account number == 0 none of the below operation can be performed. The 2 selections are:
1 : Deposit
2 : Withdrawal
Use switch case to determine the choice and filter invalid valuesestion here.

#include <iostream>
#include <string>
#include "ATM.h"
using namespace std;
void main()
{

int number;
string AccountName;
double balance;
cout << "Enter your name: ";
cin >> AccountName;
cout << "Enter your amount: ";
cin >> number;
cout << "Enter initial balance: " ;
cin >> balance;

ATM BankAccount(number, AccountName, balance);
if (number != 0)
{
int num;
cout << "Enter amount selection: [1] Deposit or [2] WithDrawal: ";
cin >> num;
switch (num)
{
case 1:
cout << "Your balance is: " << BankAccount.Deposit();
break;
case 2:
cout << "Your balance is: " << BankAccount.WithDrawal();
break;
}
cout << endl << endl;
}
}


p/s : i already create bank.h with atm.h header file. So this is the code. I can't get the switch selection correct. Im not sure about the switch selection part.
Your switch statement is working fine.
Dear mutexe, why when executing my program after press [1] the console does not calculate and it ends by showing ''please enter to continue''. Please help.
that has nothing to do with your switch statement then.
I took your switch statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>

int main()
{
	int num (0);
	std::cout << "Enter amount selection: [1] Deposit or [2] WithDrawal: ";
	std::cin >> num;
	switch (num)
	{
	case 1:
		std::cout << "deposit" << std::endl;
		break;
	case 2:
		std::cout << "withdrawl" << std::endl;
		break;
	}


	return 0;
}


and the correct strings are outputted depending on whether i type 1 or 2.

Perhaps the issue lies within your ATM::Deposit() method?
class ATM
depends on your own design and imagination. Note that ATM must work as a proxy class between BankAccount and main.

#include <iostream>
#include <string>
#include "Bank.h"
using namespace std;

class ATM
{
public:
ATM(int no, string newName, double balanceM) // constructor
{
AccountM = new BankAccount(no, newName);
balance = balanceM;
}
double Deposit()
{
getAmount();
return AccountM -> Deposit(balance, amount); // -> is a 'THIS' pointer
}
double WithDrawal()
{
getAmount();
return AccountM -> Deposit(balance, amount); // -> is a 'THIS' pointer
}
void getAmount()
{
cout << "Key in amount: " << amount << endl;
}
private:
BankAccount *AccountM;
double balance;
int amount;
};

p/s : this is my ATM.h file. So the question states that when we press 1 or 2, it must display the output of the deposit/withdrawl with balance.
1. Write 2 classes in 2 different files
1. class BankAccount
2. class ATM (a proxy class)
where:
class BankAccount has
private:
int AccNum
string name;
double Balance
/*utility function to calculate deposit or
withdrawal. Refer to below formulas* */
double Calculate(double d, int c)
public:
BankAccount(int AN, string NM)
BankAccount()
double Deposit(double d, int c)
double Withdrawal (double w, int c)
................................................................................................................
#include <iostream>
#include <string>
using namespace std;
class BankAccount
{
public:
BankAccount(int AN, string NM)
{
AccountNumber = AN; // bank account number owner
AccountName = NM; // bank account name
}
double Deposit(double balance, int deposit)
{
return balance = balance + deposit;
}
double WithDrawal(double balance, int WithDrawal)
{
if ((balance - WithDrawal) > 10)
{
cout << "Success" << endl;
return balance = balance - WithDrawal;
}
else
{
cout << "Fail. Please try again" << endl;
return balance;
}
}
private:
int AccountNumber;
string AccountName;
double balance;
};

This the bank.h code. please help me to correct which mistake i made.
this logic:
1
2
3
4
5
6
7
8
9
10
	double Deposit()
	{
		getAmount();
		return AccountM->Deposit(balance, amount); // -> is a 'THIS' pointer
	}

	void getAmount()
	{
		cout << "Key in amount: " << amount << endl;
	}


is a bit screwed. getamount just prints zero to the screen, but then your code does output the balance I entered just fine.


Enter your name: tom
Enter your amount: 500
Enter your initial balance: 200
Enter your amount selection: [1] Desposit or [2] WithDrawl: 1
Key in amount: 0
Your balance is: 200

Last edited on
mutexe thank you :) . It helps me alot.
Topic archived. No new replies allowed.