Jump to content
  • Advertisement
Sign in to follow this  
TheOne1

limiting characters into buffer

This topic is 5106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
cin.getline( buf, 20 ) should work.
strncpy() also makes a good replacement for strcpy() (idem with sprintf()/snprintf(), etc)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 ]).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...".

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!