C ++ structure and bool isEqual

 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;
}
mhhoAsked:
Who is Participating?
 
rstaveleyConnect With a Mentor Commented:
...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
 
WelkinMazeCommented:
Hi,
What do you mean by "diffefrence method"?
0
 
jkrConnect With a Mentor Commented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
rstaveleyCommented:
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
 
mhhoAuthor Commented:
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
 
rstaveleyCommented:
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
 
mhhoAuthor Commented:
is there a way you could  reduce he number of return statement inside the function to one?
0
 
rstaveleyCommented:
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
 
mhhoAuthor Commented:
thanks !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.