1

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[];

?

4
  • 1
    What is the problem you are trying to solve with multiple arrays? A vector<string> firstArray; gets rid of the size constraint. Commented Feb 28, 2012 at 14:40
  • You do mean std::string, right? I mean, I don't see a using namespace anywhere... Commented Feb 28, 2012 at 14:40
  • Well he's used string without std:: in the question. Commented Feb 28, 2012 at 14:43
  • didn't use std::, a bit new to c++ .. Commented Feb 28, 2012 at 14:45

2 Answers 2

8

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];
Sign up to request clarification or add additional context in comments.

Comments

5

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

2 Comments

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 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.

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.