Hi,

I am almost done with C++ and my project is on Resistors. The problem is that when i try to tun the program, line 37 and 34 has an error saying "no match operator". It is the same as line 28 and 31. Then when I make line 34 and 37 a comment it works but, the program acts in a different way. When asked for color for "band A", i typed in a correct color but when it goes to the member function "checker", it says that the inputted color for "band A" is wrong. The checker returns a correct value but i think it just ignores it or gives it a 1.


Please help me correct this project of mine.

Thank you in advance.

Here is part of my program :

class Resistance
{
      private:
                string Color[4]; 
                int Resistance, Tolerance;
      public:
            void GetData(); 
            int  Checker();
            int Option();

};

void Resistance:: GetData()
{ 

             cout << "Please enter a color for band A: ";
             cin  >> Color[0];
             cout << "Please enter a color for band B: ";
             cin  >> Color[1];
             cout << "Please enter a color for band C: ";
             cin  >> Color[2];
             cout << "Please enter a color for band D: ";
             cin  >> Color[3];
};

int Resistance:: Checker()
{
             if (Color[0] == "black" || "gold" || "silver" || !("brown" || "red" || "orange" || "yellow" || "green" || "blue" || "violet" || "gray" || "white") )
             return 1;

             if (Color[1] == "gold" || "silver" || !("brown" || "red" || "orange" || "black" || "yellow" || "green" || "blue" || "violet" || "gray" || "white") )
             return 2;  
             
             if (Color[2] == !("black" || "gold" || "silver" || "brown" || "red" || "orange" || "yellow" || "green" || "blue" || "violet" || "gray" || "white") )
             return 3;    
             
             if (Color[3] == !("black" || "gold" || "silver" || "brown" || "red" || "orange" || "yellow" || "green" || "blue" || "violet" || "gray" || "white") )
             return 4;   
};

int Resistance:: Option ()
{
    int option;
    
    cout << "Please Choose From The Fallowing Option : " << endl;
    cout << "\t1 - Convert color of bands to ohms\n\t2 - Convert ohm to color of bands" << endl;
    cout << "Enter Option Number : " ;
    cin  >> option; 
    
    return option;
    
};

