Like Tree5Likes
  • 1 Post By LTA85
  • 1 Post By AndiPersti
  • 1 Post By Crossfire
  • 1 Post By esbo
  • 1 Post By Matticus

Calculating number of days for a given date code suggestions/how to func?

This is a discussion on Calculating number of days for a given date code suggestions/how to func? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int d,m,y,month= 0 ; longint t; printf ( "enter a date dd-mm-yyyy: " ); ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    25

    Calculating number of days for a given date code suggestions/how to func?

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int d,m,y,month=0;
    longint t;
    printf("enter a date dd-mm-yyyy: ");
    	scanf("%d-%d-%d", &d, &m, &y);
    	if (d<=0||d>31) {
    printf("wrong date format!");
    	}else if(m<=0||m>12){
    printf("wrong date format!");
    		}else if(y<0){
    printf("Jesus was not born on that date that's all I know");
    		}
    	
    	if(m>2 && y%4==0 && y%100!=0 || m>2 && y%400==0)
    		d=d+1;
    	
    	(long int)t=(y-1)*365.25+month+d;
    	switch (m) {
    		case 1:month=d;
    		case 2:month=31+d;
    		case 3:month=59+d;
    		case 4:month=90+d;
    		case 5:month=120+d;
    		case 6:month=151+d;
    		case 7:month=181+d;
    		case 8:month=212+d;
    		case 9:month=243+d;
    		case 10:month=273+d;
    		case 11:month=304+d;
    		case 12:month=334+d;
    default:break;
    		
        }printf("%li days past since jesus was born", t);
    getchar();
    return0;
    	
    }
    I wrote this code myself and I am pretty new in C programming I just like have suggestions about what I did wrong and right and I have some questions if u can help
    1.I dont know intending format checked on internet couldnt find exact answer
    2.I tried changed this a function compiler said nested functions are disabled
    3.I did casting for "t" but I dont know what value I will get for instance for x.75 will compiler give me x or x+1?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Indentation

    2 -- Are the number of the closing brackets equal to the number of the closing ones??

    3 -- run it to find out !
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,652
    Quote Originally Posted by std10093 View Post
    2 -- Are the number of the closing brackets equal to the number of the closing ones??
    I sure hope so

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are right Matticus.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    (long int)t=(y-1)*365.25+month+d;
    The variable is already declared.
    Try this:
    t=(y-1)*365.25+month+d;
    amadeus111 likes this.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    (long int)t=(y-1)*365.25+month+d;
    switch (m) {
        case 1:month=d;
        case 2:month=31+d;
    ...
    What value has "month" when you calculate "t"?

    Bye, Andreas
    amadeus111 likes this.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    also looks to me that the switch will add up EVERY month, instead of just the one you want....no breaks on each case?
    amadeus111 likes this.

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Jesus was almost certainty not born on Dec 31 nor Dec 25, or in the year 0.

    4-6 BC is more likely and probably in September.
    amadeus111 likes this.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Code:
    #include <stdio.h>
    
    
    
    
    int main()
    {
    	int d,m,y,month=0;
    longint t;
    printf("enter a date dd-mm-yyyy: ");
    	scanf("%d-%d-%d", &d, &m, &y);
    	if (d<=0||d>31) {
    printf("wrong date format!");
    	}else if(m<=0||m>12){
    printf("wrong date format!");
    	}else if(y<0){
    printf("Jesus was not born on that date that's all I know");
    	}
    	
    	if(m>2 && y%4==0 && y%100!=0 || m>2 && y%400==0)
    		d=d+1;
    	
    	switch (m) {
    		case 1:month=0;
    break;
    		case 2:month=31;
    break;
    		case 3:month=59;
    break;
    		case 4:month=90;
    break;
    		case 5:month=120;
    break;
    		case 6:month=151;
    break;
    		case 7:month=181;
    break;
    		case 8:month=212;
    break;
    		case 9:month=243;
    break;
    		case 10:month=273;
    break;
    		case 11:month=304;
    break;
    		case 12:month=334;
    break;
    default:break;}
    			
    	
    	t=((y-1)*365.25)+month+d;
    				
    printf("%li days past till %d-%d-%d, since date started ", t,d,m,y);
    getchar();
    return0;
    	
    }
    I have tried to fix the code and came out like this, check it pls, anymore suggestions ?
    thanks to everybody

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Quote Originally Posted by std10093 View Post

    2 -- Are the number of the closing brackets equal to the number of the closing ones??
    I am sorry, but I didn't understand what you mean

  11. #11
    Registered User
    Join Date
    Jan 2013
    Location
    UK
    Posts
    19
    Their are the same amount of closing brackets with opening brackets.

    You should put fflush stdin before getchar();

    Code:
        fflush(stdin);
        getchar();

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,652
    Quote Originally Posted by 0x47617279 View Post
    Their are the same amount of closing brackets with opening brackets.

    You should put fflush stdin before getchar();

    Code:
        fflush(stdin);
        getchar();
    No! Dont use "fflush(stdin)" - it results in undefined behavior.
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    stahta01 likes this.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by amadeus111 View Post
    I am sorry, but I didn't understand what you mean
    I was wrong, so never mind (sorry)

    I agree hardly on Matticus statement about flushing the stdin
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function days from date to december 31st
    By cameuth in forum C Programming
    Replies: 6
    Last Post: 11-25-2011, 06:35 PM
  2. Adding #days to a date
    By acidblue in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2011, 02:36 PM
  3. current date - 3 days ago
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 04:21 PM
  4. How to add Days to a Date?
    By Bufferlo in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 10:19 AM
  5. Calculating days between two Dates
    By Niy in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2002, 08:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21