>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:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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;
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
> 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][/code] tags.
3. #include
This is an obsolete header. It hasn't been valid for the last 10 years (where have you been?)
4. #include
This isn't even a C++ header, it belongs to C.
5. #include
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?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953