The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 12-14-2004
Registered User
 

Join Date: Jun 2004
Posts: 13
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 ........
Reply With Quote
Forum Sponsor
  #2  
Old 12-15-2004
Registered User
 

Join Date: Nov 2004
Location: Wherever i may roam
Posts: 3
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
Reply With Quote
  #3  
Old 12-17-2004
Registered User
 

Join Date: Oct 2004
Posts: 235
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
Reply With Quote
  #4  
Old 12-17-2004
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Quote:
if(v) {

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
Reply With Quote
  #5  
Old 12-22-2004
Registered User
 

Join Date: Oct 2003
Posts: 69
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;
   }

   ...
}
Reply With Quote
  #6  
Old 12-22-2004
Registered User
 

Join Date: Jan 2002
Location: India
Posts: 111
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0