Thread: Need some help 1-month calendar almost working

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    15

    Need some help 1-month calendar almost working

    Hello, I am new to C, and I need to create a calendar, where the user inputs the days of the month and what day the calendar starts on, such as Mo, Tu, we, etc, and show a calendar. I have the calendar output the correct number of days with formatting, but do not know how to start on a specific day? I am wondering how to turn the string of characters (we, th) into a number 1-7, I am thinking that would be the solution. Also, I need to show an error message if the starting day is not correct (ex. tg, mi). Here is my code:

    as you can see I have certain parts taken out as a comment, because it did not work.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int
    main()
    {
        int num_days;
        int i;
        char day;
        char day2;
        printf("This prigram displays the calender for a month.\n\n\n");
        printf("Enter number of days in month: \n");
        scanf("%i", &num_days);
        printf("Enter starting day of the week (Su,Mo,Tu,...,Sa): \n");
        scanf("%*[ \n]%c", &day);
        //if (day != "Su" || day != "Mo" || day != "Tu" || day != "We" || day != "Th" || day != "Fr" || day != "Sa" || day != "su" || day != "mo" || day != "tu" || day != "we" || day != "th" || day != "fr" || day != "sa")
          //   printf("That is an invalid day of the week.");
        //else {
        day2 = toupper(day);
        printf("%c\n", day2);
        //}
        printf("  Su  Mo  Tu  We  Th  Fr  Sa\n");
    
    
        for (i = 1 ;  i < num_days + 1 ;  ++i ){
            printf("%3i ", i);
        if(i % 7  ==  0)
            printf("\n");
        }
        printf("\n");
    
    
    }
    Here is my latest attempt, which still does not work:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int
    main()
    {
        int num_days;
        char day;
        char day2;
        char Su, su;
        char Mo, mo;
        char Tu, tu;
        char We, we;
        char Th, th;
        char Fr, fr;
        char Sa, sa;
    
    
        printf("This program displays the calender for a month.\n\n\n");
        printf("Enter number of days in month: \n");
        scanf("%i", &num_days);
    
    
        printf("Enter starting day of the week (Su,Mo,Tu,...,Sa): \n");
        scanf("%*[ \n]%c", &day);
    
    
        day2 = toupper(day);
    
    
        printf("%c\n", day2);
        printf("  Su  Mo  Tu  We  Th  Fr  Sa\n");
    
    
        if (day == Su || day == su)
            printf("");
        else if (day == Mo || day == mo)
            printf("  ");
        else if (day == Tu || day == tu)
            printf("    ");
        else if (day == We || day == we)
            printf("      ");
        else if (day == Th || day == th)
            printf("        ");
        else if (day == Fr || day == fr)
            printf("          ");
        else if (day == Sa || day == sa)
            printf("            ");
        int count;
            for (count = 1 ;  count < num_days + 1 ;  count++ ){
            printf("%3d ", count);
        if(count % 7  ==  0)
            printf("\n");
            }
    }
    Thanks for the help!!!

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    One thing, int needs to be on the same line as main, ex int main() also you have char day but you are comparing it to 2 characters. you need to either change day to a string or change your days to T, M ,W H, F... ect that way it will be one character.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Actually there is nothing wrong with the int being on a separate line from the main(), C does not care. Any of the following are acceptable:
    Code:
    int main(void) // The most used version with no arguments.
    int
    main(void)  // Used by several Documentation systems to make locating the function names easier.
    int
    main
    (void)   // Rarely used but legal C.

    Second the following:
    Code:
    char Su, su;
    Is creating two char variables one named Su, the other name su. These variables can hold only one character. You are however using these variables improperly. You never initialize these variables to any value before you try to compare them to your user input.

    Also your variable day can also only hold one character. As stated by camel-man, you should be using C-strings or only ask for a single character input.

    Jim

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    Ok thanks, I changed everything to S, M, T, etc, but I am still not seeing any progress, could you please show a sample code of what I should change with respect to starting on a certain day?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What I've done is assign day variables, with values - similar to what you were thinking about, above. Sun = 0, Mon = 1, Tue = 2, etc.

    Then before the first day, do a bit of arithmetic to get the correct starting place:

    Month starts on Tuesday, so blanks to be printed before the first day equals 2 * width of each day, in chars.

    A bit simplistic, but it works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One month calendar
    By danieldcc in forum C Programming
    Replies: 11
    Last Post: 07-13-2011, 03:18 PM
  2. Calendar not working properly?
    By kawk in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2007, 01:15 PM
  3. 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
  4. getting the next day's day, month, year etc
    By underthesun in forum C Programming
    Replies: 3
    Last Post: 02-17-2005, 07: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, 08:35 AM

Tags for this Thread