Color Class Program

Hey guys, I have to write a program:

"Program 4 - Pick color Class
-Create a class that randomly pick a color from an array of colors: string colors[7];
-Have the colors array and all functions defined in the class.
-Create and use a void setElement( index, "color") function to assign values to the array elements 0 to 6: red, orange, yellow, green, blue, indigo, violet.
-Have a function that prints out all the colors in the array.

Can anyone tell me what Im doing wrong? Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
#include <iostream>
#include <string>
#include <time.h>


using namespace std;

class ColorPicker
{
    public:
    string colors;
    void printColors (string myColors[], int sizeOfArray)
    {
        colors = myColors[];
    }
    string getColors(void)
    {
        return colors;
    }
    
    private:
    
        string myColors[7] = {"Red", "Yellow", "Orange", "Purple","Green",    "Violet", "Blue"};
    
    
    
    
    
};

int main()
{
    ColorPicker myColor1;
    
    myColor1.colors();
    
    cout << colors[0,1,2,3,4,5,6] << endl; //Is there anyway to cout colors using a loop?

}
Last edited on
Here is some help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
// not being used.
//#include <string>
//#include <time.h>

using namespace std;
class ColorPicker {
public:
    ColorPicker() {
        myColors.push_back ("Blue");
        myColors.push_back ("Red");
        myColors.push_back ("Yellow");
    }

    const vector<string> &get_myColors() const {
        return myColors;
    }

    void addColor(const string &color) {
        myColors.push_back (color);
    }

    void print(){

        for(auto const color: myColors){
            cout << color << " ";
        }
        cout << endl;
    }
private:
    vector<string> myColors;

};

int main() {

    // myColor1.colors();
    // colors is not a public method (or any?) method in your class.

    ColorPicker myColorPicker;
    myColorPicker.addColor ("purple");

    // cout << colors[0,1,2,3,4,5,6] << endl;
    //
    // lots of ways.
    // Is there anyway to cout colors using a loop?

    myColorPicker.print();
    return 0;
}


let us know if you have any questions!
Last edited on
1
2
3
4
5
6
    public:
    string colors;
    void printColors (string myColors[], int sizeOfArray)
    {
        colors = myColors[];
    }

Here you try to assign value of array of strings to a single string You can assign single element from array of strings to a single string but not a whole array. For example colors = myColors[2];

On line 36 you are calling .colors() as if it was a member function but its just data member so no () are necessary. But that line even if written without () doesn't do anything anyway.

To display elements in an array you have to work with one element at a time but do that in the loop as you said yourself
1
2
3
4
for(int i = 0; i < array_size; ++i)
{
    std::cout << array_of_colors[i] << ' ';
}

1. colors on line 38 isn't defined anywhere when you are trying to use it (check out your main function).
2. Even if myColor1.colors were intended to use instead of colors you would be subscripting a single string anyways not an array of strings so you would get only one character out of that string.
For example std::string str{"Hello!"}; std::cout << str[0] << std::endl;
would give you
H

3. Even if it was an array and actually defined at a time you use it on line 38 you still cant access all those elements at once and print them out. Use for loop as I showed you
Last edited on
Ahhhh caught the error! Thanks guys much appreciated.
Topic archived. No new replies allowed.