0

I keep seeing similar questions to mine, however, I can't seem to find one that helps my situation. Honestly, it seems like such a mundane question, I shouldn't be asking it, but here I am 2 weeks latter, still with no answer.

{
    string word;
    ArrayWithWords[d] = word;
    d++;
}

Every time this loop runs, I want to put word in position d of the array. Other examples I've found only turn the string into char*.

The array will be used more than once and having a solid value, if that's what it's called, is far more preferred. I'd like to avoid using a pointer.

1
  • 1
    Can you clarify how ArrayWithWords was declared? Also, are you running into an error or anything? What kind of loop are you using? Commented Mar 30, 2017 at 3:10

2 Answers 2

2

Just use a vector of strings.

#include <string>
#include <vector>

int main()
{
  std::vector<std::string> ArrayWithWords(10);
  size_t d = 5; // something between 0 and 9

  std::string word;
  ArrayWithWords[d] = word;
  d++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

...Completely forgot about those... Even have some code using them... Thank you.
1

Pretty much the same thing that was just posted but a little bit more old school.

#include <string>

using namespace std;

int main()
{
  string stringArray[10];

  string word;
  word = "hello";

  for (int i = 0; i < 10; i++)
  {
      stringArray[i] = string(word);
  }
}

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.