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();
 }
}

>Could you tell me a reason why this is happening?
name and acc_type are uninitialized pointers in main. You're trying to write to memory that you don't own, and the compiler seems to think that's a bad idea. :rolleyes:

Also, main really should return an int.

Why should main return an integer ?

I defined the characters p1 and p2 and initialized the pointers with their address still the program doesnt work and hangs. What should I modify in my program?
Thanks,
comwizz.

The best approach would be to show how you have altered your original code. It should be something like this:

int main()
{
   int i,no;
   
   //name and acc_type have no memory associated with them, so you can't use them yet.
   //char *name,*acc_type; 

   //allocate some memory to name---say maybe static memory
   char name[80];

   //allocate some memory to acc_tpe---try dynamic memory if you wish
   char * acc_type = new char[80];

   //now you can put some information in the memory
   cin >> name >> endl;
   cin >> acc_type >> endl;

allocate some memory to acc_tpe---try dynamic memory if you wish.
Without the user specifying the no of letters he is going to enter how should I dynamically allocate memory.for eg. if he is going to enter Dick , I have to allocate 5 bytes without the users specification about the letters he is going to enter i.e. I have to search for a null character and then allocate the memory . I cant figure out how that would be done??
The method you suggested works perfectly fine but would be a wastage of memory as we are taking arrays or allocating memory more than needed.
Thanks,
comwizz.

Heres my attempt for dynamic memory allocation.

//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,count=0;
 char *name,*acc_type,*p;
 float initial;
 account s[2];
 for(i=0;i<2;i++)
 {
  cout<<"Enter name "<<i+1<<": "<<endl;
  cin>>p;
  while(*p!='\0')
  {
   count++;
   p++;
  }
  name=new char[count+1];
  name=p;
  cout<<"Enter account no :\n";
  cin>>no;
  cout<<"Enter account type: \n";
  char *q;
  cin>>q;
  count=0;
  while(*q!='\0')
  {
   count++;
   q++;
  }
  acc_type=new char[count+1];
  acc_type=q;
  cout<<"Enter principal amount:\n";
  cin>>initial;
  s[i].initialize(name,no,acc_type,initial);
 }
 for(i=0;i<2;i++)
 {
  s[i].display();
 }
}

Thanks,
comwizz.

Please reply . I will have to submit this program tomorrow.
Thanks,
comwizz

You could read input into a dummy char array to hold input. Make it a 256 char array if you want since you only need one of them. Then determine the size of the user input string and use dynamic memory to allocate just as much memory as you need to hold user input.

The point of the original reply, which was really just reiterating previous replies, was that you need to assign memory to variables before assigning information to them. If the amount of memory needed isn't known at compile time, then you have to allocate dynamic memory.

As for "wasting" memory, that should by your last concern right now. Storing an int like 1000 in a double, is wasting memory, but needs to be done sometimes. Using databases with fixed length information fields, even if memory is "wasted", should make for easier search protocols and code maintenance. Frequently there's a a trade off between memory usage and speed that needs to be balanced.

HI comwizz
Your very first program is too better than later one...
you are facing with the problem of NULL POINTER ASSIGNMENT

what u to do is
jst

intilize the 'name' and 'acc_type' pointers
as

name= new char;
acc_type=new char;

ur program will run at its finest as far as my knowledge and work is concerned...
//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;
name=new char;
acc_type=new char;
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.initialize(name,no,acc_type,initial);
}
for(i=0;i<2;i++)
{
s.display();
}
}

chect it out Friend.........

chect it out Friend

  1. It's 2 years too late (check the post dates).

  2. You didn't read the intro messages to learn about code-tags.

  3. #include<iostream.h>
    This is an obsolete header. It hasn't been valid for the last 10 years (where have you been?)

  4. #include<string.h>
    This isn't even a C++ header, it belongs to C.

  5. #include<conio.h>
    This isn't even a standard header, so who knows what that's going to do. It might work for you, but it's useless to me.

  6. void main()
    main returns an int. Copious FAQs and ANSI/ISO standards will tell you as much. Don't rely on sloppy compilers which let you get away with any old rubbish.

  7. name=new char;
    Great, any valid C string ends in a '\0', so all you can store here is the empty string. The user types in any input AT ALL, and you've replaced your NULL POINTER assignment with a buffer overflow. How large an input did you test this with?

  8. account_type=acc_type;
    Another horror. Now you have a pointer from inside the class to something outside the class. Worse still, all the instances of this class end up pointing at the SAME memory location. You should have done your strcpy bit with this variable as well.

  9. Where are the constructor and destructor for this class?

  10. Nor do you free memory you allocated in main. If you did do, perhaps it would notice the memory corruption noted in point 7.

Wanna try again?

Hi Dear!
1stly Thanks to pointing out ma mistakes...d bigger one not watching the post dates!

2ndly..i m being working with this concept and u have given me the new Idea to think

3edly i dun mean what is useless to u and others
u would be on destination but i m on the starting.


whenever i need help i will definetly contact u.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.