I am new to C++ and am trying to do this

void rolodex::createCard(){

	char* userData = "\0";
	cout << "\n\nEnter first Name - ";
	cin >> userData;
	currentCard.setFirstName(userData);
	cout << currentCard.getFirstName();
	count++;
	
}

when I try to execute it gives me an unhandled exception, access violation error

What am I doing wrong ?? .. please help

>>char* userData = "\0";
Seeing this? This tels the compiler to allocate 2bytes to userData, So if your user enter text more than one character, you're doomed.
Better change to this:

const int MAX=20;
char userData[MAX];

I am smelling that you are using an old compiler. If not, why not using std::strings?

char* is just declaring a pointer to a character (or an array of characters), and what do you do? You're just assigning a value to it, that's a strong mistake !
You first have to reserve/assign memory to that pointer before you can store a value at the memory address where the pointer points to, you assign memory to a pointer using the new or new[] operator, and when you dynamically assign memory to a pointer using these operators, you also have to release it, this can be done using delete and delete[] :) ...

Edit:: new and delete ; new[] and delete[]
Edit:: Or just do what siddhant said:

Better change to this:

const int MAX=20;
char userData[MAX];

I am smelling that you are using an old compiler. If not, why not using std::strings?

Hey Thanks...

Ok I am getting it to work using strings ... but i am trying to learn using char*

and if I don't initialize it by doing char* varName = "/0" .. it gives me the variable is used without being initialized

My question is where exactly can we/should we use char* instead of strings.

Thanks Tux4Life ... using new fixed the problems ... appreciate ur help siddhant

so my only question is when do we use char* and when a string? if string works always why do we even bother using char* and go through the trouble of using new, delete ?

Hey Thanks...

Ok I am getting it to work using strings ... but i am trying to learn using char*

and if I don't initialize it by doing char* varName = "/0" .. it gives me the variable is used without being initialized

My question is where exactly can we/should we use char* instead of strings.

C++ strings are much easier and comfortable to use/work with, char* is used much more in C than in C++ I think, so if you want to make it yourself difficult, use char* otherwise: just use a string (if your compiler supports it)

ok makes sense Tux.. now before i let go of the char* ... when i try using it without "delete" it works fine .... but when i use the delete keyword .. it gives me a heap corruption detected debug error

The code is:

void Card::setFirstName(){
	char* inputFirstName = new char;
	cout << "\n\nEnter First Name - ";
	cin >> inputFirstName;
	firstName = inputFirstName;
	cout << firstName;
	delete inputFirstName;
}

ok makes sense Tux.. now before i let go of the char* ... when i try using it without "delete" it works fine .... but when i use the delete keyword .. it gives me a heap corruption detected debug error

The code is:

void Card::setFirstName(){
	char* inputFirstName = new char;
	cout << "\n\nEnter First Name - ";
	cin >> inputFirstName;
	firstName = inputFirstName;
	cout << firstName;
	delete inputFirstName;
}

The instruction: char* inputFirstName = new char; does actually the same as char inputFirstName; but without cleaning up the memory automatically :) ...
If the user enters more than one character ( cin >> inputFirstName; ) this will crash your program !!
(as there was reserved memory for storing one character only :))
If you really want to use the new operator, then try the following: char* inputFirstName = new char[20]; (remember only an input of 19 characters is allowed), if you use a string ( string inputFirstName; ) you'll have less trouble :) ...

thanks all ... this helps ... will o with string now :) ....

you guys are doing a great job helping newbies

>>but when i use the delete keyword .. it gives me a heap corruption detected debug error

delete an array using delete[] and not just delete.

void Card::setFirstName(){
	char* inputFirstName = new char;
	cout << "\n\nEnter First Name - ";
	cin >> inputFirstName;
	firstName = inputFirstName;
	cout << firstName;
	delete[] inputFirstName;
}
This question has already been answered. Start a new discussion instead.