How to use enums in C

0 votes

Suppose we have an enum like the following:

enum Days {Saturday, Sunday, Tuesday, Wednesday, Thursday, Friday};

I want to make an instance of this enum and give it an appropriate value, so I do the following:

Days day = Days.Saturday;

Now I'd like to compare my variable or instance to an existing enum value, thus I'll perform the following:

if (day == Days.Saturday)
{
    std::cout << "Ok its Saturday";
}

Which gives me a compilation error:

error: expected primary-expression before ‘.’ token

So, just to be clear, what's the difference between:

if (day == Days.Saturday) // Causes compilation error

and

if (day == Saturday)

?

What do these two truly mean, because one is OK and the other generates a compilation error?

Jun 15 in C++ by Nicholas
• 2,460 points
8 views

1 answer to this question.

0 votes

This will be sufficient to declare your enum variable and compare it:

enum Days {Saturday, Sunday, Tuesday, Wednesday, Thursday, Friday};
Days day = Saturday;
if (day == Saturday) {
    std::cout << "Ok its Saturday";
}
answered Jun 20 by Damon
• 3,580 points

Related Questions In C++

0 votes
0 answers

How to traverse stack in C++?

Is traversing std::stack possible in C++? It is not possible to traverse using the following method.  Because there is no member end in std::stack. std::stack<int> foo; // .. for (__typeof(foo.begin()) it = foo.begin(); ...READ MORE

Jun 1 in C++ by Nicholas
• 2,460 points
24 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 3,580 points
19 views
0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20 in C++ by Damon
• 3,580 points
6 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21 in C++ by Damon
• 3,580 points
10 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
190 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,371 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
787 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,720 points
582 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
15 views
0 votes
1 answer

How to use new[ ] and delete[ ] operator in C++

int main(){ char *str; ...READ MORE

answered Jun 20 in C++ by Damon
• 3,580 points
11 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP