# Thread: Homework: Finding Next Day Using Structures

1. ## 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. 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. 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

4. 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

5. 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. 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. 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. NOTE: In C, the spaces and indentation does NOT effect how the code runs!

Tim S.

9. Originally Posted by stahta01
NOTE: In C, the spaces and indentation does NOT effect how the code runs!

Tim S.
Originally Posted by Crossfire
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. 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. Originally Posted by MutantJohn
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. 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. 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...```

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. Originally Posted by MutantJohn
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...```

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:

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;

}```