Click here to Skip to main content
15,896,402 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can I use if or Switch structure for the objects of a class...for example:

C++
class A
{     
       int acountNo;
       float balance;
       setvalue()
      {
         cout<<"Enter A/C #:"; 
         cin>>acountNo;
         cout<<"Enter Balance:"; 
         cin>>balance;
      }


void main()
{
    A X1,X2,X3;
    A1.setval();
    A2.setval();
    A3.setval();
    
    int a;
    cout<<"Enter Account No: ";
    cin>>a;
    if (a== A1.acountNo)    // This statement gives error


if i write :
C++
if(a==A A1.acountNo)
// It also give Error

Plese Help me that what is correct method for this.
Posted
Updated 13-Apr-14 3:57am
v2
Comments
[no name] 13-Apr-14 9:49am    
It might help things along if you just tell us which error it is that you are getting. There is more than one error message in the programming world. And in no way would this code compile anyway.

1 solution

Well... I'd start by matching the names up...
Change:
C#
A X1,X2,X3;
A1.setval();
A2.setval();
A3.setval();

int a;
cout<<"Enter Account No: ";
cin>>a;
if (a== A1.acountNo)

To:
C#
A A1,A2,A3;
A1.setval();
A2.setval();
A3.setval();

int a;
cout<<"Enter Account No: ";
cin>>a;
if (a== A1.acountNo)
And see if that fixes it...
 
Share this answer
 
Comments
Member 10736823 13-Apr-14 9:54am    
No brother... :-( If statement do not accepting objects(eg. A1 or A2)?
if (a== A1.acountNo) gives error...
Member 10736823 13-Apr-14 9:59am    
Situation is that I have many objects and I want to access a specific object with the help of its data member for example "acountNo" is a data member of object A1 in this case...Every object has a "acountNo" data member, so I want to search a specific "acountNo" with the help of if-else or switch structure....but it gives error...
OriginalGriff 13-Apr-14 10:03am    
Exactly what error does it give?
Copy and paste the message, if you can.
Member 10736823 13-Apr-14 10:35am    
Now its working...Thanks! :-)

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