Am I allocating this right, and what is my instructor talking about?

Okay. Working on assignments here, trying to get them done...and I dunno if I am allocating this memory correctly. Note I am in a C++ class, so the programs we are given as homework require specific things...-but this particular thing is boggling me a bit, so I want to confirm whether or not I am doing this right:

The program already has the following inside of the main:
 
string * names


He tells me to allocate memory for the array with the new operator. So I do this.

names = new array[cellsUsed]

Note, cellsUsed is declared and set by the user before "string * names" is declared.

Is this correct or not?

Then I come to this...

We are using functions from an earlier exercise and one new function for this one. He says in a note on the function's prototype, in a comment. The functions name is inputArray.

"Note: On input, the space character ends a name."

This confuses me because I am going to use a for loop to do the input. Which is currently structured like this (I typed this myself)

1
2
3
4
for(int i = 0; i <= cellsUsed; i++)
{
in >> data[i]; //The function uses "istream & in" in the function parameters.
}


I think I'm missing something here, but I don't know what that is.

Any ideas what I may be missing?
It should be
names = new string[cellsUsed];

And, yes, cin does stop reading at a space character if you do something like
cin >> myString; // assuming myString is a std::string

The for loop doesn't make a difference.
For instance, say you have this:
1
2
3
string blah[5];
for (int i = 0; i < 5; ++i)
    cin >> blah[i];

If you type "Hi there, how are you?" as input, it will read "Hi" into blah[0], "there," into blah[1], "how" into blah[2], "are" into blah[3], and "you?" into blah[4].

Does this make sense?
Yes it does. Thank you very much. Now I can finish this exercise.
Topic archived. No new replies allowed.