Doubt in structure -- c++

hi,
Small sample program

#include <iostream>
using namespace std;

typedef struct vijay {

int a;
int b;

}VIJAY;

void main ()
{
VIJAY v;
cout << "a=" << v.a<<endl;
cout << "b=" << v.b<<endl;

if(v) {
cout << "Entered" << endl;
}
}

I am getting error :

line 17: Error: An expression of scalar type was expected.

How to solve this ........

hi vijay,

i don't think you can use a user defined type for comparisson.

maybe you should try something like:

if(v.a && v.b){
...
}

regards,
dream

Ya i accept with dream, since you have to specify the data memeber that you are comparing in if(v),

or you may store it in some varilable and do it.
arun

above can be done if v is pointer ;

I change yr prgram some thing like this ...

VIJAY *v ;

and ran ....

i got o/p ....

a=-1091576147
b=1
Entered

I think before we answer properlly we need to know what you are trying to accomplish by the if(v) statement.

I assume that you want to know if a and b members have been assigned values? If this is the case then you'll need to initialize the members to a value that the user won't enter (if possible, otherwise you'll need other members to denote that the member has been entered) and test those members against the unassigned value in the if statement as follows:

static const int UNASSIGNED_VALUE = -1;

void main()
{
   ...

   v.a = v.b = UNASSIGNED_VALUE;

   if (v.a != UNASSIGNED_VALUE && v.b != UNASSIGNED_VALUE))
   {
      cout << "Entered" << endl;
   }

   ...
}

The expression within the '(' and ')' parenthesis in an 'if' statement will be evaluated as 'test context'.

In a 'test context' the value of expression causes to flow in one or other way depending on the computed value if zero or non-zero. You can write any expression that has a 'scalar rvalue' result, because scalar types can only be compared with zero. However there can be 'side-effects' when evaluating an expression. Care should be taken about those.