Thread: Can someone help me with my next day program, please!

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Can someone help me with my next day program, please!

    Hello there,
    i am trying to rewrite this program taht should take 2 integers- say 17 and 5 which represent 17 May and print out 18 May which is the next day.
    I could use any constuctive comments and/or help that will enable me to get my program up and running quickly.
    One of my questions is do i need to write another enum and switch functions for the days eg; mon - sat or can i already somehow shorten and manipulate my code? I am trying for something short and sweet, folks.
    Code:
    #include<stdio.h>
    
    enum month {jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec}; /*This allows yoou to name a finite set and to declare identifiers*/
    typedef enum month	month;
    
    month previous_month(month M)  /*this is a function definition*/
    {
    	switch (M) /* like an if-else statement, if this month is true goto the M=month you chose*/
    		{
    	case  jan:
    		M=dec;break;
    	case  feb:
    		M=jan;break;
    	case  mar:
    		M=feb;break;
    	case apr:
    		M=mar;break;
    	case may:
    		M=apr;break;
    	case jun:
    		M=may;break;
    	case jul:
    		M=jun;break;
    	case aug:
    		M=jul;break;
    	case sep:
    		M=aug;break;
    	case oct:
    		M=sep;break;
    	case nov:
    		M=oct;break;
    	case dec:
    		M=nov;break;
    	} 
    	return M;
    }
    
    void print_month (month M)  /*this is a function definition*/
    {
    	switch (M)  /* like an if-else statement, if this month is true goto the M=month you chose*/
    	{
    	case jan:
    		printf("January");break;
    	case feb:
    		printf("February");break;
    	case mar:
    		printf("March");break;
    	case apr:
    		printf("April");break;
    	case may:
    		printf("May");break;
    	case jun:
    		printf("June");break;
    	case jul:
    		printf("July");break;
    	case aug:
    		printf("August");break;
    	case sep:
    		printf("September");break;
    	case oct:
    		printf("October");break;
    	case nov:
    		printf("November");break;
    	case dec:
    		printf("December");break;
    	} 
    }
    
    int main()
    {
    	month M, N, sat;
    	printf("Table of months and thier predecessors\n");
    	scanf("%d%d", &M, &N);
    	printf("\n %16s%16s\n", "Month", "Day");
    	for (M = jan; M <= sat; ((int)M + 1)) 
    	{
    		printf("");
    		print_month(M);  /*fucntion call to print month*/
    		printf("");
    		print_month(previous_month(M));  /*fucntion call to print previous month*/
    		putchar('\n');
    		return;
    	}
    }
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >int main()
    should really be
    >int main(void)

    >month M, N, sat;
    >for (M = jan; M <= sat; ((int)M + 1))
    sat is not assigned a value before you use it in the test. Because of this, the loop didn't run on my system.

    >printf("");
    What are you trying to do here? You're not printing anything.

    >previous_month()
    This function could simply decrement the month variable, ensure it's not negative and return. It doesn't really need to switch statement. This will give you the same end result in a lot less code.

    >print_month()
    Again, you could make some changes to loose the switch statement. Namely, store the month strings in an array, and use M as the index. But I expect that's a lesson for another day.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM