Dec 15, 2011 at 2:07pm Dec 15, 2011 at 2:07pm UTC
Hi guys,
I'm not sure how to test equality using enum. Is this code valid?
enum month = {January; February; March;};
struct Date = {int day, int month, int year};
bool(Date birthday)
{
if(birthday.month == February) return true;
else return false;
}
Thanks!
Dec 15, 2011 at 2:43pm Dec 15, 2011 at 2:43pm UTC
Yes, you can test for equality with operator==.
There are a few syntax errors in your sample, though, here's a working example (I'll rename enum month to enum Month to allow a variable named month, and I'll simplify the function a bit)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
enum Month {
January,
February,
March
};
struct Date {
int day;
Month month;
int year;
};
bool is_bday_february(Date birthday)
{
return birthday.month == February;
}
int main()
{
Date d1{1, February, 2011};
std::cout << std::boolalpha << is_bday_february(d1) << '\n' ;
Date d2{1, March, 2011};
std::cout << std::boolalpha << is_bday_february(d2) << '\n' ;
}
demo:
http://ideone.com/My3E7
Last edited on Dec 15, 2011 at 2:44pm Dec 15, 2011 at 2:44pm UTC
Dec 15, 2011 at 4:18pm Dec 15, 2011 at 4:18pm UTC
Thank you so much for your help!:)