Arrays of strings

  1. Strings can be used like any other data type.

  2. Arrays can have a string base type.

  3. For example, the array NameList declared below can store up to 20 names of any number of characters:

    string NameList[10];

  4. The array NameList can be filled as follows:

    cout << "Enter 10 names one per line:\n";
    int n;
    for (n = 0; n < 10; n++)
        getline(cin, NameList[n]);

    The user may for example type in the following:

    Osama bin Laden - a 25 million dollar man 
    George W. Bush       
    Prof. Al Davis      
    Alan (Green-back) Greenspan
    Evan Gallagher  
    Ming (the merciless) Leung      
    Pete Swanson    
    Dr. Martin Luther King, Jr.
    Mohammed Ali (born Cassius Clay)
    George Harrison, former Beatles member
    
  5. Since each name is a string, and a string can be output to the screen using cout <<, these 10 names can be written to the screen as follows:

    cout << "Here are the 10 names:\n";
    for (n = 0; n < 10; n++)
        cout << NameList[n] << endl;

    The output will appear exactly the same as the above input:
    Osama bin Laden - a 25 million dollar man 
    George W. Bush       
    Prof. Al Davis      
    Alan (Green-back) Greenspan
    Evan Gallagher  
    Ming (the merciless) Leung      
    Pete Swanson    
    Dr. Martin Luther King, Jr.
    Mohammed Ali (born Cassius Clay)
    George Harrison, former Beatles member