fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main() {
  5. // You don't need to specify the size, the compiler can calculate it for you.
  6. // Also: a single string needs a '\0' terminator. An array doesn't.
  7. string Color[] = { "Red", "Blue", "Green", "Purple", "Yellow",
  8. "Black", "White", "Orange", "Brown"};
  9.  
  10. for (auto& s : Color) // There's multiple ways to loop trought an array. Currently, this is the one everyone loves.
  11. cout << s << '\t'; // I'm guessing you want them right next to eachother ('\t' is for TAB
  12.  
  13. cout << endl; // endl adds a new line
  14.  
  15. for (int i = 0; i < sizeof(Color) / sizeof(string); i++) // Old way.
  16. cout << Color[i] << '\t';
  17. cout << endl;
  18.  
  19. for (int i = 3; i < 6; i++) // Sometimes u just want to loop trough X elements
  20. cout << Color[i] << '\t';
  21. cout << endl;
  22.  
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Red	Blue	Green	Purple	Yellow	Black	White	Orange	Brown	
Red	Blue	Green	Purple	Yellow	Black	White	Orange	Brown	
Purple	Yellow	Black