Resistor Project. Help on reading in Enum Types

I'm gradually starting to understand how to start building a program to calculate the value of a 4 band resistor. However, I'm stuck on how to work with enum types. I have defined this enum type and a variable associated with it as shown below.

I'm trying to create a statement that says:

cout << "Enter the color of Band1" << endl;
cin << Band1;

This will allow me to use my formula ((Band1 * 10 + Band2) * pow(10,Band3))

I just don't know how to input and output enum types. I already defined the enum type and did cout statements like:

cout << Black << endl;

and it gives me 0 which is great cause that is what I want as my value associated with it. However, how do I get this assigned in Band1? Also is their a way I could use a loop to assign Band2, Band3, Band4 also?

Here is what I have already:

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
#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h> 
using namespace std;

int main()
{
    
    // Define a new datatype R_Colors and
    // the values associated with R_Colors.
    
    enum R_Colors {Black, Brown, Red, Orange, Yellow,
                   Green, Blue, Purple, Gray, White,
                   Gold, Silver, None};
    
    
    // Declare these variables as type R_Colors
    
    R_Colors Band1, Band2, Band3, Band4;
    
    // The values I want for each R_Colors is
    // correct from the output shown
    
    cout << Black << endl; // This outputs 0
    cout << Purple << endl; // This outputs 7
    
    // Therefore, it looks like i'm set but how do
    // i get these values assigned to Band1, etc.
    
   
    return 0;
}




Here's one way.

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
#include <iostream>
#include <string>

int main()
{
    
    // Define a new datatype R_Colors and
    // the values associated with R_Colors.
    
    enum R_Colors {Black, Brown, Red, Orange, Yellow,
                   Green, Blue, Purple, Gray, White,
                   Gold, Silver, None};
    
    const std::string R_Color_Names [] = 
    {
        "black", "brown", "red", "orange", "yellow", "green",
        "blue", "purple", "gray", "white", "gold", "silver"
    };


    R_Colors Band[4] ;
    const unsigned BandSize = sizeof(Band) / sizeof(Band[0]) ;

    for ( unsigned i=0 ;  i < BandSize ; ++i )
    {
        std::cout << "Please enter\n" ;
        for ( unsigned j=0; j < sizeof(R_Color_Names) / sizeof(R_Color_Names[0]); ++j )
            std::cout << '\t' << j << " for " << R_Color_Names[j] << '\n' ;

        int input ;
        if ( (std::cin >> input) && input >= Black && input < None )
            Band[i] = static_cast<R_Colors>(input) ;
        else
        {
            std::cerr << "ERROR: Invalid input\n" ;
            return 0 ;
        }
    }

    for ( unsigned i=0; i < BandSize ; ++i )
    {
        std::cout << "Band[" << i << "] is " << Band[i] ;
        std::cout << " (" << R_Color_Names[Band[i]] << ")\n" ;
    }
}
Topic archived. No new replies allowed.