Hello All,

Its me again with my quirky questions.

I am writing a program for homework and the idea is to sort the top 3 runners by their finishing time.

Also a requirement of the homework is to gather the names of the runners along side their times. The user should enter a name that would not overflow the array. I have:

const int iLength = 21; //20 Chars + 1 null terminator.


//3 char. variables to hold 3 user input.
char cRunner1[iLength], cRunner2[iLength], cRunner3[iLength];


//Accept the name of the runner.
cout << "Name of Runner number 1: ";
cin.getline(cRunner1, iLength);

I would like to put the last two lines in a While Loop to prevent the user from going over the 20 character limit, but I do not know how to figure out the length of the string entered.

Any help and clarification will be greatly appreciated.


8 Years
Discussion Span
Last Post by Ancient Dragon

>> would like to put the last two lines in a While Loop to prevent the user from going over >>the 20 character limit,

When you use getline() that isn't necessary because getline() will no accept more than iLength number of characters including the string null terminator.


Thanks Ancient Dragon

So I understand that getline() will limit the user's input to what I place in iLength, so iLength should be 20 or 21 to allow up to 20 characters?

In other words do I need to reserve the string null terminator's position manually or will the getline() take care of it automatically.

Thanks again.


If you want 20 characters then iLength = 21 is correct. getline() will insure that no more than 20 characers are put into the input buffer.

If you change to using c++ std::string then you don't have to worry about the length at all because std::string will expand to fit however many characters are typed from the keyboard (I think there is a limit of 32,525 or something like that, but I hardly doubt anyone is going to type that many charcters.)

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.