using getline() in for loop for n times, but will let me enter n-1 times

hello, i'm trying to write a code for entering a number(n) of strings and then fill a vector with those strings, but will let me enter n-1 times using for loop. is this because of the way getline() works? because with cin>>, it works perfectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>

using namespace std;

int main() {

	cout << "enter the number of strings:\n";

	int n;

	cin >> n;

	vector<string> v;

	cout << "enter strings " << n << "times:\n";

	for (int i = 0; i < n; i++) {
		string t;
		getline(cin, t);
		v.push_back(t);
	}

	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << endl;
	}

	cout << endl;

	system("pause");
}
it ran n times: print v.size() if you don't see it.

The first time it ran, it consumed the rest of the user input that was waiting to be consumed after "cin << n;", most likely a single newline character, returning an empty string.
Many thanks, I understand now :)
Topic archived. No new replies allowed.