Hello everyone,
I ahd to make a program for initializing members of a class and displaying them.The members were name , bank account no, account type and current balance.
Well , the program works well except for that null pointer assignment is displayed on the screen after correct output.Could you tell me a reason why this is happening?
Thanks,
comwizz.
/Program to deposit money in a bank account
#include<iostream.h>
#include<string.h>
#include<conio.h>
class account
{
public:
void initialize(char *p,int,char *c,float);
void deposit(float);
void withdraw(float);
void display(void)
{
cout<<"Name: "<<p<<endl;
cout<<"Balance: "<<balance<<endl;
}
private:
float balance,principal;
char *p,*account_type;
int account_no;
};
void account::initialize(char *name,int acc_no,char *acc_type,float initial)
{
int q=strlen(name);
p=new char[q+1];
strcpy(p,name);
account_no=acc_no;
account_type=acc_type;
principal=initial;
balance=initial;
}
void account::deposit(float add)
{
balance=add+principal;
}
void account::withdraw(float subtract)
{
balance=principal-subtract;
}
void main()
{
int i,no;
char *name,*acc_type;
float initial;
account s[2];
for(i=0;i<2;i++)
{
cout<<"Enter name "<<i+1<<": "<<endl;
cin>>name;
cout<<"Enter account no :\n";
cin>>no;
cout<<"Enter account type: \n";
cin>>acc_type;
cout<<"Enter principal amount:\n";
cin>>initial;
s[i].initialize(name,no,acc_type,initial);
}
for(i=0;i<2;i++)
{
s[i].display();
}
}