constexpr size_t Capacity = 15;
size_t size = 0;
std::string words [Capacity];
// put 5 values:
std::string input;
while ( size < 5 and size < Capacity and std::cin >> input ) {
words[ size++ ] = input;
}
// add another word
if ( size < Capacity ) {
words[ size++ ] = "hello";
}
// show words
for ( size_t w=0; w < size; ++w ) {
std::cout << words[ w ] << '\n';
}
Technically we do not add any strings; the array has 15 strings all the time. We simply update some strings and care only about the size first strings.
Vectors are more convenient:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
std::vector<std::string> words;
words.reserve(15); // optional
// words is still empty. 0 strings in it.
// put 5 values:
std::string input;
while ( words.size() < 5 and std::cin >> input ) {
words.emplace_back( input );
}
// add another word
words.emplace_back( "hello" );
// show words
for ( auto w : words ) {
std::cout << w << '\n';
}