Color
Color
n'est pas une seule chaîne, c'est un tableau de chaînes, donc vous devez faire une boucle pour imprimer chacune d'elles. De plus, vous devez trouver un bon endroit pour apprendre le C++, parce que les choses que vous faites est de C.
<string.h>
est pour les chaînes C, <string>
est C++ std :: string
Le '\0'
à la fin du tableau est aussi une chose C.
const char * pointers[] = {"HELLO","WORD", '\0' }; // the third element is a NULL pointer (in C++11 now you can use nullptr)
int i = 0;
while (pointers[i] != nullptr)
std::cout << pointers[i++];
Demo
#include <iostream>
#include <string>
using namespace std;
int main() {
// You don't need to specify the size, the compiler can calculate it for you.
// Also: a single string needs a '\0' terminator. An array doesn't.
string Color[] = { "Red", "Blue", "Green", "Purple", "Yellow",
"Black", "White", "Orange", "Brown"};
for (const auto& s : Color) // There's multiple ways to loop trought an array. Currently, this is the one everyone loves.
cout << s << '\t'; // I'm guessing you want them right next to eachother ('\t' is for TAB)
// In the loop &s is a direct reference to the string stored in the array.
// If you modify s, your array will be modified as well.
// That's why if you are not going to modify s, it's a good practice to make it const.
cout << endl; // endl adds a new line
for (int i = 0; i < sizeof(Color)/sizeof(string); i++) // Old way.
cout << Color[i] << '\t';
cout << endl;
for (int i = 3; i < 6; i++) // Sometimes u just want to loop trough X elements
cout << Color[i] << '\t';
cout << endl;
return 0;
}