Setting the size of an item in a vector

Hi! I've been searching for a bit of code that will allow me to set the size of an item in a vector. This is what I've come up with - its a portion of the code in my class. Will this work? or will the size automatically resize if a larger string is typed in by a user?

void item::setItName(string itNameValue)
//copy up to 14 chars for item name
itName = itNameValue;
int length = itNameString.size();
length = (length < 14 ? length : 13);
itNameString.copy ("", length);
itName[length] = '\0'; //append null character to vendor
}//end function setitName

It looks like you making things unnecessary complicated.

itName = itNameValue; is all you need to do if you just want to give itName the value of itNameValue. The sizes will change automatically so after this itName.size() == itNameValue.size().

If you for some reason want to limit the size of itName to 13 characters you can use the resize function.
if (itName.size() > 13)
Last edited on
Thats exactly what I need to do, I seem to have a real problem complicating things. I read something about this feature in C++ that stated even if you set this, if a user types in a longer string, it will resize to that string. Is this true?

I am going to try this out, thank you so much.
Topic archived. No new replies allowed.