Thread: enum and arrays

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Angry enum and arrays

    Here is the code I have. It prints out the current month and previous month in the correct column. Where would I go to make it print out rest of the months underneath them in order? Thanks!!


    #include<stdio.h>

    enum month{jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
    typedef enum month month;

    month find_prev_month ( month d );

    char *month_names[] = {
    "December", // this is month 0 - ie, jan-1
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
    };

    int main() {
    month d,m;
    printf("Enter number of the month: ");
    scanf("%d", &d);
    m=find_prev_month(d);
    printf("Current\t\tPrevious\n");
    printf("%s\t\t%s\n",month_names[d],month_names[m]);

    return 0;
    }

    month find_prev_month ( month d ) {
    return d-1;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you could use a backward counting for loop calling your find_previous_month() func. with the loop index.
    Code:
    int main() { 
    month d,m;
    int i; 
    printf("Enter number of the month: "); 
    scanf("%d", &d); 
    m=find_prev_month(d); 
    printf("Current\t\tPrevious\n"); 
    printf("%s\t\t%s\n",month_names[d],month_names[m]); 
    for(i=m;i<0;i--)
    {
    m=find_prev_month(i);
    printf("%s\n",month_names[m]);
    }
    return 0; 
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    e

    Stoned;

    I inserted the code into the main body of the program, overwriting mine, then ran it, and I got the same result. The output looks like:

    Current Previous
    August July

    This is if I entered 8. So it is running with the exception of printing the remaining months such as:

    Current Previous
    August July
    September August
    October September

    and so on through the rest of the months.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    thought that was what you wanted. how do you want it to print exactly?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    Yeah, that's the start of how I needed it printed. But that is printing the current and previous month only. Underneath the first line of months, I was wanting it to list in order from that point on the remaining months. Say I started with August or 8 as input, then it would print August and then July on the first line. On the second line it would print September then August. On the third line would be October then September. This would continue until the last line which would read July for Current and June for Previous.

    From the starting point, I am wanting to list the remaining 11 months for each column.

    Hope this helps you with my crazy way of thinking.....hahaha

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok then in that case add a function that returns next month rather than last month. now this is what we are aiming for...

    Code:
    current       previous
    next           current
    next+1       next
    next+2       next+1
    
    and so on until month == 12 (december)
    now its easy.... look at the above pattern and have a go then repost if you still cant do it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with arrays
    By implor in forum C Programming
    Replies: 9
    Last Post: 06-01-2009, 06:12 AM
  2. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  3. arrow keys in arrays
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 03-06-2006, 01:26 PM
  4. Replies: 1
    Last Post: 04-20-2003, 05:02 PM
  5. Problem with character arrays.
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 05:25 PM