#How can i make it cleaner

37 messages · Page 1 of 1 (latest)

calm path
#

#include <stdio.h>

void readDate(int* day, int* month, int* year, int flag)
{
printf("Enter date %d (DD.MM.YYYY): ", flag);
scanf("%d.%d.%d", day, month, year);
}

void printDate(int flag, int day, int month, int year)
{
printf("Date %d: %02d.%02d.%d\n", flag, day, month, year);
}

int checkSeason(int day, int month)
{
switch(month)
{
case 3:
{
if(day < 21)
{
return 0;
}
else
{
return 1;
}
}
case 4:
case 5:
{
return 1;
}
case 6:
{
if(day < 21)
{
return 1;
}
else
{
return 2;
}
}
case 7:
case 8:
{
return 2;
}
case 9:
{
if(day < 23)
{
return 2;
}
else
{
return 3;
}
}
case 10:
case 11:
{
return 3;
}
case 12:
{
if(day < 21)
{
return 3;
}
else
{
return 0;
}
}
case 1:
case 2:
{
return 0;
}

default:
{
  return 5;
}

}

}

void printResults(int seasonCheck, int flag)
{
if(seasonCheck == 0)
{
printf("Date %d is in Winter", flag);
}
if(seasonCheck == 1)
{
printf("Date %d is in Spring", flag);
}
if(seasonCheck == 2)
{
printf("Date %d is in Summer", flag);
}
if(seasonCheck == 3)
{
printf("Date %d is in Autumm", flag);
}
if(seasonCheck == 5)
{
printf("ERROR");
}
printf("\n");
}

int main()
{
int day, month, year;
int day2, month2, year2;
int flag = 1;
int seasonCheck;

readDate(&day, &month, &year, flag);
flag++;
readDate(&day2, &month2, &year2, flag);

flag = 1;
printDate(flag, day, month, year);
flag++;
printDate(flag, day2, month2, year2);

flag = 1;
seasonCheck = checkSeason(day, month);
printResults(seasonCheck, flag);
flag++;
seasonCheck = checkSeason(day2, month2);
printResults(seasonCheck, flag);

return 0;
}

tribal flare
#

discord kinda fucks up the rendering

tribal flare
#

switch statement on printResults btw

#

also use enums

tribal flare
#

which is like every 4th year but not every 100th year but every 400th year

calm path
#

ye

#

i didnt think that deeply about it

tribal flare
#

just improve on the read date function to fix faulty data

calm path
#

okay imma try

tribal flare
#

I don't see why you need a flag variable in the main function if you aren't gonna do a loop

calm path
#

bc i use the function 2 times for both dates

tribal flare
#

yes

#

you could write 1 and 2

calm path
#

ye

#

thats true

#

idk

tribal flare
#

you already write 1 and 2 when calling the checkSeason function

calm path
#

in my shool my teacher said u should use a variable or a symbolic constant for everything and not write numbers into the programm

tribal flare
#

mm idk about this case

calm path
#

would it be better like that?

tribal flare
#

if you are gonna make it into an array sure

tribal flare
calm path
#

ye i was thinking that but it would be kinda hard to do that

tribal flare
#

it sure is a lot of cases, I would make a while (1) loop and an break statement

calm path
#

hmm

#

ye

tribal flare
#

I'm unsure if that's the best thing to do

calm path
tribal flare
#

idk

#

like 2 years maybe? My memory isn't great.

calm path
#

okay

short thunder
#

@calm path

#
#include <stdio.h>
void readDate(int* day, int* month, int* year, int flag) {
    printf("Enter date %d (DD.MM.YYYY): ", flag);
    scanf("%d.%d.%d", day, month, year);
}
void printDate(int flag, int day, int month, int year) {
    printf("Date %d: %02d.%02d.%d\n", flag, day, month, year);
}
int checkSeason(int day, int month) {
    switch(month) {
        case 3: return (day < 21) ? 0 : 1;
        case 4: case 5: return 1;
        case 6: return (day < 21) ? 1 : 2;
        case 7: case 8: return 2;
        case 9: return (day < 23) ? 2 : 3;
        case 10: case 11: return 3;
        case 12: return (day < 21) ? 3 : 0;
        case 1: case 2: return 0;
        default: return 5;
    }
}
 
void printResults(int seasonCheck, int flag) {
    if(seasonCheck == 0) printf("Date %d is in Winter", flag);
    else if(seasonCheck == 1) printf("Date %d is in Spring", flag);
    else if(seasonCheck == 2) printf("Date %d is in Summer", flag);
    else if(seasonCheck == 3) printf("Date %d is in Autumn", flag);
    else if(seasonCheck == 5) printf("ERROR");
    printf("\n");
}
int main() {
    int day, month, year, day2, month2, year2, flag = 1, seasonCheck;
    readDate(&day, &month, &year, flag++);
    readDate(&day2, &month2, &year2, flag);
    flag = 1;
    printDate(flag++, day, month, year);
    printDate(flag, day2, month2, year2);
    flag = 1;
    seasonCheck = checkSeason(day, month);
    printResults(seasonCheck, flag++);
    seasonCheck = checkSeason(day2, month2);
    printResults(seasonCheck, flag);
    return 0;
}
tribal flare