Error in Calculation if/else

A simple ATM machine program.
Opening balance is 1000.
A minumum balance of 10 is required all the times. If the balance after withdrawal is less than Rm10, do not allow the withdrawal and prompt the user for another amount.

EDIT=The error is that i dont know which strucutre i have to insert for another condition for the minumum balance of 10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include<iostream>
#include<cmath>
using namespace std;
int main()
{

	double opb=1000.00;
	char option;
	double amount;
	cout<<"Your opening balance is RM1000.00"<<endl;

	do
	{
	cout<<"Enter any one of the following options: "<<endl;
	cout<<"0.\tQuit"<<endl;
	cout<<"1.\tDeposit"<<endl;
	cout<<"2.\tWithdrawal"<<endl;
	cout<<"3.\tBalance"<<endl;
	cout<<"Your option: ";
	cin>>option;
	if(option!='0' && option!='1' && option!='2' && option!='3')
	{
		cout<<"Invalid option."<<endl;
		cout<<"Please enter again."<<endl;
	}

	if(option=='0')
	{
		break;
	}
	else if(option=='1')
	{
		cout<<"Amount to Deposits: RM ";
		cin>>amount;
		opb+=amount;
	}
	else if(option=='2' || opb<10.00)
	{
		cout<<"Amoun to Withdraw: RM ";
		cin>>amount;
		opb-=amount;  
		cout<<"Sorry, a minumum balance of RM10.00 is required."<<endl;
		cout<<"Please enter a new amount or 0 to quit."<<endl;
	}
	else if(option=='3')
	{
		cout<<"Your current balance is RM "<<opb<<endl;
	}	
	cout<<"Your current balance is RM "<<opb<<endl;
	}while(!(option>='0' && option<='3'));
	system("pause");
	return 0;
}
		


expected output:
Your opening balance is RM1000.00
Enter any one of the following options: "<<endl;
0.      Quit
1.      Deposit
2.      Withdrawal
3.      Balance   
Your option: 2
Amoun to withdraw: 995
Sorry, a minimum balance of 10.00 is required.
Please enter a new amount or 0 to quit.
Amount to withdraw : 950
Your current balance is 50.00
Press any key to continue.....
Last edited on
Inside the else if statement on line 37, you need to put in a while loop that only executes when opb is less than 10. This should be after line 41.

Inside this while loop reject withdrawal and prompt user for another amount.

After the while loop write code that accepts withdrawal.
Last edited on

use else if instead of if in line no 27





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<cmath>
using namespace std;
int main()
{

	double opb = 1000.00;
	char option;
	double amount;
	cout << "Your opening balance is RM1000.00" << endl;

	do
	{
		cout << "Enter any one of the following options: " << endl;
		cout << "0.\tQuit" << endl;
		cout << "1.\tDeposit" << endl;
		cout << "2.\tWithdrawal" << endl;
		cout << "3.\tBalance" << endl;
		cout << "Your option: ";
		cin >> option;
		if (option != '0' && option != '1' && option != '2' && option != '3')
		{
			cout << "Invalid option." << endl;
			cout << "Please enter again." << endl;
		}

		else if (option == '0')
		{
			break;
		}
		else if (option == '1')
		{
			cout << "Amount to Deposits: RM ";
			cin >> amount;
			opb += amount;
		}
		else if (option == '2' || opb<10.00)
		{
			cout << "Amoun to Withdraw: RM ";
			cin >> amount;
			opb -= amount;
			cout << "Sorry, a minumum balance of RM10.00 is required." << endl;
			cout << "Please enter a new amount or 0 to quit." << endl;
		}
		else if (option == '3')
		{
			cout << "Your current balance is RM " << opb << endl;
		}
		cout << "Your current balance is RM " << opb << endl;
	} while (!(option >= '0' && option <= '3'));
	system("pause");
	return 0;
}


Last edited on
@boostlexicalcat @bird1234 thanks for the reply and advice. i fixed my code :)
DesmondLee wrote:
@boostlexicalcat

I'm no cat. :P
@boost lexical cast im soooo sorry.. typing error haha
Topic archived. No new replies allowed.