![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Search attributes in one structure using the values from another structure | dhanamurthy | High Level Programming | 3 | 03-27-2008 12:37 AM |
| MV files from one directory structure(multiple level) to other directory structure | srmadab | UNIX for Advanced & Expert Users | 4 | 09-13-2006 01:01 PM |
| Copying a Directory Structure to a new structure | jhansrod | UNIX for Dummies Questions & Answers | 8 | 07-27-2005 03:24 AM |
| if then else structure | props | UNIX for Dummies Questions & Answers | 4 | 12-27-2003 03:50 AM |
| Ram structure | Dorian | HP-UX | 1 | 11-05-2002 06:48 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 ........ |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
reply
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 |
|
#4
|
||||
|
||||
|
Quote:
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 |
|
#5
|
|||
|
|||
|
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: Code:
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;
}
...
}
|
|
#6
|
|||
|
|||
|
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.
__________________
Regards, Satya Prakash Prasad |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|