I am struct to a very basic question. I want to create dynamically an array of string in c++.
How can I do that ?
This is my attempt:
#include <iostream>
#include <string>
int main(){
unsigned int wordsCollection = 6;
unsigned int length = 6;
std::string *collection = new std::string[wordsCollection];
for(unsigned int i = 0; i < wordsCollection; ++i){
std::cin>>wordsCollection[i];
}
return 0;
}
But it giving the following error
error C2109: subscript requires array or pointer type
What's the error ?
And also if I'am getting input number from user, from std::cin can I create an array of that size statically ?
{}button; this will make your code show up properly in the question.wordsCollection[i]instead ofcollection[i]. You can't use a size obtained dynamically (e.g. throughstd::cin) to create a static array. Also, the array is leaking since you never release it.