| | |
Error:Null Pointer assignment
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
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.
C++ Syntax (Toggle Plain Text)
/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(); } }
•
•
•
•
Originally Posted by comwizz
Why should main return an integer ?
•
•
Join Date: Jul 2005
Posts: 1,739
Reputation:
Solved Threads: 281
The best approach would be to show how you have altered your original code. It should be something like this:
C++ Syntax (Toggle Plain Text)
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.
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.
Thanks,
comwizz.
C++ Syntax (Toggle Plain Text)
//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(); } }
comwizz.
Last edited by comwizz; Jan 29th, 2006 at 12:43 am. Reason: mistake in code
![]() |
Similar Threads
- getting null pointer assignment error (C)
- In which case "Null Pointer Assignment" will be displayed on the output ?? (C)
- Why this C code is returning Null Pointer Assignment as output (C)
Other Threads in the C++ Forum
- Previous Thread: runtime dll search
- Next Thread: Any Sample Program???
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






