Thread: Homework: Finding Next Day Using Structures

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    Homework: Finding Next Day Using Structures

    Ok, for my Sunday homework I have to find the next day of the year using structures. Now, I can get the structure to read the entered date (tested that with a simple scanf to printf entered data), but I need to get it to be able to tell that it is the end of the month and print out the next month when necessary.

    Here is what I got so far:

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    
    struct Date{
       int month;
       int day;
       int year;
    };
    
    int main(){
    
       int max_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       int i;
       struct Date next;
    
       printf("This program calculates the next day.\n");
       printf("Please enter the current date in format dd/mm/yyyy: ");
       scanf("%d%*c%d%*c%d", &next.month, &next.day, &next.year);
    
       for (i = next.month-1; i < 12;)
        if (next.day = max_days[i])
           next.month++;
           next.day = 1;
    
       printf("Tomorrows date is %d/%d/%d", next.month, next.day, next.year);
    
       return 0;
    
    }
    As you can see, I am trying to use an array for the max day of each month (excluding Leap Year for now).

    Where I use the entered month to = 'i'. Then I use 'i' as the position in the array to see if next.day is = to the max day of that position of the array.

    I am having no compiler issues with GCC using -w or -Wall, but after entering the current day the program stops working and doesn't go beyond the input.

    commiedic@localhost Project18$ gcc -o -Wall Exercise4.c
    commiedic@localhost Project18$ gcc -o -w Exercise4.c
    commiedic@localhost Project18$ gcc -w Exercise4.c
    commiedic@localhost Project18$ make Exercise4
    make: `Exercise4' is up to date.
    commiedic@localhost Project18$ make Exercise4
    cc Exercise4.c -o Exercise4
    commiedic@localhost Project18$ ./Exercise4
    This program calculates the next day.
    Please enter the current date in format dd/mm/yyyy: 07/21/2013
    ^C - Accidently hit ctrl+c to copy and it made the program quit, but usually it just sticks here.
    commiedic@localhost Project18$

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You made a bunch of typos
    Code:
       for (i = next.month-1; i < 12; i++) {
        if (next.day == max_days[i]) {
           next.month++;
           next.day = 1;
       }
    }
    Although, you need to do something similar for next year, when 31/12/13 wraps around to 1/1/14.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    this is what i get

    This program calculates the next day.
    Please enter the current date in format dd/mm/yyyy: 21/07/2013
    Tomorrows date is 21/1/2013Press any key to continue...

    it is just resetting the month back to jan, didnt stick for me at all!

    (using pelles c ide)

    make sure you enclose the section you want to happed after an IF statement in curley braces, your is always setting month to 1 despite the if statement.

    also month should increment unless it is dec, then reset to 1

    second you need an else, for when it isnt the end of the month!

    MAIN needs to be
    Code:
    int main(void)
    for the "new style" of compiliers!

    and on your IF statement, you need a ==, not a = when compairing!

    start there, then we will fix the rest after that
    Last edited by Crossfire; 07-21-2013 at 08:11 PM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
     if (next.day = max_days[i])
    that is assignment and you meant comparison!

    I am not sure what are the stars in your scanf.
    I tried with this
    Code:
        int a,b,c;
        scanf("%d/%d/%d", &a,&b,&c);
        printf("%d, %d, %d\n", a, b, c);
        return 0;
    and it worked by inputing 12/26/35
    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
    Jun 2013
    Posts
    70
    Yea, I just realize that right as you posted lol.

    Meant for it to be mm/dd/yyyy. Didn't catch the "==" though, rookie mistake.

    updating code now.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ok, this is my updated code hopefully with typos fixed and not using %*c to ignore the '/'s'. Though it is still sticking after date input.

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    
    struct Date{
       int month;
       int day;
       int year;
    };
    
    int main(){
    
       int max_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       int i;
       struct Date next;
    
       printf("This program calculates the next day.\n");
       printf("Please enter the current date in format mm/dd/yyyy: ");
       scanf("%d/%d/%d", &next.month, &next.day, &next.year);
    
       for (i = next.month-1; i < 12;)
        if (next.day == max_days[i])
           next.month++;
           next.day = 1;
        
    
       printf("Tomorrows date is %d/%d/%d", next.month, next.day, next.year);
    
       return 0;
    
    }

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    your FOR statement is your main prob for the error in the next day, it is taking i and subtracting over and over, you should set i to the current month, not making a for statement!

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    NOTE: In C, the spaces and indentation does NOT effect how the code runs!

    Please add the suggested braces.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by stahta01 View Post
    NOTE: In C, the spaces and indentation does NOT effect how the code runs!

    Please add the suggested braces.

    Tim S.
    Quote Originally Posted by Crossfire View Post
    your FOR statement is your main prob for the error in the next day, it is taking i and subtracting over and over, you should set i to the current month, not making a for statement!

    Sorry, I missed the braces in the code provide above. Thanks, I have been doing "for" statements so much in the last couple of weeks, I didn't think to not use it lol.

    Here is my code that works without implementing "leap year" and "next year" so far. I am pretty sure I can do a couple of "if else" statements for those.

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    
    struct Date{
       int month;
       int day;
       int year;
    };
    
    int main(){
    
       int max_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       int i;
       struct Date next;
    
       printf("This program calculates the next day.\n");
       printf("Please enter the current date in format mm/dd/yyyy: ");
       scanf("%d/%d/%d", &next.month, &next.day, &next.year);
    
       i = next.month-1;   
    
        if (next.day == max_days[i]){
           next.month++;
           next.day = 1;
           }
        else {
           next.day = next.day + 1;
        }
        
       
        
    
       printf("Tomorrows date is %d/%d/%d\n", next.month, next.day, next.year);
    
       return 0;
    
    }
    print out:

    commiedic@localhost Project18$ ./Exercise4
    This program calculates the next day.
    Please enter the current date in format mm/dd/yyyy: 01/31/2013
    Tomorrows date is 2/1/2013
    commiedic@localhost Project18$ ./Exercise4
    This program calculates the next day.
    Please enter the current date in format mm/dd/yyyy: 01/30/2013
    Tomorrows date is 1/31/2013
    commiedic@localhost Project18$

  10. #10
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    If you want, I think you can put the body of your code in a do-while loop and that way you wouldn't have to run it manually each time you wanna see the next day.

    Like,
    Code:
    do {/* code in here*} while (something)
    Does getch work? Like, you could have getch exit parameter or something? Idk, hand waving solves everything :P

  11. #11
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by MutantJohn View Post
    If you want, I think you can put the body of your code in a do-while loop and that way you wouldn't have to run it manually each time you wanna see the next day.

    Like,
    Code:
    do {/* code in here*} while (something)
    Does getch work? Like, you could have getch exit parameter or something? Idk, hand waving solves everything :P
    haha, I am not sure. Don't know if I wanna go that far with it yet.

    Though, I am thinking I need to do a "do-while" loop anyways in order to do one separate "if-else" chain for leap year and non-leap year. Trying to figure that out now.

    starting with this:
    Code:
       printf("Is the current year a leap year [y/n]: ");
       scanf("%c", &leap_year);

  12. #12
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ok, I got leap year down (without do while loop =P). Now to tackle 12/31/2013 - 01/01/2014

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    
    struct Date{
       int month;
       int day;
       int year;
    };
    
    int main(){
    
       int max_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       int i;
       char leap_year;
       struct Date next;
    
       printf("This program calculates the next day.\n");
       printf("Is the current year a leap year [y/n]: ");
       scanf("%c", &leap_year);
       printf("Please enter the current date in format mm/dd/yyyy: ");
       scanf("%d/%d/%d", &next.month, &next.day, &next.year);
    
       i = next.month-1;   
    
        if (leap_year == 'y'){
           max_days[1]=29;
        }
        else {
           max_days[1]=28;
        }
    
    
        if (next.day == max_days[i]){
           next.month++;
           next.day = 1;
           }
        else {
           next.day++;
        }
        
       
        
    
       printf("Tomorrows date is %d/%d/%d\n", next.month, next.day, next.year);
    
       return 0;
    
    }

  13. #13
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Well, having the user input if it's a leap year is one method of doing it, a much better way would be to use a leap year algorithm.

    This should be a pretty easy algorithm to build in C.
    Code:
    if (year%400==0) {}
    else if (year%100==0) {}
    else if (year%4==0) {}
    else...
    I'ma copy-paste the original from the leap year Wikipedia page:

    if year is divisible by 400 then is_leap_yearelse if year is divisible by 100 then not_leap_yearelse if year is divisible by 4 then is_leap_yearelse not_leap_year

  14. #14
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by MutantJohn View Post
    Well, having the user input if it's a leap year is one method of doing it, a much better way would be to use a leap year algorithm.

    This should be a pretty easy algorithm to build in C.
    Code:
    if (year%400==0) {}
    else if (year%100==0) {}
    else if (year%4==0) {}
    else...
    I'ma copy-paste the original from the leap year Wikipedia page:

    if year is divisible by 400 then is_leap_yearelse if year is divisible by 100 then not_leap_yearelse if year is divisible by 4 then is_leap_yearelse not_leap_year
    I can try to implement that a little later (got other homework to do), but here is my code so far with everything working as is.

    Thanks for all the help!

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    
    struct Date{
       int month;
       int day;
       int year;
    };
    
    int main(){
    
       int max_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       int i;
       char leap_year;
       struct Date next;
    
       printf("This program calculates the next day.\n");
       printf("Is the current year a leap year [y/n]: ");
       scanf("%c", &leap_year);
       printf("Please enter the current date in format mm/dd/yyyy: ");
       scanf("%d/%d/%d", &next.month, &next.day, &next.year);
    
       i = next.month-1;   
    
        if (leap_year == 'y'){
           max_days[1]=29;
        }
        else {
           max_days[1]=28;
        }
    
        if (next.month == 12 && next.day == max_days[i]){
             next.month = 1;
             next.day = 1;
             next.year++;
        }
        else if (next.day == max_days[i]){
            next.month++;
            next.day = 1;
        }
        else {
           next.day++;
        }
        
       
        
    
       printf("Tomorrows date is %d/%d/%d\n", next.month, next.day, next.year);
    
       return 0;
    
    }
    Print Out:

    commiedic@localhost Project18$ ./Exercise4
    This program calculates the next day.
    Is the current year a leap year [y/n]: n
    Please enter the current date in format mm/dd/yyyy: 02/28/2013
    Tomorrows date is 3/1/2013 //Without Leap Year
    commiedic@localhost Project18$ ./Exercise4
    This program calculates the next day.
    Is the current year a leap year [y/n]: n
    Please enter the current date in format mm/dd/yyyy: 12/31/2013
    Tomorrows date is 1/1/2014 //Next Year
    commiedic@localhost Project18$ ./Exercise4
    This program calculates the next day.
    Is the current year a leap year [y/n]: y
    Please enter the current date in format mm/dd/yyyy: 02/28/2013
    Tomorrows date is 2/29/2013 //With Leap Year
    commiedic@localhost Project18$ ./Exercise4
    This program calculates the next day.
    Is the current year a leap year [y/n]: n
    Please enter the current date in format mm/dd/yyyy: 05/13/2013
    Tomorrows date is 5/14/2013 //Random Date
    Will check back in a little bit for any more suggestions. The assignment is due at 11:59pm, so I will probably check back in a hour or so if you guys have any more tips and to possibly plug in that algorithm for leap year.

    Thanks!

    EDIT:

    I went ahead and added the "if-else" chain to check for leap year instead of asking the user. I hope this is what you had in mind.

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    
    struct Date{
       int month;
       int day;
       int year;
    };
    
    int main(){
    
       int max_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       int i;
       struct Date next;
    
       printf("This program calculates the next day.\n");
       printf("Please enter the current date in format mm/dd/yyyy: ");
       scanf("%d/%d/%d", &next.month, &next.day, &next.year);
    
       i = next.month-1;   
    
        if (next.year % 400==0){
           max_days[1]=29;
        }
        else if (next.year % 100==0) {
           max_days[1]=28;    
        }
        else if (next.year % 4==0) {
           max_days[1]=29;    
        }
    
    
        if (next.month == 12 && next.day == max_days[i]){
             next.month = 1;
             next.day = 1;
             next.year++;
        }
        else if (next.day == max_days[i]){
            next.month++;
            next.day = 1;
        }
        else {
           next.day++;
        }
        
       
        
    
       printf("Tomorrows date is %d/%d/%d\n", next.month, next.day, next.year);
    
       return 0;
    
    }
    Last edited by Cameron Taylor; 07-21-2013 at 09:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2012, 05:44 PM
  2. Replies: 6
    Last Post: 02-06-2012, 09:38 AM
  3. Replies: 5
    Last Post: 04-03-2011, 02:05 PM
  4. Replies: 3
    Last Post: 11-25-2010, 02:51 AM
  5. Homework problem...structures or arrays?
    By tortan in forum C++ Programming
    Replies: 21
    Last Post: 08-30-2006, 01:26 AM