Like Tree5Likes
  • 1 Post By CommonTater
  • 1 Post By nonoob
  • 3 Post By quzah

One month calendar

This is a discussion on One month calendar within the C Programming forums, part of the General Programming Boards category; Hello, I'm doing an exercise where i need to print on the screen a one month calendar. My code is ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    46

    One month calendar

    Hello, I'm doing an exercise where i need to print on the screen a one month calendar. My code is mostly done except I don't know how to indent for the first day of the month so it looks in following format (user enter amount of days in month and first day of month)

    indent1 2 3 4
    4 5 6 7 8 9 10

    So I don't know how to start the 1 in the middle there....my code posted below...
    Code:
    #include <stdio.h>
    
    int main (void) {
    
    int i, days, dayofweek;
    printf ("Please enter the number of days in the month: ");
    scanf ("%d", &days);
    printf ("Please enter first day of week (1=sun, 7=sat): ");
    scanf ("%d", &dayofweek);
    
    for (i = 1; i <= days; i++) {
     
    printf ("%4d", i);
    if (i == 7 || i == 14 || i == 21 || i == 28)
    printf ("\n"); 
     
    }
    printf ("\n"); 
    return 0;
    
    }
    Last edited by danieldcc; 07-12-2011 at 09:20 PM.

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    ok. i figured it out...Not sure if best way of doing it. Any suggestions?

    Code:
    #include <stdio.h>
    
    int main (void) {
    
    int i, j, days, dayofweek;
    printf ("Please enter the number of days in the month: ");
    scanf ("%d", &days);
    printf ("Please enter first day of week (1=sun, 7=sat): ");
    scanf ("%d", &dayofweek);
    
    
    for (j = 0; j < dayofweek; j++)
      printf ("    ");
    for (i = 1; i <= days; i++) {
    j++;
    printf ("%4d", i);
    if (j == 7 || j == 14 || j == 21 || j == 28)
    printf ("\n"); 
     
    }
    printf ("\n"); 
    return 0;
    
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if (j == 7 || j == 14 || j == 21 || j == 28)
    What do those numbers have in common? Can you think of an operator that would help you out with what they have in common?


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

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    if (j % 7 == 0) or could i use maybe just if (j % 7) ? is that what you mean? I was also hoping for some more input on an easier way to indent other than

    Code:
    for (j = 0; j < dayofweek; j++)
      printf ("    ");

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by danieldcc View Post
    if (j % 7 == 0) or could i use maybe just if (j % 7) ?
    Well, considering 0 if false and any nonzero is true it would be:
    Code:
    if(!(j%7))
    Quote Originally Posted by danieldcc View Post
    I was also hoping for some more input on an easier way to indent other than
    As for spacing you could try doing tabs, e.g. \t but for console output unless you want to get into goto(x,y) then spaces and tabs are what you have for spacing.

    Also, I think your program may be 1 day off when it prints, could be wrong though.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    cool. tks. Yes it was one off...I modified it, but that's just details...

    Tks for the help

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    It gives you an appreciation of the ASCII artists who slaved away when dinosaurs still roamed the earth.....*cough*quzah, Salem,Tater*cough*
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    It gives you an appreciation of the ASCII artists who slaved away when dinosaurs still roamed the earth.....*cough*quzah, Salem,Tater*cough*
    Hey, don't put me on that list! I at least had enough sense to wait until after the astroid hit...

    http://www.pbs.org/wgbh/evolution/ex.../asteroid.html
    AndrewHunter likes this.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    Quote Originally Posted by CommonTater View Post
    Hey, don't put me on that list! I at least had enough sense to wait until after the astroid hit...
    So, you never learned COBOL?

    BP- Cobol Programmer Dinosaur Mug from Zazzle.com

    Tim S.
    PS: I never took a class in COBOL; and I hope to never program in it.
    Last edited by stahta01; 07-13-2011 at 11:14 AM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stahta01 View Post
    So, you never learned COBOL?

    BP- Cobol Programmer Dinosaur Mug from Zazzle.com

    Tim S.
    PS: I never took a class in COBOL; and I hope to never program in it.
    Nope, no Cobol, Forth or Prolog... and no Flintstone either.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by danieldcc View Post
    if (j % 7 == 0) or could i use maybe just if (j % 7) ? is that what you mean? I was also hoping for some more input on an easier way to indent other than

    Code:
    for (j = 0; j < dayofweek; j++)
      printf ("    ");
    Easier way to indent without loop
    Code:
    printf("%*s", dayofweek * 4, "");
    tabstop likes this.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    It gives you an appreciation of the ASCII artists who slaved away when dinosaurs still roamed the earth.....*cough*quzah, Salem,Tater*cough*
    Code:
                                     _.-_
                                 __.'  o "-.
                        ___----""       ,,,'
                   __--"             _-_ ^^;
           ____--""  ___         _-\"   `""
    ..--===-------"""   "(   __-"  ,}
                        .'_/"\
                      .'/"  `\`._
                      "-_c    "  '
    I stole it.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Year, month and day
    By Newbie999 in forum C Programming
    Replies: 15
    Last Post: 12-01-2006, 09:17 AM
  2. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 06:17 AM
  3. getting number for Month
    By Jasonymk in forum C# Programming
    Replies: 2
    Last Post: 08-09-2005, 03:35 AM
  4. getting the next day's day, month, year etc
    By underthesun in forum C Programming
    Replies: 3
    Last Post: 02-17-2005, 06:43 AM
  5. in one month i shall return.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 02-05-2002, 07:35 AM

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