Thread: Limit to text string?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Limit to text string?

    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.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I'd suggest reading the whole name into the string, then using substr to get only the first 8 characters of the user input.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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?

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Might not substr() cause a copy operation? Perhaps this would be more efficient:
    Code:
    std::getline(std::cin, s);
    //s = s.substr(0,8);
    s.resize(8);
    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.

    Just my 2 cents
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by elad
    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.

    I think resize is fine, maybe with a size check to avoid a couple of unnecessary nulls:
    Code:
    if (name.size() > 8)
        name.resize(8);
    It doesn't matter that much in a small program, but it would avoid a copy.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Reading text file into a string obj
    By thetinman in forum C++ Programming
    Replies: 8
    Last Post: 09-02-2008, 11:42 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM