Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im having some trouble with an Array in C++. See I want to let the user give the program an input in form of a String, and keep doing it until the user is satisfied. My input is working fine but when i want to store the strings in to an array im running in to some problems. I have to define a size for my array apparently? and is there a way to store the input in 2 or 3 different arrays (depending on the input, which i sort with some if-statements) of strings, and the print them out? My code looks something like this now..

string firstarray[10];
string secarray[10];

//The cin stuff here and reading strings from user-input

    if(MyCondition1){ 
for(int x = 0; x<=9;x++){ 
firstarray[x] = name;  
}

  if(MyCondition2){ 
    for(int x = 0; x<=9;x++){ 
    secarray[x] = name;  
    }

Is there a way to skip the 10-limit of an array? could it be like string

firstarray[];

?

share|improve this question
1  
What is the problem you are trying to solve with multiple arrays? A vector<string> firstArray; gets rid of the size constraint. – Captain Giraffe Feb 28 '12 at 14:40
You do mean std::string, right? I mean, I don't see a using namespace anywhere... – Mr Lister Feb 28 '12 at 14:40
Well he's used string without std:: in the question. – BoBTFish Feb 28 '12 at 14:43
didn't use std::, a bit new to c++ .. – Handsken Feb 28 '12 at 14:45

2 Answers

up vote 8 down vote accepted

You're looking for a std::list. Or better, a std::vector which lets you access elements by their position.

Both of them can be expanded dynamically.

using namespace std;

// looks like this:
vector<string> firstvector;

firstvector.push_back(somestring); // appends somestring to the end of the vector

cout << firstvector[someindex]; // gets the string at position someindex
cout << firstvector.back(); // gets the last element

About your second question:
You can of course create several arrays / vectors to put your strings in. Maybe even use a std::map of type map<key, vector<string>> where key can be an enum for the category (or a string, but enum is better).

You put a new value into one of the vectors:

tCategoryEnum category = eCategoryNone;
switch(condition)
{
  case MyCondition1:
    category = eCategory1;
    break;
  case MyCondition2:
    category = eCategory2;
    break;
  // ...
}
// check if a category was found:
if(category != eCategoryNone)
{
    categoryMap[category].push_back(name);
}

Then to output this, you can simply loop over each category and vector element

for(int i = 0; i < categoryMap.size(); i++)
  for(int j = 0; j < categoryMap[i].size(); j++)
    cout << categoryMap[i][j];
share|improve this answer

Have you considered using an std::vector<string> > ?

share|improve this answer
i have encounterd it in some google-result. But i thought that it was to "over do it", guess i was wrong. Can some one link a how-to? – Handsken Feb 28 '12 at 14:44
1  
@Handsken no way is this overdoing it, vectors are simpler to use than arrays. Look at Martin's example it should get you on the right track. – Motti Feb 28 '12 at 14:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.