Ankit Ahuja
Ankit Ahuja

Reputation: 73

expression must have a constant value

I am trying to create an array out of a string input.

string input;
getline(cin,input);
string inputarray1[100];
istringstream pp(input);
int* inputPosition=0;
while (!pp.eof())
{
    getline( pp, inputarray1[*inputPosition], ' ' );
    inputPosition++;
}
int* a = inputPosition;
string halp[a];

I am using getline to parse my input (along with an istringstream) and placing that into an array, but how can I create an array that has no extra empty locations?

Upvotes: 0

Views: 1495

Answers (2)

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

Use a vector, from the header <vector>

vector<string> inputArray;
while (getline(pp, input, ' '))
    inputArray.push_back(input);

The number of strings can be obtained with inputArray.size(), and you can access individual elements just like with an array, inputArray[index].

Note that operator>> is delimited on whitespace, so you can probably also do this(unless you for some reason want to treat tabs differently)

while (pp >> input)
    inputArray.push_back(input);

Upvotes: 2

Oleksi
Oleksi

Reputation: 13097

I'm not 100% clear on your question, but it sounds like you really want to use a Hash Table instead of an array. This will let you map user inputs to something else, without the empty array locations you mentioned.

Upvotes: 0

Related Questions