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");
}
limiting characters into buffer
cin.getline( buf, 20 ) should work.
strncpy() also makes a good replacement for strcpy() (idem with sprintf()/snprintf(), etc)
strncpy() also makes a good replacement for strcpy() (idem with sprintf()/snprintf(), etc)
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?
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 ]).
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 ]).
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?
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: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.
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
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.
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.
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...".
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.
Quote:Original post by CyberJay82Actually, 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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement