I've been working on my first project in C++, and I have a function where I get a users name. It goes like this:
1 2 3 4 5 6 7 8 9
void getName()
{
char name[24];
cout << "Enter your name: ";
cin.getline(name, 24);
cin.clear();
cout << "Your chosen name is ";
cout << name << endl;
}
It works fine for the most part, but the thing I'm trying to make it do is ignore any input past the first 24 characters. Currently when I run this, if you use say 26 characters, it just crashes.
Please advise! Thanks.
Make it if (playerName.length()>10)playerName.resize(10); or playerName.resize(min(playerName.length(),10u));, otherwise you'd append null chars to a name shorter than 10 characters.
Edit: unless you already do this. I guess you might.