I'm trying to make an array that hold multiple user input strings, I've tired structs and dynamic arrays and it does not work
I tried making a struct that holds a string, and tried making array of struct and it didn't work
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main()
{
int rows;
cin >> rows;
string **arr = new string*[rows];
for( int i = 0; i < rows; ++i)
{
arr[i]= new string[1];
}
for(int i = 0; i < rows; ++i)
{
getline(cin, arr[i][0]);
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
for(int i = 0; i < rows; ++i)
{
cout << arr[i][0] << '\n';
}
for( int i = 0; i < rows; ++i)
{
delete [] arr[i];
}
delete [] arr;
return 0;
}
it allows rows + 1 inputs then crashes
std::vector<std::string>.std::string, you should usestd::vector<std::string>as stated earlier. For this code, however,arrshould bestd::string *arr = new std::string[rows];, all referrals toarr[i][0]should be simplyarr[i], and the last for loop should be completely thrown out.