Array of Objects question

I am attempting to make a program that calculates the value of a resistor just by inputting the colors of each individual band. I want the user to be able to input the entire word and thought this could be accomplished by creating a color class that contains both the color and its value. I have a few questions.

First, is this...
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
class Value
{
    public:
        char color[128];
        int colorvalue;
};

Value black;
black.color = 'black';
black.colorvalue = 0;

Value brown;
brown.color = 'brown';
brown.colorvalue = 1;

Value red;
red.color = 'red';
red.colorvalue = 2;

Value orange;
orange.color = 'orange';
orange.colorvalue = 3;

Value yellow;
yellow.color = 'yellow';
yellow.colorvalue = 4;

Value green;
green.color = 'green';
green.colorvalue = 5;

Value blue;
blue.color = 'blue';
blue.colorvalue = 6;

Value violet;
violet.color = 'violet'
violet.colorvalue = 7;

Value grey;
grey.color = 'grey';
grey.colorvalue = 8;

Value white;
white.color = 'white';
white.colorvalue = 9;


the same as this...
1
2
3
4
5
6
7
8
9
10
Value band1[10] = {{ "black", 0},
                   { "brown", 1},
                   {   "red", 2},
                   {"orange", 3},
                   {"yellow", 4},
                   { "green", 5},
                   {  "blue", 6},
                   {"violet", 7},
                   {  "grey", 8},
                   { "white", 9}};


and is it possible to give the user the option of inputting any particular color and then taking the integer of that object and utilizing that in some other task. I hope I have made sense here and am grateful for any help or correction provided.
closed account (zb0S216C)
Cube Code wrote:
the same as this...

Yes it is.

Cube Code wrote:
and is it possible to give the user the option of inputting any particular color and then taking the integer of that object and utilizing that in some other task

Hmmm... you lost me. Do you mean let the user enter their own string literal for Value::color and an integer for Value::colorvalue? or do you mean something else?

Wazzak
Last edited on
Almost, the first code snippet won't compile as there are a few errors such as single quotes instead of double quotes and the assignments (under VS2008).

You may want to consider enum's rather than a class as it will assign it a numerical value automatically and you can use it as a integer.

You can find it at the bottom of this page: http://www.cplusplus.com/doc/tutorial/other_data_types/ .

enum Value { black, brown, red, ... };
Black would equal 0, brown would equal 1, red would equal 2, and so on.

Although I suppose if you wanted to ask the user enter a specific colour e.g. "Yellow" then you would need to convert the string into it's numerical form to compare to your enumerated data type.

I hope this helps!
I want to be able to have this
1
2
3
4
5
6
7
8
9
10
Value band1[10] = {{ "black", 0},
                   { "brown", 1},
                   {   "red", 2},
                   {"orange", 3},
                   {"yellow", 4},
                   { "green", 5},
                   {  "blue", 6},
                   {"violet", 7},
                   {  "grey", 8},
                   { "white", 9}};

for every band of the resistor (slightly different for the tolerance). I want to know how (if) I can have the user input Value band1[10] but, not the information. Is it possible to somehow have the user input say "yellow" and then find yellow and do something with 4?
I f I were to use enum's how would I go about converting the string into its numerical form? More importantly though(which was one of my questions with my other method) could I make so when the user is prompted to enter a color, they could enter any possible color into the terminal? I can not figure out how to enable multiple possible inputs.
Last edited on
Take a string input from the user, then compare that user given string against Value::color.

All the methods you need are here: http://www.cplusplus.com/reference/string/string/ .

If you were to use enumerations then you would need to convert the string given by the user, e.g. "Yellow" (or yellow, or YELLOW), to 4 to compare it against your enumerated type.

You could do this by having a function that compares it against all different colours (and all their possible variations) and when the correct string has been found, to use it's numerical value when dealing with your enumerated type.

I'm not sure if this approach is better, but I'll leave that up to you.
Thanks for the replies, they were helpful.
Topic archived. No new replies allowed.