PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] How to compare 2 integers to see if they are equal?

 July 19, 2022     c++, compare, integer     No comments   

Issue

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/


Solution

The function you found is for comparing two std::strings. You don't have std::strings, you have ints. To test if two ints are equal, you just use == like so:

if (user_input_id == p->id) {
  // ...
}

In fact, even if you had two std::strings, you'd most likely want to use == there too.



Answered By - Joseph Mansfield
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing