How initialize a default value for string array

Hi... I am new to C++. I want to know how to initialize default value for string array.. ex:

string myArray[100];

in the for loop, the empty array should be display as "empty". But I tried in this way in constructor.

myArray[100]="empty" , but it didn't work.. please give an idea...
So you want to create an array of 100 strings, am i correct?
1
2
string myArray[ 100 ] = {
   "value1", "value2" };  // etc, etc. 

no.. it's not working... I am getting these two errors in the line

error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'

I written in constructor like, myArray[100]={"empty"};

@AngelHoof, yeah you correct...
Example program:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string myArray[100] = {"This string goes into the first element", "This string goes into the second element, and so on..."};
   cout << myArray[0]; //displays the first element of the array, in this case "This string goes into the first element."
   return 0;
}
Last edited on
If it's previously declared and you want to assign values to it, I think you might have to loop through and assign the values. Try:
1
2
3
4
5
// this will assign the string "empty" to all 100 strings in the array
for( int i = 0; i < 100; ++i )
{
    myArray[i] = "empty";
}
Topic archived. No new replies allowed.