entering strings dynamically

Hi,

I am trying to enter strings dynamically. Suppose i enter 2 strings, it is accepting only 1 string. can anyone solve the problem

#include <iostream>
#include <string>
using namespace std;

int main()
{
int emp;
cout<<"enter no of emp";
cin>>emp;
string str2;
string* arr1=new string[emp];
for(int i=0;i<emp;i++)
{
cout<<"enter name";
getline(cin,str2);
arr1[i].append(str2);
}
for(int k=0;k<emp;k++)
cout<<arr1[k];
return 0;
}
1
2
3
4
5
6
7
8
for ( int i=0; i<emp; i++ )
{
   string str2;
   cout << "enter name";

   getline( cin, str2 );
   arr1[i] = str2;
}


Instead of the dynamically allocated array it is better to use standard container std::vector


1
2
3
4
5
6
7
8
9
10
vector<string> v;

for ( int i=0; i<emp; i++ )
{
   string str2;
   cout << "enter name";

   getline( cin, str2 );
   v.push_back( str2 );
}


Last edited on
Topic archived. No new replies allowed.