Sign in to follow this  

limiting characters into buffer

This topic is 4855 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
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
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
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.


how do I null terminate the string?

Share this post


Link to post
Share on other sites
In this case, getline() will put the null-terminating '\0' at the end for you, but if you ever need to do it, just set the element immediately after your last character equal to '\0'.

char text[32];
text[0] = 'f';
text[1] = 'o';
text[2] = 'u';
text[3] = 'r';

//Null Terminate the string:
text[4] = '\0';

The '\0' at the end lets all standard functions that work with character array strings know where the string actually ends. Without it, functions (such as sending things to cout) will just keep reading characters, waiting for a '\0' to indicate that they should quit reading characters.

But like I said, in this case, it is already handled for you.

Share this post


Link to post
Share on other sites
Maybe something is wrong with my coding? I don't know, but if I run this simple program,

#include <iostream>

using std::cout;
using std::cin;

#include <cstring>

int main()
{
char string[ 100 ];

cout << "String > ";
cin.getline( string, 20 );

cout << string << endl;

return 0;
}



It will read only 20 chars, and print 20. Why doesn't this work with the other one?

Just in case this is important, void createChar( PlClient &setObj ) is a FRIEND function of class PlClient.

Share this post


Link to post
Share on other sites
Can someone test it out? When you open the program, select:
New Game -> Create Character

and when it prompts you to enter a name, type in more than 19 characters, and tell me what happens.

http://us.f2.yahoofs.com/bc/412faeb0_106b0/bc/Solitude.exe?bfNN7LBBFYMfhW4T

Share this post


Link to post
Share on other sites

This topic is 4855 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this