String comapre problem


#1

I have a code like this why the notename.compare© alwasys one?
I use D4 is still say notename.compare© is true

        String::String notename = MidiMessage::getMidiNoteName( midiNoteNumber, 0, 0, 0);
  
        
        if (notename.compare(C)){
        flag=1;
        }else flag=0;
        int temp =notename.compare(C);
      
        if (notename.compare(G)){
            flag=1;
        }else flag=0;
        
        if (flag==1){
            note=note+c1[0];
            
        }

pls ,help thank you


#2

From the manual:

Case-sensitive comparison with another string.

Returns
0 if the two strings are identical; negative if this string comes before the other one alphabetically, or positive if it comes after it.

i.e. identical == 0 -> casted to bool means false
not identical != 0 -> casted to bool means true

If you use compare just for equality, you should write:

if (notename.compare(C) != 0){
//...

HTH

…also, I am assuming that C is a string defined as String C (“C”); right? otherwise write compare (“C”), does the same


#3

It still not working notename.compare© is true


#4
    String::String C={"C"};
    String::String G={"G"};

#5

My bad, I meant

if (notename.compare(C) == 0){

is the case, if they are equal, sorry :wink:


#6

Thank you, the problem solved


#7

You might want to invest a bit of time learning basic C++ syntax - I’m amazed that the compiler would accept something like that.