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.

and

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

and

//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.

Thanks

Recommended Answers

All 3 Replies

>> 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.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.