Setting Variables equal to Words

I'm writing a program that spits out a 4 band color code based off a users imputed resistance. What I'm struggling with is how to set color phrases to variables. If this sounds vague or confusing please let me know, but this is just the piece of code that I can't do right :/

When I run it, the program displays nothing instead of the color phrases. For example, when the user imputs in 23000, the first 2 band colors should be Red & Orange, but like I said, the program displays nothing.

I'm only posting the part where I'm having trouble with, unless you guys think I should post the whole program.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    string Color1, Color2, Color3, Color4;
    string Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Grey, White, Gold, Silver;

    // First band Color
    if (firSIG == 0)
    {
        Color1 = Black;
    }
    else if (firSIG == 1)
    {
        Color1 = Brown;
    }
    else if (firSIG == 2)
    {
        Color1 = Red;
    }
    else if (firSIG == 3)
    {
        Color1 = Orange;
    }
    else if (firSIG == 4)
    {
        Color1 = Yellow;
    }
    else if (firSIG == 5)
    {
        Color1 = Green;
    }
    else if (firSIG == 6)
    {
        Color1 = Blue;
    }
    else if (firSIG == 7)
    {
        Color1 = Violet;
    }
    else if (firSIG == 8)
    {
        Color1 = Grey;
    }
    else if (firSIG == 9)
    {
        Color1 = White;
    }

    cout << Color1 << endl;

    // Second band color
    if (secSIG == 0)
    {
        Color2 = Black;
    }
    else if (secSIG == 1)
    {
        Color2 = Brown;
    }
    else if (secSIG == 2)
    {
        Color2 = Red;
    }
    else if (secSIG == 3)
    {
        Color2 = Orange;
    }
    else if (secSIG == 4)
    {
        Color2 = Yellow;
    }
    else if (secSIG == 5)
    {
        Color2 = Green;
    }
    else if (secSIG == 6)
    {
        Color2 = Blue;
    }
    else if (secSIG == 7)
    {
        Color2 = Violet;
    }
    else if (secSIG == 8)
    {
        Color2 = Grey;
    }
    else if (secSIG == 9)
    {
        Color2 = White;
    }
Could you post the rest of the code?
closed account (o3hC5Di1)
Hi there,

You declare the colour variables, but you don't actually assign any value (text) to them. Therefore they remain empty, and nothing can be shown:

1
2
3
string Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Grey, White, Gold, Silver;

string Black("Black"), Brown("Brown"), etc. ;


Also, in cases like this it's sometimes more readable to use a switch-statement. You could even wrap this in a function since the checks are the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::string get_colour(int SIG)
{
    string Black("Black"), Brown("Brown"), etc. ;

    switch(SIG)
    {
        case 1:
            return Black;
            break; //not necessary strictly speaking as you are returning

        case 2:
            return Red;
            break;

        //etc.
    }
}

std::string first_colour = get_colour(firSIG);
std::string second_colour = get_colour(secSIG);


Let us know if you need any further help.

All the best,
NwN
Thanks for your help. I actually figured out my mistake and did it a different way, but I really do appreciate your help.

Cheers m8s
Topic archived. No new replies allowed.