Like Tree4Likes

help - making a calendar using c

This is a discussion on help - making a calendar using c within the C Programming forums, part of the General Programming Boards category; pls help me. Im totally confused how to make this one- Make a program (include strings) that receives the month ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    11

    Exclamation help - making a calendar using c

    pls help me. Im totally confused how to make this one- Make a program (include strings) that receives the month and year from the keyboard as integers and prints the calender in the following format. March 2011 Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 7 .... etc Given that 01/01/1900 was Monday. pls help

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    What do you have coded so far? (Be sure to use code tags.)

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    11
    I have no idea.
    also could you tell me the difference between these two: int i = 0 and int *i = 0 ??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
      int year[4];
      char month[9];
      int p;
      printf("Enter Year: ");
      scanf("%d", &year);
      printf("Enter Month: ");
      scanf("%d", &month);
      
    
    
      char days[7][3] = {"Mon",
                         "Tue",
                         "Wed",
                         "Thu",
                         "Fri",
                         "Sat",
                         "Sun"};
                         
      
      p = strcmp (month, "jan");
      if (p == 0)
      {display(year, month);}
      else
      printf("Please enter a valid month name!");
       
      
      
      
      system("PAUSE");	
      return 0;
    }
    
    
    
    
    display (year, month)
    {
          int *i = 0;
          while (*i != 0);
            printf(" %s %s", month[i], year[i]);
    }
    Last edited by newC72; 06-13-2012 at 01:08 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    For starters, the code you posted has a lot of basic errors:

    Line 5: If no arguments are passed to "main()", you should indicate with "int main(void)"
    Line 11: You should specify which element of the "year" array you want to store the user input
    Line 13: You're scanning for an integer (%d) and storing it in a character array - you should specify which element of the "month" array you want to store the user input
    Line 17: You're using the days of the week as strings, but only declare each string to have 3 elements - you need a fourth to account for the null character at the end of each string
    Line 42: "Display()" should have a return type (void if nothing is being returned)
    Line 42: If a function is defined after "main()", it must be declared before "main()"
    Line 42: Specify data types for the two arguments passed to this function
    Line 44: You are declaring an integer pointer, not an integer - this does not appear to be what you want
    Line 45: There are so many problems with this single line of code, I won't even list them
    Line 46: You're trying to print simple arrays (including an integer array as strings)

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    The trick to writing a program like this isn't to sit down and start writing code. Develop the process by hand, on paper, first. Write down everything you need to do. Then write down step by step how you would do it. Use this to begin developing your code. This process will help you determine the structure and logical flow of your program.

    Code:
    int i = 0  // declares an integer named "i" and assigns it a value of zero
    int *i = 0 // declares a pointer (named "i") to an integer and assigns it a value of zero (not good practice)
    Salem likes this.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    11
    Thanks.. I've just started with the C language..

    Q.

    int i = 0 and int *i = 0

    How do these two differ?

    Could you give me some directions about making this prog...?

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    You were given quite a bit of directions above. You will have to trace your own path to reach the end. Here you will get guidance when you get off track. Did you write the code you pasted above? Do you fully understand it? i.e. understand the role and usage of all the functions? Probably not. You have to understand how C works, since you are just starting, and what is the mechanic behind displaying/making a calendar disregarding the chosen language.

  8. #8
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by Matticus View Post
    Line 5: If no arguments are passed to "main()", you should indicate with "int main(void)"
    I come from a C++ background, so my C isn't quite up to snuff, but is this still true? I thought it was only some ancient C dialect that required this.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Quote Originally Posted by antred View Post
    I come from a C++ background, so my C isn't quite up to snuff, but is this still true? I thought it was only some ancient C dialect that required this.
    My own knowledge of 'C' comes nowhere near that of the experts on this site, so I'm not sure if it's required (and if so, the consequences of omitting it) - but it's definitely good practice to be explicit in this regard.

    (C) The difference between int main() and int main(void)

    A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:

    int foo();

    In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments.
    (link)
    antred likes this.

  10. #10
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Ok, that's good enough for me.

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    Quote Originally Posted by antred View Post
    Ok, that's good enough for me.
    Look at Salem's avatar :P

  12. #12
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by mariostg View Post
    Look at Salem's avatar :P
    Oh, but Salem is making a very different point. void main() == bad, int main() == good. Whereas I was talking about int main(void) vs int main().
    Matticus likes this.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    @antred. Yes, my bad :-). I even double checked before I post, yet my brain did not see it that way this time.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Matticus
    Line 5: If no arguments are passed to "main()", you should indicate with "int main(void)"
    I agree, but this is not actually an error per se (i.e., "it's definitely good practice to be explicit in this regard"). That article is correct, but in a function definition, an empty parameter identifier list is equivalent to a parameter type list with void as the only item. So, unless you have a declaration of main other than its definition, you're actually fine... except that this feature is supposedly obsolescent (but I don't see how it will be removed in practice when compatibility with C++ is useful).
    Matticus likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    I generated the list of observations, then headed the list with the verbiage that included the word "errors" - I did realize that this point was not precisely an "error" though. I tried implying so by saying "you should" as opposed to "you must," but I should have been more clear. Good catch =)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with calendar in C
    By tastyTreeHUGGER in forum C Programming
    Replies: 3
    Last Post: 11-11-2010, 10:20 AM
  2. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 07:17 AM
  3. C++ calendar HELP!!!
    By chocha7 in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2004, 08:06 AM
  4. Calendar ALMOST DONE!!!
    By ftblstr2319 in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2004, 01:30 AM
  5. calendar
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-10-2002, 05:10 PM

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