Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have 2 integer a and b, which want to used in 2 condition.

C++
int condition1()
{
  if (a==0){
   b=5;
  }
  else(a==1)
   b=7;
  }
  ......
}

C++
int condition2()
{
  if (b==5){
   a=0;
  }
  else(b==7)
   a=1;
  }
  ......
}


What I have tried:

i want as
C++
int condition1()
{
int x;
 check(x);
}

int condition2()
{
int x;
 check(x);
}

int check(int a)
{
 int b
// and i dont know how to write condition in here which use in both condition.
return b;
}
Posted
Updated 22-Oct-20 20:11pm

Note that
Quote:
int condition1()
{
if (a==0){
b=5;
}
else(a==1)
b=7;
}
......
}
is not valid code. You should write something like
C++
int condition1()
{
  if (a == 0)
  {
    b = 5;
  }
  else if ( a == 1)
  {
    b = 7;
  }
  //...
}


In order to combine two (or more) conditions, there are the logical operators, gently provided by the programming language, see, for instance: C++ Logical Operators[^].
For example
C++
if ( a == 0 && b ==5)
{
  //do something wonderful here..
}
 
Share this answer
 
It's not at all clear what you are trying to do, but what is clear is that you are doing it completely wrong.
Your check method returns a value - which is correct - but the code that calls it doesn't use it.
Your condition methods should return a value, but don't.
Your check method takes a parameter, but doesn't use it.
You condition methods don't take a parameter and should.

What I'd suggest is that you think about exactly what you are trying to do, and start working out what values should be parameters and what should be returned.

If what you means is "can I write a function that both condition1 and condition2 can call?" then you will need to be a lot more specific about what it should do - particularly when the value doesn't match either of the if conditions!
 
Share this answer
 

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