0

I am trying to use a set method to apply 7 colors to an array in c++. I am not entirely sure how to use the setElement method in my main function to help me append colors to the indices in my array, as I cannot find any help online or in my book. So far, I have:


#include <iostream>
#include <string>

using namespace std;

class randColor 
{
    private:

        string colors[7];

    public: 

    void setElement(index, "color")
    {
        colors[] = index; 
        index = "color";
    }

    void printColor()
    {
        int i = 0;

        for(i = 0; i <= 7; i++)
        {
            cout << colors[i] << endl;
        }
    }
};

int main()
{
    randColor RandomOne;

    RandomOne.setElement(index, {"red", "orange", "yellow", "blue", "green", "indigo", "violet"});
    RandomOne.printColor();

    return 0;
}

evanparial
  • 45
  • 2
  • 7
  • 2
    You cannot "append" to a statically sized array. Those arrays have a fixed size that doesn't change at rntime – UnholySheep Sep 10 '20 at 18:29
  • 1
    `for(i = 0; i <= 7; i++)` is an off by 1 error. With that said you should avoid magic number programming. Related: [https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) – drescherjm Sep 10 '20 at 18:29
  • 1
    `colors[] = index;` not sure what you were trying to do here but its not valid `c++` – drescherjm Sep 10 '20 at 18:33
  • 1
    `void setElement(index, "color")` You have to give your variables types. With that said the second parameter is a string literal and not even a variable name. – drescherjm Sep 10 '20 at 18:34
  • `index` seems to be some variable not defined in the code – drescherjm Sep 10 '20 at 18:37
  • 1
    Maybe you want to change `string colors[7];` to `std::vector colors;` that would allow you to add colors 1 at a time to the vector and you could actually put more than 7 colors in your class. – drescherjm Sep 10 '20 at 18:42
  • You're trying (although incorrectly) to use a compound literal as the function argument. That's a C feature, but it doesn't exist in C++. – Barmar Sep 10 '20 at 19:23
  • @UnholySheep sorry, I meant add lol – evanparial Sep 10 '20 at 19:27

3 Answers3

2

setElement() is for setting just one element, you can't give it an array.

You need to declare the types of the function parameters, and they shouldn't be in quotes.

    void setElement(unsigned int index, std::string color)
    {
        colors[index] = color;
    }

If you want to set all the elements, you need to call it in a loop.

std::string rainbow[] = {"red", "orange", "yellow", "blue", "green", "indigo", "violet"};
for (int i = 0; i < sizeof rainbow/sizeof *rainbow; i++) {
    RandomOne.setElement(i, rainbow[i]);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    FYI that you can replace `sizeof rainbow/sizeof *rainbow` with `std::size(rainbow)` in C++17 and later (Visual Studio also has [`_countof()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/countof-macro?view=vs-2019) available). Or you can write your own `size()` function in earlier versions: `template size_t size(const T (&)[N]) { return N; }` – Remy Lebeau Sep 10 '20 at 19:35
2

Your setElement() method is implemented all wrong. It needs to look like this instead:

void setElement(int index, const string &color)
{
    colors[index] = color;
}

Also, your printColor() method is going out of bounds of the array. The loop needs to use < instead of <=:

for(i = 0; i < 7; i++)

And then, your main() should look like this:

int main()
{
    randColor RandomOne;

    RandomOne.setElement(0, "red");
    RandomOne.setElement(1, "orange");
    RandomOne.setElement(2, "yellow");
    RandomOne.setElement(3, "blue");
    RandomOne.setElement(4, "green");
    RandomOne.setElement(5, "indigo");
    RandomOne.setElement(6, "violet");

    RandomOne.printColor();

    return 0;
}

Alternatively:

int main()
{
    randColor RandomOne;

    const string colors[] = {"red", "orange", "yellow", "blue", "green", "indigo", "violet"};
    for(int i = 0; i < 7; ++i) {
        RandomOne.setElement(i, colors[i]);
    }

    RandomOne.printColor();

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

In case you want to keep the setElement() function call in the main() function as is you could use an std::initializer_list, although I personally wouldn't recommend it in this case.

#include <iostream>
#include <string>
#include <initializer_list>

using namespace std;

class randColor
{
private:

    string colors[7];

public:

    void setElement(int index, initializer_list<string> cols)
    {
        int i = 0;
        for (auto it = cols.begin(); ++it; it != cols.end()) {
            if (i++ == index) {
                colors[i] = *it;
                break;
            }
        }
    }

    void printColor()
    {
        int i = 0;

        for (i = 0; i < sizeof(colors) / sizeof(string); i++)
        {
            cout << colors[i] << endl;
        }
    }
};

int main()
{
    randColor RandomOne;

    int index = 5;
    RandomOne.setElement(index, { "red", "orange", "yellow", "blue", "green", "indigo", "violet" });
    RandomOne.printColor();

    return 0;
}