int main ()
{ 
    int option, check;
    bool restart;
    
    Resistance A;
    
    option = A.Option();
    
    if (option == 1)
    {

    do
    {
    A.GetData();
    check = A.Checker();
    

            if(check == 1 ){
           cout << "Error! Invalid input in Color band A" << endl;
           restart = true;
           }
           
            if(check == 2 ){       
           cout << "Error! Invalid input in Color band B" << endl;
           restart = true;}

             if(check == 3 ){
           cout << "Error! Invalid input in Color band C" << endl;
           restart = true;}

            if(check == 4 ){
           cout << "Error! Invalid input in Color band D" << endl;
           restart = true;
           }
           
     }
     
     while (restart = true);
     }

Your if statements look very confused.

This, for example

!("black" || "gold" || "silver" || "brown" || "red" || "orange" || "yellow" || "green" || "blue" || "violet" || "gray" || "white")

gives boolean false every time, so your line

if (Color[2] == !("black" || "gold" || "silver" || "brown" || "red" || "orange" || "yellow" || "green" || "blue" || "violet" || "gray" || "white") )

is asking

if (Color[2] is false)

which is almost certainly not what you intended.


This one

if (Color[0] == "black" || "gold" || "silver" || !("brown" || "red" || "orange" || "yellow" || "green" || "blue" || "violet" || "gray" || "white") )

says

if (( Color[0] is black) or true)

Which will always come up as true, because (anything OR true) is true.

You need to go back and read about how logical comparison operators such as || work.

Edited 5 Years Ago by Moschops: n/a

my corrections:

for

if (Color[2] == !("black" . . . )

I am saying that if the string is not the word "black" or "gold" and so on.. then it should return the number i specified.

for

If(Color[0] == "black" || "gold" || "silver" || . . .

the second || should be &&

right?

Not quite. What you need to say is

if ( (colour does not equal gold) AND
(colour does not equal black) AND
(colour does not equal silver) AND
.....


Everything on either side of a logical operator is evaluated to true or false. When you say

Color[0] == "black" || "gold"

There are essentially two evaluations made before the OR operator even gets a go. Firstly, Color[0] == "black" is evaluated. May come out as true, may come out as false. Then the part on the other side is evaluated; "gold" . Comes out as true (as a rule of thumb, anything that isn't a boolean false and isn't the number zero comes out as true).

So now we have something on the left, that might be true or false, depending on what Color[0] is, and on the right we have true.

(something || true) is always going to come out to true.

If(Color[0] == "black" || "gold" || "silver" || . . . is basically
If(Color[0] == "black" || true || true || . . .

Anything OR true is always going to come out as true.

Edited 5 Years Ago by Moschops: n/a

Ah i see. I was thinking in a algebra form where if there is a !() on all of the cases i can factor it out. . silly me. haha. thank you for pointing that out.

I have a question not relating on this, is there way to use tolower() on a string? and if i use the loop to get all letters lowercase is there anyway i can assign that to a string?

ok now i found something weird. my program is not responding to my inputted color.
can you please check and see what's wrong with it?

here's my code

if (Color[0] == "brown" || Color[0] == "red" || Color[0] == "orange" || Color[0] == "yellow" || 
                 Color[0] == "green" || Color[0] == "blue" || Color[0] == "violet" || Color[0] == "gray" || 
                 Color[0] == "white" )
             return 0;
             
             else if (Color[0] == "black" || Color[0] == "gold" || Color[0] == "silver")
             return 1;
            
             if (Color[1] == "black" || Color[1] == "brown" || 
                 Color[1] == "red" || Color[1] == "orange" || Color[1] == "yellow" || Color[1] == "green" || 
                 Color[1] == "blue" || Color[1] == "violet" || Color[1] == "gray" || Color[1] == "white" )
             return 0;  
             
             else if (Color[1] == "gold" || Color[1] == "silver" )
             return 2;
            
             if (Color[2] == "black" || Color[2] == "gold" || Color[2] == "silver" || Color[2] == "brown" || 
                 Color[2] == "red" || Color[2] == "orange" || Color[2] == "yellow" || Color[2] == "green" || 
                 Color[2] == "blue" || Color[2] == "violet" || Color[2] == "gray" || Color[2] == "white" )
             return 0;    
                  
             else if (Color[2] != "black" || Color[2] != "gold" || Color[2] != "silver" || Color[2] != "brown" || 
                      Color[2] != "red" || Color[2] != "orange" || Color[2] != "yellow" || Color[2] != "green" || 
                      Color[2] != "blue" || Color[2] != "violet" || Color[2] != "gray" || Color[2] != "white" )
             return 3;
                   
             if (Color[3] == "black" || Color[3] == "gold" || Color[3] == "silver" || Color[3] == "brown" || 
                 Color[3] == "red" || Color[3] == "orange" || Color[3] == "yellow" || Color[3] == "green" || 
                 Color[3] == "blue" || Color[3] == "violet" || Color[3] == "gray" || Color[3] == "white" )
             return 0;   

             else  (Color[3] != "black" || Color[3] != "gold" || Color[3] != "silver" || Color[3] != "brown" || 
                    Color[3] != "red" || Color[3] != "orange" || Color[3] != "yellow" || Color[3] != "green" || 
                    Color[3] != "blue" || Color[3] != "violet" || Color[3] != "gray" || Color[3] != "white" );
             return 4;

it suppose to give me a return value corresponding which error the user typed it in. For example, if the user typed in a color not in the "Color[2]" it should give me a return value of 3 and give me a warning.it only worked for "Color[0]" others doesn't.

Hi i created a code in vba. the requirements is that
-input is 3 color bands from a resistor(in order)
-and the output of the resistor in ohms
-make function work for 3 colors.I am using vba. I came up with a code but it has my function highlighted in yellow. whats wrong with it?
'Resistor By: Diamond Wongus
Function Resistor(color1, color2, color3)

'I cannot put the equation until the end
Select Case color1
Case "black", "Black"
color1 = 0
Case "brown", "Brown"
color1 = 1
Case "Red", "red"
color1 = 2
Case "Orange", "orange"
color1 = 3
Case "Yellow", "yellow"
color1 = 4
Case "Green", "green"
color1 = 5
Case "Blue", "blue"
color1 = 6
Case "Violet", "violet", "Purple", "purple"
color1 = 7
Case "Grey", "grey", "Gray", "gray"
color1 = 8
Case "White", "white"
color1 = 9
Case "Gold", "gold"
color1 = 0.1
Case "Silver""silver"
color1 0.001
'the above information is for color 1
Select Case color2
Case "black", "Black"

color2 = 0
Case "brown", "Brown"
color2 = 1
Case "Red", "red"
color2 = 2
Case "Orange", "orange"
color2 = 3
Case "Yellow", "yellow"
color2 = 4
Case "Green", "green"
color2 = 5
Case "Blue", "blue"
color2 = 6
Case "Violet", "violet", "Purple", "purple"
color2 = 7
Case "Grey", "grey", "Gray", "gray"
color2 = 8
Case "White", "white"
color2 = 9
Case "Gold", "gold"
color2 = 0.1
Case "Silver""silver"
color2 0.001
'the above data is color 2
Select Case color3

Case "black", "Black"
color3 = 0
Case "brown", "Brown"
color3 = 1
Case "Red", "red"
color3 = 2
Case "Orange", "orange"
color3 = 3
Case "Yellow", "yellow"
color3 = 4
Case "Green", "green"
color3 = 5
Case "Blue", "blue"
color3 = 6
Case "Violet", "violet", "Purple", "purple"
color3 = 7
Case "Grey", "grey", "Gray", "gray"
color3 = 8
Case "White", "white"
color3 = 9
Case "Gold", "gold"
color1 = 0.1
Case "Silver""silver"
color3 0.001
'The above data is color 3
End Select
Resistor = (color1 * 10 + color2) * (10 ^ color3)


End Function

This article has been dead for over six months. Start a new discussion instead.