Thread: Any help much appreciated

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    30

    Any help much appreciated

    I have been asked to write a program that will ask the user to enter two months, and then give the number of days from the start of the first month to the end of the second. I have very little experience with programming and i'm struggling with this one...
    Please do not flame me, i am not asking anyone to do this for me, but if you could give me a push in the right direction it would be much appreciated.

    The main problem i am having is trying to figure out how to do the actual working out.. The number of days in each month is to be stored in an array, numDays, am i right in thinking that i'll have to use ++ in some way? I am really stuck with this, any help at all would be great.

    Here is what i have so far... It isn't even half done yet, i know that its a big mess but pls bear in mind i am a total programming n00b..

    I have no idea where to put the array, i realise its probably not meant to go there....
    The 'if' statement will almost certainly not work, it was a stab in the dark...
    I'm sure i've either got too many variables or else they arn't defined very well...

    Take pity on me?

    Code:
    #include <stdio.h>
    
    int main()
    {
        int fromMonth, toMonth;
        
        printf ("This program will tell you the number of days from the start of one month to the end of another\n");
        printf ("Enter the nmumber of the months you wish to use (Jan=1, Feb=2 etc..): ");
        scanf ("%d", &fromMonth );
        scanf ("%d", &toMonth );
        printf ("The total number of days is %d\n", howManyDays( fromMonth, toMonth ) );
        return 0;
    }
    
    int howManyDays (int fromMonth, int toMonth)
    {
        if fromMonth < 1, > 12, if toMonth < 1, > 12
        printf ("Error: Please check the input..\n");
        howManyDays = numDays [fromMonth] 
        
        
        int numDays[12];
        $numDays [1]=31;
        $numDays [2]=28;
        $numDays [3]=31;
        $numDays [4]=30;
        $numDays [5]=31;
        $numDays [6]=30;
        $numDays [7]=31;
        $numDays [8]=31;
        $numDays [9]=30;
        $numDays [10]=31;
        $numDays [11]=30;
        $numDays [12]=31;

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    first thing, arrays start at 0 not 1. so with
    Code:
    int numDays[12]
    the identifiers are actually 0-11.

    You can also merge the 2 scanf()s together:
    Code:
    scanf("&#37;d%d", &fromMonth, &toMonth)
    I personally have never seen if statements like the ones in your code. An if statement should be in the following format:
    Code:
    if (expression)
        statement
    Please note that the statement can be a compound statement sorrounded by brackets

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    thanks a lot for the help

    To calculate the result, could i use a for statement? something like for (fromMonth <= toMonth then some sort of ++ thing... but then im not sure how that would call and add the days for each month... I think i'm just not cut out for this stuff ...
    Last edited by euclid; 11-13-2007 at 10:18 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, a for-loop woudl work quite well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Oh ya. Another thing is that you can initialize the array like this:
    Code:
    int numDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    And your howManyDays() function should look like this:
    Code:
    int 
    if (fromMonth < 0 || fromMonth > 11 || toMonth < 0 || toMonth > 11)
       printf("Error. Please check the input...\n");
    else
    {
       int numDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       int count, result=0;
    
       for (count=fromMonth; count <= toMonth; count++)
          result += numDays[count];
    
       return result;
    And since arrays start from 0 you should either make Jan=0, Feb=1... or after inputting the data just deduct 1 from the two variables.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    thanks, you're life savers, I would really be stuck without the help.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Could someone give me a hint as to why this isn't compiling? I get told that the are 'too many arguments to function howManyDays'. I cant see what i'm doing wrong, i've checked all the online guides i can and it looks to me like im doing the right thing (although im clearly not).

    As always, i'm thankful for any help

    Code:
    #include <stdio.h>
    
    int howManyDays ( int result );   //declare numDays function
     
    int main ()   //Start of main function
    {
        int toMonth, fromMonth;
        int numDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        
        printf ("This program will tell you the number of days from the start of one month to the end of another\n");
        printf ("Enter the nmumber of the months you wish to use (Jan=1, Feb=2 etc..): ");
        scanf("%d%d", &fromMonth, &toMonth);
        printf ("The total number of days is %d\n", howManyDays( fromMonth, toMonth ) );   // sends values for from/toMonth to numDays
        return 0;
    }   //main function ends
    
    int howManyDays( fromMonth, toMonth ) {      //numDays function
    if (fromMonth < 0 || fromMonth > 11 || toMonth < 0 || toMonth > 11)
       printf("Error [-1]. Please check the input...\n");
    else
    {
       int numDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       int count, result=0;
    
       for (count=fromMonth; count <= toMonth; count++)
          result += numDays[count];
    
       return result;              //result sent to main
    }

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int howManyDays( fromMonth, toMonth ) {      //numDays function
    ->
    Code:
    int howManyDays( int fromMonth, int toMonth ) {      //numDays function
    Also, you declare this in main()
    Code:
    int numDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    but never use it. In addition, in howManyDays() where you also declare it, you can leave out the "12" for the size if you like, and the compiler will calculate the number of elements in the array for you.

    Also, your code has January as 0, Feb. as 1, etc, but you print
    Code:
    Enter the nmumber of the months you wish to use (Jan=1, Feb=2 etc..):
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    30

    Talking

    thanks

    I've made those changes, but i'm still getting the ' too many arguments to function `howManyDays' ' error, any ideas?
    Last edited by euclid; 11-14-2007 at 02:18 PM.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    ahhh think i got it, its compiled now at least

    Code:
    #include <stdio.h>
    
    int howManyDays (  int fromMonth, int toMonth  );   //declare numDays function
     
    int main ()   //Start of main function
    {
        int toMonth, fromMonth;
        
        printf ("This program will tell you the number of days from the start of one month to the end of another\n");
        printf ("Enter the nmumber of the months you wish to use (Jan=0, Feb=1 etc..): ");
        scanf("%d%d", &fromMonth, &toMonth);
        printf ("The total number of days is %d\n", howManyDays( fromMonth, toMonth ) );   // sends values for from/toMonth to numDays
        return 0;
    }   //main function ends
    
    
    
    int howManyDays( int fromMonth, int toMonth ) {      //numDays function
    
        int numDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (fromMonth < 0 || fromMonth > 11 || toMonth < 0 || toMonth > 11)
       printf("Error [-1]. Please check the input...\n");
    else
    {
       int count, result=0;
    
       for (count=fromMonth; count <= toMonth; count++)
          result += numDays[count];
    
       return result;              //result sent to main
    }
    }

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Very good . . . just one thing: what happens just after you print "Error [-1]. ..."? What does howManyDays() return? A random value.

    You probably want to return -1 or 0 or something in this case.

    Oh, and what about leap years?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by euclid View Post
    ahhh think i got it, its compiled now at least
    Yes, make sure your declarations and definitions of function are one and the same! They should be equal, or you will get compile errors or linking errors.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24
    You could also use an enum to declare your months, instead of having arrays..

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    thanks for all the help, this is the finished code, which now tests itself and prints out the results..
    Code:
    #include <stdio.h>
    
    int howManyDays (  int fromMonth, int toMonth  );   //declare numDays function
     
    int main ()   //Start of main function
    {
        int toMonth, fromMonth, i, j;
        
    	for (i = 0; i < 12; i++)
    		for (j = 0; j < 12; j++)
    		printf ("%d\n", howManyDays(i, j));
    
        printf ("This program will tell you the number of days from the start of one month to the end of another\n");
        printf ("Enter the nmumber of the months you wish to use (Jan=0, Feb=1 etc..): ");
        scanf("%d %d", &fromMonth, &toMonth);     //user enters 2 months
        printf ("The total number of days is %d\n", howManyDays( fromMonth, toMonth ) );   // sends values for from/toMonth to numDays
        return 0;
    }   //main function ends
    
    
    
    int howManyDays( int fromMonth, int toMonth ) {      //numDays function
    	int count, result=0;
        int numDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  //array declared
    	if (fromMonth < 0 || fromMonth > 11 || toMonth < 0 || toMonth > 11) {
       printf("Error [-1]. Please check the input..\n");
    	return 0;
    	} else
    {
       for (count=fromMonth; count <= toMonth; count++)
          result += numDays[count];
    
       return result;              //result sent to main
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Program Control, HELP appreciated.
    By Astra in forum Windows Programming
    Replies: 7
    Last Post: 01-01-2007, 06:59 AM
  3. Help Appreciated
    By cboard_member in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2005, 06:31 AM
  4. C++ Programs -- Any input appreciated
    By Kyoto Oshiro in forum Game Programming
    Replies: 0
    Last Post: 02-27-2002, 11:22 PM
  5. I need help..anything will be appreciated
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2001, 10:55 AM