-1

I need to create an array of different colors and the second array would be for the number of objects, so the 1st object detected would loop through all of the colors. I am having trouble getting the list of colors to appear in the Terminal box.

This is what I have to far:

#include < iostream>
#include < string.h>
using namespace std;
int main() {
    string Color[11] = { "Red", "Blue", "Green", "Purple", "Yellow", "Black", "White", "Orange", "Brown", '\0' };
    cout << Color << endl;
    return 0;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Virang Patel
  • 83
  • 2
  • 10
  • is wrong if what you need is std::string, that would be , also you declared array of size 11 but i only counted 10 values in the initializer. did you forget one? third, you cannot print an array like that. (P.S: using namespace std; is considered bad practice, get used to pre-fixing things with std::) – Borgleader Feb 12 '16 at 02:18

2 Answers2

2

1.

The correct include file is <string>. string.h is C, and this is C++.

2.

std::string objects are initialized with character strings.

'\0'

is not a character string. It's a single character. It does not belong in the initializer list.

3.

I count nine strings in your array (the spurious '\0' excluded), and not 11. The additional array elements don't hurt, but they're unnecessary.

4.

cout << Color << endl;

Color is an array. You cannot write entire arrays to std::cout, only one element, one string at a time. You need to simply iterate over the Color array, and write each individual array element to std::cout. Presumably with meaningful separators.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

Color is not a single string, it's an array of strings, therefore you have to do a loop to print each one. Also, you need to find a good place to learn C++, because the things you are doing is from C.

<string.h> is for C strings, <string> is for C++ std::string

The '\0' at the end of the array is also a C thing.

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;
}
Jts
  • 3,447
  • 1
  • 11
  • 14
  • I recommend using automatic array size: `string Color[] = { ... }`, instead of declaring an 11-element array with 9 strings in the initialiser. And maybe actually mention why you took out the null character. – paddy Feb 12 '16 at 02:21
  • Thanks! Ill try this and see what happens! – Virang Patel Feb 16 '16 at 16:46