[Solved] Why it doesn’t accept more than 2 charcters? [closed]


The problem is with this:

char *name = new char;

You’re only allocating 1 char, and that is not enough if you want to store into it something larger than 1 character (not to mention that you need another one for the null-terminator).

Instead try something like this:

char* name = new char[64];  // Be careful. Storing more than 64 characters will lead you to more or less the same error
cin.getline(name, 64);
...
delete[] name;  // Be sure to delete[] name

Better:

char name[64]; // Again, be careful to not store more than 64 characters
cin.getline(name, 64);
...

Best:

std::string name;  // The sane way to use strings
std::getline(std::cin, name);


UPDATE:
If you want to get the length of the string using std::string, you can use std::string::size()

std::cout << name.size() << std::endl;

0

solved Why it doesn’t accept more than 2 charcters? [closed]