?
Solved

C ++ structure  and bool isEqual

Posted on 2006-05-04
9
Medium Priority
?
878 Views
Last Modified: 2008-01-09
 I build a cartype structure with four elements that are typically different between car types. These four elements are the color, how many doors it has, how many passengers it can carry, and whether it is a Toyota or not. My question is is there a diffefrence method to use rather bool isEqual with  else if ...  


#include <iostream>
#include <string>
 
using namespace std;
 
struct cartype
{
 bool bolToyota; // is the car a Toyota
 string strColor; // what color the car is
 int intDoors; // how many doors the car has
 int intPassengers; // how many passengers the car can carry
};
 
bool isEqual(cartype, cartype); // prototype of compare function
 
int main(void)
{
 cartype CarA;
 cartype CarB;
 
 CarA.bolToyota = true;
 CarB.bolToyota = false;
 CarA.intDoors = 4;
 CarB.intDoors = 2;
 CarA.intPassengers = 4;
 CarB.intPassengers = 5;
 CarA.strColor = "Red";
 CarB.strColor = "Blue";
 
 if (! isEqual(CarA, CarB))
 {
  cout << "The cars are different" << endl;
 }
 else
 {
  cout << "The cars are the same" << endl;
 }
 
 return 0;
}
 
bool isEqual(cartype c1, cartype c2)
{
 bool b = true ; // default check is true
 // check each structure type, exit on first difference
 
 if (c1.intDoors != c2.intDoors)
 {
  b = false;
 }
 else if (c1.bolToyota != c2.bolToyota)
 {
  b = false;
 }
 else if (c1.intPassengers != c2.intPassengers)
 {
  b = false;
 }
 else if ( c1.strColor.compare(c2.strColor) != 0)
 {
  b = false;
 }
 return 0;
}
0
Comment
Question by:mhho
9 Comments
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16610088
Hi,
What do you mean by "diffefrence method"?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 16610114
You could just use

bool isEqual(cartype c1, cartype c2)
{
 bool b = true ; // default check is true
 // check each structure type, exit on first difference
 
 if (c1.intDoors != c2.intDoors)
 {
  return false;
 }
 if (c1.bolToyota != c2.bolToyota)
 {
   return false;
 }
 if (c1.intPassengers != c2.intPassengers)
 {
   return false;
 }
 if ( c1.strColor.compare(c2.strColor) != 0)
 {
   return false;
 }
 return true;
}
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16610342
You can define operator==() and operator!=().

e.g.
--------8<--------
bool operator==(const cartype& lhs,const cartype& rhs)
{
      return isEqual(lhs,rhs);
}

bool operator!=(const cartype& lhs,const cartype& rhs)
{
      return !operator==(lhs,rhs);
}
--------8<--------

That way you can compare cars thus:

    if (cara == carb) { /* */ }
    if (cara != carb) { /* */ }

Note the use of const reference, which would be good style for your isEqual function too:

    bool isEqual(const cartype& c1, const cartype& c2) {
        // ...
    }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:mhho
ID: 16610990
Note the use of const reference, which would be good style for your isEqual function too:
I have  an error .....please advise...
#include <iostream>
#include <string>
 
using namespace std;
 
struct cartype
{
 bool bolSportcar; //  the car is sport car
 string strColor; // what color the car is
 int intDoors; // how many doors
 int intPassengers; // how many passengers
};
bool isEqual(const cartype& c1, const cartype& c2); // prototype of compare function
 
int main(void)
{
 cartype CarA;
 cartype CarB;
 
 CarA.bolSportcar = true;
 CarB.bolSportcar = false;
 CarA.intDoors = 2;
 CarB.intDoors = 4;
 CarA.intPassengers = 2;
 CarB.intPassengers = 5;
 CarA.strColor = "Red";
 CarB.strColor = "Blue";
 
 if (! isEqual(CarA, CarB))
 {
  cout << "It not the same car" << endl;
 }
 else
 {
  cout << "The cars are the same" << endl;
 }
 
 return 0;
}

