Sunday, July 12, 2009

How do I test for equal chars in c++? Such as names?

Say I have two chars a and b





Both have the same char value of "Dr Suess" how do I set up an if statement such as


if (a==b)





to test if they are equal or different from each other?

How do I test for equal chars in c++? Such as names?
That is NOT a character value. What you have there is a String which is an object variable type as opposed to a primitive variable type. While I have never taken c++ I have taken other high level languages, and there should be a method such as compareTo(String s) or equals(String s)provided by the string class to compare these values.





Thus the code would be similar to this:





String a = "DR. Suess";


String b = "DR. Suess";





if(a.equals(b) == true){


//do something


}else{


//do something else


}





or you could





if(a.compareTo(b) == 0){


//do something


}


else{


//do something else


}
Reply:first you have to introduce the variable





then assign a value by typing an input statement or type it with the introduction of the variable.





then





if (a == a)


{


statement 1;


statement 2;


statement 3;


}





or





if (a == b)


statement;





if you only have 1 statement.





you can also add an extension so that the first statements will be for when a is == b and the other set is for a is != b.





if (a==b)


{


statement 1;


statement 2;


statement 3;


}


else


{


statement 1;


statement 2;


statement 3;


}

easter cards

No comments:

Post a Comment