Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++, simple bank simulation. devc++ compiler STOPS halfway. (http://www.go4expert.com/forums/cpp-simple-bank-simulation-devcpp-t26237/)

jose_peeterson 8Jul2011 10:24

C++, simple bank simulation. devc++ compiler STOPS halfway.
 
// dear experts, i have no idea where the error is, i don't get any error messages but //after running it
// stops half way, please rectify this problem, thanks.
// there is No error in code.


#include<iostream.h>
using namespace std;

class bankacct
{
private : int acc_num ;
double bal;

public : bankacct()
{
acc_num = 20110001;
bal = 0;
}
void Newaccount(int num);
void deposit(double amount);
void withdrawal(double amount);
void print(int num, class bankacct account[5]);
};

void bankacct::Newaccount(int num)
{
cout<<"Your choice 1\n\n";
acc_num = acc_num + num;
cout<<"Result: ["<<num<<"/5 account created.\n";
cout<<"Account Number: "<<acc_num<<"\n";
cout<<"Balance: $0.00\n";
return;
}

void bankacct::deposit(double amount)
{
cout<<"Your choice: 2\n";
cout<<"Account Number: "<<acc_num<<"\n";
cout<<"Amount: "<<amount<<"\n\n";

cout<<"Result: Deposit performed\n";
cout<<"Account Number: "<<acc_num<<"\n";
cout<<"Balance: $"<<(bal = (bal + amount))<<"\n";
return;
}

void bankacct::withdrawal(double amount)
{
if(amount > bal)
{
cout<<"Withdrawal failed, withdrwal greater than account balance\n";
return;
}

cout<<"Your choice: 3\n";
cout<<"Account Number: "<<acc_num<<"\n";
cout<<"Amount: "<<amount<<"\n\n";

cout<<"Result: Withdrawal performed\n";
cout<<"Account Number: "<<acc_num<<"\n";
cout<<"Balance: $"<<(bal = (bal - amount));
return;
}

void bankacct::print(int num, class bankacct account[5])
{
int ctr;

cout<<"Result: "<<num<<" existing accounts.\n";
for(ctr=0;ctr<num;ctr)
{
cout<<"Account number: "<<account[ctr].acc_num<<"\n";
cout<<"Balance: $"<<account[ctr].bal<<"\n\n";
}
return;
}

int main()
{
class bankacct account[5];
double amount;
int num = 20110000; // user number 0 - 4, account[num];
int operation = 0;

while((operation != 5) || (num > 4))
{
cout<<"Service available (1. New Acct, 2. Deposit, 3. Withdrawal, 4. Print All, 5. exit)\n";
cin>>operation;

if(operation == 1)
num++;

if(operation == 1)
{
account[num].Newaccount(num);
}

if(operation == 2)
{
cout<<"Enter the amount to deposit : ";
cin>>amount;
account[num].deposit(amount);
}

if(operation == 3)
{
cout<<"Enter the amount to withdraw : ";
cin>>amount;
account[num].withdrawal(amount);
}
if(operation == 4)
{
account[1].print(num,account);
}
}

if(num>4)
{
cout<<"Sorry, not more than 5 accounts in 1 attempt\n";
return 0;
}

system("pause > null");
return 0;
}

xpi0t0s 8Jul2011 11:29

Re: C++, simple bank simulation. devc++ compiler STOPS halfway.
 
You've been around here long enough to know by now: USE CODE BLOCKS WHEN POSTING CODE. It's REALLY not that hard. Read the posting guidelines if you've forgotten how.

>> there is No error in code

Are you really certain about that? Have a good think about the following lines:
Code:

class bankacct account[5];
int num = 20110000;
account[num].Newaccount(num);


jose_peeterson 9Jul2011 16:41

Re: C++, simple bank simulation. devc++ compiler STOPS halfway.
 
mr xipittos,
thank you for being meticulous enough to detect silly errors. i got my program finished and working. but i dont get what you mean by code blocks. you dont have to reply me now
tell me in my answer to next question.
thanks.

other errors include ctr (ctr++) num (num+1) num=0 (num=-1)

jose_peeterson 9Jul2011 16:50

Re: C++, simple bank simulation. devc++ compiler STOPS halfway.
 
//sorry one more thing,
//in the previous program when i wanted to print the accounts. i had to do this

if(operation == 4)// how to print all existing accounts??
{
account[1].print(num,account);
}

//here i randomly chose account[] to be 1. but is there any standard way to call this function.
// i think my programing style is not good. i should have printed each account individually but using a for loop in the main itself. RIGHT?

xpi0t0s 11Jul2011 02:21

Re: C++, simple bank simulation. devc++ compiler STOPS halfway.
 
When you press New Topic to start a new thread, there's a bit of text that says

Before proceeding, please make sure you are aware of the Community Guidelines and you have read the Hints before you make a post thread.

It's between the title box and the main message box, you can't really miss it. The explanation of code blocks is in one of those two so please read them both in full.

xpi0t0s 11Jul2011 02:32

Re: C++, simple bank simulation. devc++ compiler STOPS halfway.
 
About printing the accounts, you don't really seem to have got a handle on this object-oriented thing. A bankacct object should represent just one bank account, not the whole collection, so the print function should display only one account, i.e. "this" one. So you'd end up with something like:
Code:

void bankacct:print()
{
cout<<"Account number: "<<acc_num<<"\n";
cout<<"Balance: $"<<bal<<"\n\n";
}

//...

if (operation==4)
{
int ctr;

cout<<"Result: "<<num<<" existing accounts.\n";
for(ctr=0;ctr<num;ctr)
  account[ctr].print();
}



All times are GMT +5.5. The time now is 07:39.