bool operator==(const cartype& lhs,const cartype& rhs)
{
     return isEqual(lhs,rhs);
}

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16611011
What's your error?

This compiles OK:
--------8<--------
#include <iostream>
#include <string>

using namespace std;

struct cartype
{
 bool bolToyota; // is the car a Toyota
 string strColor; // what color the car is
 int intDoors; // how many doors the car has
 int intPassengers; // how many passengers the car can carry
};

bool isEqual(cartype, cartype); // prototype of compare function

inline bool operator==(const cartype& lhs,const cartype& rhs)
{
      return isEqual(lhs,rhs);
}

inline bool operator!=(const cartype& lhs,const cartype& rhs)
{
      return !operator==(lhs,rhs);
}


int main(void)
{
 cartype CarA;
 cartype CarB;

 CarA.bolToyota = true;
 CarB.bolToyota = false;
 CarA.intDoors = 4;
 CarB.intDoors = 2;
 CarA.intPassengers = 4;
 CarB.intPassengers = 5;
 CarA.strColor = "Red";
 CarB.strColor = "Blue";

 if (CarA != CarB)
 {
  cout << "The cars are different" << endl;
 }
 else
 {
  cout << "The cars are the same" << endl;
 }

 return 0;
}

bool isEqual(cartype c1, cartype c2)
{
 bool b = true ; // default check is true
 // check each structure type, exit on first difference

 if (c1.intDoors != c2.intDoors)
 {
  b = false;
 }
 else if (c1.bolToyota != c2.bolToyota)
 {
  b = false;
 }
 else if (c1.intPassengers != c2.intPassengers)
 {
  b = false;
 }
 else if ( c1.strColor.compare(c2.strColor) != 0)
 {
  b = false;
 }
 return 0;
}
--------8<--------
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 400 total points
ID: 16611022
...but better with

    bool isEqual(const cartype&, const cartype&);  

--------8<--------
#include <iostream>
#include <string>

using namespace std;

struct cartype
{
 bool bolToyota; // is the car a Toyota
 string strColor; // what color the car is
 int intDoors; // how many doors the car has
 int intPassengers; // how many passengers the car can carry
};

bool isEqual(const cartype&, const cartype&); // prototype of compare function

inline bool operator==(const cartype& lhs,const cartype& rhs)
{
      return isEqual(lhs,rhs);
}

inline bool operator!=(const cartype& lhs,const cartype& rhs)
{
      return !operator==(lhs,rhs);
}


int main(void)
{
 cartype CarA;
 cartype CarB;

 CarA.bolToyota = true;
 CarB.bolToyota = false;
 CarA.intDoors = 4;
 CarB.intDoors = 2;
 CarA.intPassengers = 4;
 CarB.intPassengers = 5;
 CarA.strColor = "Red";
 CarB.strColor = "Blue";

 if (CarA != CarB)
 {
  cout << "The cars are different" << endl;
 }
 else
 {
  cout << "The cars are the same" << endl;
 }

 return 0;
}

bool isEqual(const cartype& c1, const cartype& c2)
{
 bool b = true ; // default check is true
 // check each structure type, exit on first difference

 if (c1.intDoors != c2.intDoors)
 {
  b = false;
 }
 else if (c1.bolToyota != c2.bolToyota)
 {
  b = false;
 }
 else if (c1.intPassengers != c2.intPassengers)
 {
  b = false;
 }
 else if ( c1.strColor.compare(c2.strColor) != 0)
 {
  b = false;
 }
 return 0;
}
--------8<--------
0
 

Author Comment

by:mhho
ID: 16618516
is there a way you could  reduce he number of return statement inside the function to one?
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16618544
You'll get short-circuit evaluation with...

bool isEqual(const cartype& c1, const cartype& c2)
{
   return c1.intDoors == c2.intDoors
   && c1.bolToyota == c2.bolToyota
   && c1.intPassengers == c2.intPassengers
   && c1.strColor.compare(c2.strColor) == 0
   ;
}

.. i.e. at run time it immediately returns false if doors don't match, without checking Toyota etc.
0
 

Author Comment

by:mhho
ID: 16620019
thanks !
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question