How much characters can a user input? Would there be a null-terminator at 5th index? |
If you use the library function
getline for input to an array of characters the terminating null is automatically added at the end.
istream& istream::getline( char* str, streamsize count )
This will read everything up to and including the newline character, but will only store at most
count - 1 characters and then add the terminating null. The newline character, although read from input, is not stored. If you attempt to read more than
count - 1 characters the excess characters are discarded and not stored and the failbit on the stream is set.
In your example you could use:
getline( Name, 6 );
If you type in "Joe" then you would have Name = { 'J', 'o', 'e', '\0', '?', '?' }
If you type in "James" then you would have Name = { 'J', 'a', 'm', 'e', 's', '\0' }
If you type in "Joseph" then you would have Name = { 'J', 'o', 's', 'e', 'p', '\0' } with the final 'h' in "Joseph" dropped and the failbit of the stream set.