7

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 ?

2
  • 1
    In the future please select your code block and use the {} button; this will make your code show up properly in the question. Commented Jan 20, 2012 at 22:27
  • 1
    You typed wordsCollection[i] instead of collection[i]. You can't use a size obtained dynamically (e.g. through std::cin) to create a static array. Also, the array is leaking since you never release it. Commented Jan 20, 2012 at 22:30

6 Answers 6

11

You meant to type:

std::cin>>collection[i];

And you also need to delete[] collection (or you'll leak this memory).

It would be better use std::vector<std::string> collection; and avoid the raw pointer usage altogether:

#include <iterator>
#include <iostream>
#include <string>
#include <vector>

int main()
{
    const unsigned int wordsCollection = 6;

    std::vector<std::string> collection;
    std::string word;
    for (unsigned int i = 0; i < wordsCollection; ++i)
    {
        std::cin >> word;
        collection.push_back(word);
    }

    std::copy(collection.begin(),
              collection.end(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
}
Sign up to request clarification or add additional context in comments.

Comments

10

use std::vector<string> or std::list<string> over hand rolling it.

4 Comments

But the real problem is that wordsCollection should be collection.
Oh my God how silly i am... Any how thanks... can you tell me the answer for my second question that can i create a static array of that
I agree, but if you choose to go ahead with new std::string do not forget to delete[] collection when you are done. It is a good idea to ensure that for every new you have a delete
You can't create an array of any size where you get the size at runtime. You can create a pointer to a buffer of n elements that acts like an array but you will have to delete it. Thats one of the many reason using the stl containers are preferable.
1

I think that should be:

std::cin >> collection[i];

Comments

0

I think it's a simple typo. std::cin>>wordsCollection[i] should be std::cin>>collection[i].

Comments

0

Try the following:

#include <vector>
#include <string>
#include <iostream>

int main(int argc, char* argv[])
{
    std::vector<std::string> myStrings;
    myStrings.push_back(std::string("string1"));
    myStrings.push_back(std::string("string2"));

    std::vector<std::string>::iterator iter = myStrings.begin();
    std::vector<std::string>::iterator end = myStrings.end();
    while(iter != end)
    {
        std::cout << (*iter) << std::endl;
        ++iter;
    }
    return 0;
}

Comments

0

You're getting this error because you're trying access the elements of an int (i.e. wordsCollection), not an array of int (i.e. collection). What you should be writing is

std::cin>>collection[i]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.