Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>

using namespace std;

int main()
{
    int a;
int my_name=1;
int my_class=5;
int my_age=3;
cout<<"enter the number"<<endl;
cin>>a;
if (a==my_*){
    cout<<"success";
}

}


What I have tried:

any other shortcut process, except this
C++
if ((a==my_name)&&(a==my_class)&&(a==my_age)){
    cout<<"success";
Posted
Updated 18-Nov-20 20:17pm

BTW - With the values you have assigned those variables your "shortcut" condition will never be true.

I find macros to be helpful in situations like this. They can definitely be abused in evil ways but they can also be quite useful. Here's how I would do this :
C++
using PCTSTR = const char *;

bool CompVarValues( PCTSTR varname, int value1, int value2 )
{
   bool state = ( value1 == value2 );
   PCTSTR boolstr = state ? "true" : "false";
   std::cout << "comparing value of " << varname << " result is " << boolstr << std::endl;
}

// a macro to help with this - it uses the string-izing operator #

#define CompareValues( a, b )  CompVarValues( #a, a, b )

// usage :

CompareValues( my_name, a );
CompareValues( my_class, a );
CompareValues( my_age, a );
 
Share this answer
 
Comments
Richard MacCutchan 19-Nov-20 5:07am    
+5. And using the stringizer is the only time I ever create macros these days.
You might, for instance, iterate over a container
C++
#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
  int a;

  unordered_map<string, int> um = { {"my_name", 1}, {"my_class", 5}, {"my_age", 3} };

  cout<<"enter the number"<< endl;
  cin >> a;

  unsigned matches = 0;

  for (auto p : um )
    if ( p.second == a)
      ++matches;

  if ( matches == um.size() )
    cout << "success" << endl;
  else
    cout << "failure, matches " << matches << "/" << um.size() << endl;

}
 
Share this answer
 
Comments
Rick York 24-Nov-20 1:52am    
very nice.
CPallini 24-Nov-20 2:02am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900