Search code examples
ccountermonthcalendar

Roll over counter skipping zero


Maybe an easy question, but I cannot get my head around it.

I'm doing some kind of a month counter, the idea is the user can press - or + and still reach the desired month: for example I have: Today is Feb so something like this The user press - 2 1 12 11 10 9 ...... Some goes for + The problem is I don't want the zero tobe displayed

SINT16 tempVal = pDate->month + upDown;
    if (tempVal < 0) {
        tempVal += (12+1);
    } 
    pDate->month % (12 + 1);

This is what i have come up with- it rolls over 12 and then 0 or 1 0 12 But I want get rid of the zero. Any help will be appreciated :)


Solution

  • The question is very unclear, but i'll try

    SINT16 tempVal = pDate->month + upDown;
    if (tempVal < 0) {
        /* if you are at negative 1 - assuming upDown is either (1,-1) */
        tempVal += (12); //just add the cycle value
    } 
    
    //pDate->month % (12 + 1);
    pDate->month = (pDate->month % 12) + 1;//modulo by the cycle and then shift output
    

    to be clear modulo 12 will output values (0 - 11) so the +1 should not be included in the modulo function