Jul 13, 2013 at 8:43am Jul 13, 2013 at 8:43am UTC
i don't think that enumeration can be use for increment operations... maybe you better use switch
Jul 13, 2013 at 9:30am Jul 13, 2013 at 9:30am UTC
chipp i need to use enumeration. you got any other way to solve it?
Jul 13, 2013 at 9:35am Jul 13, 2013 at 9:35am UTC
Either
1 2
for ( day_count=mon; day_count<=sun; /*day_count++*/ day_count = days(day_count+1) )
{ /* ... */ }
Or overload the increment operator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
enum day_t { mon=1, tue, wed, thu, fri, sat, sun, invalid } ;
day_t& operator ++( day_t& d ) { return d = day_t( d + 1 ) ; }
// EDIT: corrected to fix the error pointed out by chipp
day_t operator ++( day_t& d, int ) { auto old_value = d ; ++d ; return old_value ; }
int main()
{
for ( day_t d = mon ; d != invalid ; ++d ) std::cout << d << ' ' ;
std::cout << '\n' ;
}
Last edited on Jul 14, 2013 at 8:29am Jul 14, 2013 at 8:29am UTC
Jul 13, 2013 at 11:15am Jul 13, 2013 at 11:15am UTC
1 2 3 4 5
day_t & operator ++( day_t &d )
{
if ( d == sun ) return d = mon;
return d = static_cast <day_t>( d + 1 ) ;
}
Last edited on Jul 13, 2013 at 11:21am Jul 13, 2013 at 11:21am UTC
Jul 13, 2013 at 2:09pm Jul 13, 2013 at 2:09pm UTC
can't. anyone help please? easy way
Jul 13, 2013 at 2:45pm Jul 13, 2013 at 2:45pm UTC
I do not understand what help you need. All was already shown. What is the problem? You may simplify your ptogram the following way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
using namespace std;
enum days {mon=1, tue, wed, thu, fri, sat, sun};
int main()
{
cout<<"Simple day count\n" ;
cout<<" using enum\n" ;
for ( int day_count = mon; day_count <= sun; day_count++ )
{
cout << " " << day_count << "\n" ;
}
}
Last edited on Jul 13, 2013 at 2:45pm Jul 13, 2013 at 2:45pm UTC
Jul 13, 2013 at 3:25pm Jul 13, 2013 at 3:25pm UTC
thanks i will try. thanks so much JLBorges
Jul 13, 2013 at 10:35pm Jul 13, 2013 at 10:35pm UTC
Expression day_count+1 is implicitly converted to an integral type. However an integral expression can not be implicitly converted to an enumeration type. You shall explicitly specify the type of enumeration to which you want to convert an integral expression. And this record
days(day_count+1)
means that expression
day_count+1
must be converted to the enumeration type enum days .
Last edited on Jul 13, 2013 at 10:36pm Jul 13, 2013 at 10:36pm UTC
Jul 14, 2013 at 8:24am Jul 14, 2013 at 8:24am UTC
Last edited on Jul 14, 2013 at 8:29am Jul 14, 2013 at 8:29am UTC
Jul 14, 2013 at 9:29am Jul 14, 2013 at 9:29am UTC
you're welcome, thx for the link... checking it now...