need help on homework

1.Store the resistor colors in an array
2.Ask the user to enter three colors
3.Use a loop to find their colors in the array and convert them to their number equivalents
4.Calculate and print out the resistance value for the colors they entered
This is my homework. I can't figure out how to do it. I stored the colors in an array and asked the user to enter three colors,but i can't figure how to convert them to their number equivalent and calculate them use the formula R = (10 a+b) *10c. The code looks repetitive because we haven't learn the function. I am not expecting an answer but some hint and advise. Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string colors[] = {"Black", "Brown", "Red", "Orange", "Yellow",
        "Green", "Blue", "Violet", "Gray", "White"};
   string color1,color2,color3;
	while (color1 != "exit") {
        cout << "Please enter colors1 :";
        cin >> color1;
		  cout << "Please enter colors2 :";
        cin >> color2;
		  cout << "Please enter colors3 :";
        cin >> color3;
		for (int i=0; i<10; i++) {
            if (colors[i] == color1) {
				cout <<i << endl;}}
		for (int i=0; i<10; i++) {
            if (colors[i] == color2) {
				cout <<i << endl;}}
		for (int i=0; i<10; i++) {
            if (colors[i] == color3) {
				cout <<i << endl;}}
	}
closed account (48T7M4Gy)
The aim is to
1. find the 3 values a,b and c so they can be substituted in the formula for R
2. display R

So once the colours are recorded the program has to find its corresponding number.

colors[2] is "Red" which means "Red = 2 and 2 can be put in the formula and so on.

If you had "Red" then "Brown" then "Violet" then a = 2, b = 1, c = 7. R= (10x2 +1) x 10x7

Create integers c1, c2 and c3 to represent the color values. Then use your loops to lookup the color:
1
2
3
for (c1=0; c1 < 10 && color1 != colors[c1]; ++c1) {
    ;
}

The empty statement enclosed in braces probably looks strange. They are there to make it really REALLY clear that there is nothing happening in the loop. Coding it like this:
for (c1=0; c1 < 10 && color1 != colors[c1]; ++c1);
is functionally the same but it's way too easy for a person to miss the semicolon and get confused.
Topic archived. No new replies allowed.