Thread: Calculate Days Between 2 Months

  1. #16
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Hi,

    I have the same problem. So far I have this code:

    Code:
    #include<stdio.h>
    
    int main()
    
    {
    	
    int month1, month2;
    int NumDaysInEachMonth[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    int total;
    int i;
    int totalday =0;
    int j;
    int totalday2 =0;
    
    	do{
    
    printf("\t\t\n\n ..... Number of days across two months program.....\n\n\n");
    printf("\t 1........... January \n");
    printf("\t 2........... Febuary \n");
    printf("\t 3............. March \n");
    printf("\t 4............. April \n");
    printf("\t 5............... May \n");
    printf("\t 6......... .....June \n");
    printf("\t 7......... .....July \n");
    printf("\t 8............ August \n");
    printf("\t 9......... September \n");
    printf("\t 10......... .October \n");
    printf("\t 11......... November \n");
    printf("\t 12......... December \n\n\n\n\n");
    
    
    do{
    		printf("\t Please choose the first month (1-12)\n");
    		scanf("\t%d \n", &month1);
    
    		if	(month1<1||month1>12) // error checking 
    		printf("\tinvalid section, please enter another\n");
    
    }
    		while(month1<1||month1>12);  // loop until user enters a valid menu number
    
    		
    do{
    
    		printf("\tPlease enter a second month (1-12)");
    		scanf("\t %d \n", &month2);
    
    		if	(month1<1||month1>12) // error checking 
    		printf("\t invalid section please enter another\n");
    
    }
    		while(month1<1||month1>12);
    			
    		
    			
    			if
    				(i=month1, i<=month2, i++);
    {
    				totalday = totalday+NumDaysInEachMonth[i];
    } 
    
    
    			if
    				(j=month2, j<=month1, j++);
    			{
    				totalday2 = totalday2+NumDaysInEachMonth[j];
    			} 
    	
    			printf("\t %d   \n\n\n\n",totalday+=totalday2);
    			}
    
    	while (totalday<28||month1>62);
    
    	return 0;
    	}
    for some reason now that i have the second if
    Code:
     if
    				(j=month2, j<=month1, j++);
    			{
    				totalday2 = totalday2+NumDaysInEachMonth[j];
    			}
    it wont allow me to enter a second date. if I remove this and change the final printf. the program will allow me to enter two months and this will return how many days are in the final month. which is wrong because i want the program to return with the number of days across the two months the user inputted.
    Could somebody give me an idea of were i have gone wrong. because at the moment i am clueless.


    Tom.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    do{
    
    		printf("\tPlease enter a second month (1-12)");
    		scanf("\t %d \n", &month2);
    
    		if	(month1<1||month1>12) // error checking 
    		printf("\t invalid section please enter another\n");
    
    }
    		while(month1<1||month1>12);
    See the problem with copy-paste code?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    [quote]
    Code:
     if (i=month1, i<=month2, i++);
      {
        totalday = totalday+NumDaysInEachMonth[i];
       }
    [quote]

    Ummmm.... you can't use if with the parameters of for... C will get real upset at you for that.

    Plus you are missing something very important... if the user enters 1, 2 for the months... how many days is that? How many days between January 1 and February 1 ?

    (Although it is kind of fun watching you two "borrowing" each other's broken code...)
    Last edited by CommonTater; 11-28-2010 at 08:35 AM.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    [QUOTE=CommonTater;986147]

    (Although it is kind of fun watching you two "borrowing" each other's broken code...)
    ha yeah i guess it must be. Im going to scrap all that code and just start again.

  5. #20
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    I think I've got it working. Now I'll just work on making the program recognise that a user has entered an invalid month and then asks to enter a month again.

    Thanks for the help everyone. Bye.
    Last edited by Khadafi; 11-28-2010 at 12:41 PM.

  6. #21
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    deleted all my code and started again. Now have a program thats fully working. so pretty pleased

  7. #22
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Thanks To Everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing Days
    By nhubred in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2009, 06:22 PM
  2. summing number of days between 2 months
    By duff_johnny in forum C Programming
    Replies: 5
    Last Post: 11-03-2005, 02:36 PM
  3. Algorithm help
    By mmyers1 in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2004, 09:56 PM
  4. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 11:50 PM