ATM

hello guys, i am quite new with c++ and i have to make an ATM machine i've made my code but when i withdrawn the money and i want to check my balance? it does not add up. can you please point out where did i go wrong? and if can how can i fix it

[code]
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
char code, option;
double amount, inibalance, withd, newbalance, balance;
cout<<"Please enter initial balance : ";
cin>>inibalance;
do
{
cout<<"Please enter required code to continue, B for balance and W for withdrawal"<<endl;
cin>>code;

switch(code)
{
case 'B':
inibalance-=newbalance;
cout<<"your balance = "<<endl;


break;
case 'W':
cout<<"how much do you want to withdraw: "<<endl;
cin>>withd;

if (withd<0)
cout<<"why you put negative number one ha? aiyoooooooo "<<endl;
else if (withd>1000)
{
cout<<"The amount to be withdrawn cannot exceed 1000"<<endl;

}
else if(withd>inibalance)
{
cout<<"insufficient amount to be withdrawn"<<endl;

}
else if(withd<=300)
{
newbalance = inibalance - withd;
cout<<setprecision(2)<<fixed<<"you current balance : "<<newbalance<<endl;
}
else if( withd>300)
{
double servicecharge;
servicecharge = ( withd - 300)* (double(4)/100);
newbalance = inibalance - (withd + servicecharge);
cout<<fixed<<setprecision(2)<<newbalance;
}


}
sub:
cout<<"\ndo you wish to continue? Enter Y to continue and enter N to exit ATM"<<endl;
cin>>option;



}
while(option=='Y');

/*if (option != 'Y' && option !='N')
{
cout<<"invalid code was entered"<<endl;
goto sub;}
else if ( option == 'N')*/

return 0;
}
Last edited on
Hello dizz767,

First I will suggest that you initialize all your variables. The best example of this in in line inibalance-=newbalance;. newbalance has no value, so this will usually give a run time error. Throughout your code you should only use balance in all your calculations so the code will be less confusing.

Stay away from goto statements. They will get you in trouble also they are hard to follow. I am not sure if trying to go back into the do while loop will even work. That bit of code needs to be inside the do while loop and works better as:
1
2
3
4
5
6
7
8
9
10
11
option = toupper(option);

while (option != 'Y' && option !='N')
{
	cout<<"invalid code was entered"<<endl;
	cin.clear();
	cin.ignore(32700, '\n');
        cout << "\n Do you wish to continue? Enter Y to continue and enter N to exit ATM ";
	std::cin >> option;
	option = toupper(option);
}


Hope that helps,

Andy

Topic archived. No new replies allowed.