I am struct to a very basic question. i want to create dynamically an array of string in c++.
How can i do that
#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"
wats the error?
And also if 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. – Mark Ransom Jan 20 '12 at 22:27wordsCollection[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. – R. Martinho Fernandes Jan 20 '12 at 22:30