limiting characters into buffer

Started by
14 comments, last by TheOne1 19 years, 8 months ago

void createChar( PlClient &setObj )
{
	cin.ignore( 1 );
	char setName[ 20 ];

	cout << "Character creation process\n\n";

	cout << "Enter name for character (20 Characters max): ";
	cin.getline( setName, 20 );
	cout << '\n';

	strcpy( setObj.plName, setName );

	system("pause");
	system("cls");
}

How do I limit the characters that is read into cin.getline? So how do I make it so that it ignores everything after 20 characters? I tried using setw( 20 ), but for some reason it won't work.
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
Advertisement
cin.getline( buf, 20 ) should work.
strncpy() also makes a good replacement for strcpy() (idem with sprintf()/snprintf(), etc)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
In a quick test I got it to work just fine. The "20" in the call to getline should be all you need. What's happening that's wrong? Is it trying to read more data and thus writing beyond the end of your array?
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Yeah, if I enter past 20 characters, it gets stuck in the function. Like a infinite call to the function. Haven't tried it yet, but I think strncpy will work, since one of the parameters is the max that can be read into the buffer.

EDIT: Hmmm, no, strncpy doesn't work. Anything past 20 characters makes it so that you get stuck in the function. Seems to keep on calling it over and over. It's like cin.getline is reading past 20 characters, which is writing beyond the array size (plName[ 21 ]).
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
No matter how many times you hit enter, it just sits there with "Enter name for character (20 Characters max): " plus whatever you typed on the screen?
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
If you're wanting it to exit the function immediately after your buffer is full, without waiting for you to hit enter, I don't think that getline will do that. It guarantees that it won't write any more characters into your buffer, buf from my local MSDN docs, it says this:
Quote:
In order of testing, extraction stops:
1. At end of file.
2. After the function extracts an element that compares equal to delim, in which case the element is neither put back nor appended to the controlled sequence.
3.After the function extracts is.max_size() elements
So it keeps on reading as much as it can apparently, or until the delimiter is found ('\n' in your case). It doesn't actually quit extracting just because the count has been reached.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
After entering 20 characters, and pressing enter, it will be like:


Character Creation Process

Enter name for character (20 Characters max):
Press any key to continue. . .


and if you press any key, it just repeats the above. And I would like the user to press enter. I just don't want it to read anything past 20.
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
Is this by any chance all that the program does? Because in MSVC6, and maybe others, depending on what mode you're in, it will make an extra call to system("pause") at the end of the program, but only when you run the program from the development environment. Find the program's .exe and run it like a normal executable, not from the development environment, and it shouldn't have an extra "Press any key to continue...".
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Also make sure you null terminate you string, if you enter 20 characters exactly, then no null terminater will be placed in the string, and will not work right.
Its not a bug, its a feature!!
Quote:Original post by CyberJay82
Also make sure you null terminate you string, if you enter 20 characters exactly, then no null terminater will be placed in the string, and will not work right.
Actually, according to the docs, the getline() will make sure that only up to 19 characters are read, and a '\0' will be placed at the end. But good thing to be aware of nonetheless.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement