I want to input multi-word strings into vector "name". Using cin works fine for single word inputs. What i want is :
- Take number of string inputs from the user. for example : "Random Name"
- Store all the String value into the vector names
Here is the code i wrote
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> names;
string user;
int n;
cout << "Enter number of Users : ";
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> user;
names.push_back(user);
}
for (int i = 0; i < names.size(); i++)
{
cout << names[i] << " ";
}
}
Problem: When i use getline() instead of cin in the for loop, it omits 1 input. For example , if the user inputs - Number of Users = 3, it only takes 2 string inputs
string user[n]; // value of n given by user using cin
for (int i = 0; i < n; i++)
{
getline(cin, user[i]);
names.push_back(user[i]);
}