I am using char * for getting data from the user. Now, if i just do

char * data = new char[100];
cin >> data;

The data is corrupted, i.e. only data before whitespace is stored; and the following is an example result:

enter: "Microsoft Co."
data: "Microsoft"

Can someone tell me why this thing happens?

Also, secondly, I want to avoid declaring a size to the char *, since i want the input to be flexible. Now, the following code says that the 'data' is not initailized when executed:

char * data;
cin >> data;

Any idea how to come around this?

Thanks!

I am using char * for getting data from the user. Now, if i just do

char * data = new char[100];
cin >> data;

The data is corrupted, i.e. only data before whitespace is stored; and the following is an example result:

enter: "Microsoft Co."
data: "Microsoft"

Can someone tell me why this thing happens?

It has nothing to do with dynamic memory allocation. The >> operator stops at the first white space. If you want text that includes spaces then you need to use getline(). Using std::string is preferred but you can use character arrays too. cin.getline(data, 100);

Also, secondly, I want to avoid declaring a size to the char *, since i want the input to be flexible. Now, the following code says that the 'data' is not initailized when executed:

char * data;
cin >> data;

Any idea how to come around this?

Thanks!

Your compiler is correct, you can not use a pointer before it has been allocated, as in your first example. The solution you are seeking is in std::string. With that class you do not need to specify the string's length, and can be used almost exactly like a character array.

std::string data;
getline(cin, data);

Edited 6 Years Ago by Ancient Dragon: n/a

I am using char * for getting data from the user. Now, if i just do

char * data = new char[100];
cin >> data;

The data is corrupted, i.e. only data before whitespace is stored; and the following is an example result:

enter: "Microsoft Co."
data: "Microsoft"

Can someone tell me why this thing happens?

Also, secondly, I want to avoid declaring a size to the char *, since i want the input to be flexible. Now, the following code says that the 'data' is not initailized when executed:

char * data;
cin >> data;

Any idea how to come around this?

Thanks!

for dynamic string allocation while using the string use the C++ CString class which will grow and shrink the length of the string as required.

to get a whole line of imput including white spaces between words try using cin.getline()

for dynamic string allocation while using the string use the C++ CString class

There is no such standard c++ class. You are either thinking of std::string, which I already mentioned in my post, or MFC CString class, which only works with Microsoft compilers and MFC.

There is no such standard c++ class. You are either thinking of std::string, which I already mentioned in my post, or MFC CString class, which only works with Microsoft compilers and MFC.

char *textToKeep;					//pointer to the text you want to keep

	textToKeep= (char*) malloc(1024);	//set to an arbitrary large size to begin with - enough to take in whatever input you expect

	cin.getline(textToKeep,1024,'\n');	//get characters from the keyboard and stop reading characters if there are more than 1024(or whatever limit you like)
										//or until you get to a new line character '\n'
		
	textToKeep=(char*)realloc(textToKeep,strlen(textToKeep)+1);	//reallocate the momory and only keep the length of the string including terminating NULL that was typed in
This question has already been answered. Start a new discussion instead.