For loop initial run

Need some help with C++. I’m trying to create a function where you simply add a user-specified amount of strings to a vector then simply print the strings, both using functions. So far I have

#include <iostream>

#include <vector>

using namespace std;

void print(vector <string> );

void insert(vector <string>);

int main() {

    vector <string> vec {};




    return 0;


void insert(vector <string> v){

     cout << "How many elements? ";

    int elements{};

    cin >> elements;

    for(int i{0}; i <= elements; i++){

        cout << "Enter string " << i << ": ";

        string e{""};

        getline(cin, e);





void print(vector <string> v){

    for(auto s: v)

        cout << s << " ";


When I call the “insert” function, the first cout line runs twice before the chance to insert a string. The console returns:

How many elements? 2

Enter string 0: Enter string 1:

Why is this? shouldn’t it ask for input before “Enter string 1”? Also, the print function to then display the added strings doesn’t seem to be working either.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.