How do I compare two integers in C++?
I have a user input ID (which is int
) and then I have a Contact ID that is part of my Struct. The Contact ID is int
also.
I need to compare to see if they are the same, to know that it exists.
I did something like this*:
if(user_input_id.compare(p->id)==0)
{
}
but I get an error message saying that expression must have class type.
*based on reading this page http://www.cplusplus.com/reference/string/string/compare/
The function you found is for comparing two std::string
s. You don't have std::string
s, you have int
s. To test if two int
s are equal, you just use ==
like so:
if (user_input_id == p->id) {
// ...
}
In fact, even if you had two std::string
s, you'd most likely want to use ==
there too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With