getline(cin, name);
Is what I use to get the string name. I'd like to make the program limit the name to eight characters or less, truncating the name if it's too long. I'm a bit of a newbie, help would be appreciated.
getline(cin, name);
Is what I use to get the string name. I'd like to make the program limit the name to eight characters or less, truncating the name if it's too long. I'm a bit of a newbie, help would be appreciated.
I'd suggest reading the whole name into the string, then using substr to get only the first 8 characters of the user input.
To my knowledge there is no way to limit the size of std::string, except maybe max_size(), which is maximum size any string can hold. Therefore, if somebody posts a string method that limits the size, I'll be enlightened, too. Barring that you could always to something like this:
char str[9];
cin.getline(str, 9);
string String(str);
now the maximimum size of String is 8, though the size and contents of String can be changed. You could make String a const string if you wish. jlou's suggestion is another way I to accomplish the same outcome.
Im not sure if this would be a good method or even if it will work couldn't you do something like input the string then check the size() and if its greater than you want erase the last element in your string
Woop?
Might not substr() cause a copy operation? Perhaps this would be more efficient:
Although, if you have fewer than 8 characters then you might end up having to allocate more memory for it or something, which wouldn't be the greatest thing either.Code:std::getline(std::cin, s); //s = s.substr(0,8); s.resize(8);
Just my 2 cents
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
This might also work.
string s;
getline(cin, s);
string limited(s, 0, 9);
limited is now a string that has, at most, up to 9 char, which are the initial chars of s (up to the first 8 char anyway) and a terminal null char. Or at least that's the way I think it works. I've never used it so I don't know if the 9 includes or excludes the null terminal char. You can check it out yourself if you want to go this route.
You don't need to worry about the null terminator with C++ strings, so you would use 8 instead of 9.Originally Posted by elad
I think resize is fine, maybe with a size check to avoid a couple of unnecessary nulls:It doesn't matter that much in a small program, but it would avoid a copy.Code:if (name.size() > 8) name.resize(8);
Although, might not the branching eliminate any performance gain we have garnered?
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
I dunno, and I don't care when it comes to one branch and 8 characters in a newbie program.
If you don't check the size, then you will add null characters to a shorter string, and since the standard string class allows embedded nulls, you end up with a different value. Any future use of the size of the string would return 8 instead of the true size in characters. Also, I would guess that the branch would be faster than copying of 8 characters in the substr.
Well, I'm sorry I haven't got back to you guys in a while...thanks for the quick response!
I used Hunter's method with resize(). I don't really care about great performace, but thank you all for the information! I learned a great deal.