I am a beginner. I make a little program:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define BOOL bool
int main(void)
{
int month, date, day = 0, week = 0;
int Jan=31, Feb=28, Mar=31, Apr=30, May=31, Jun=30, Jul=31, Aug=31, Sep=30, Oct=31, Nov=30, Dec=31;
int cmonth[12]={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
int k;
char restart='e';
bool DoYouWantToRestart = true;
printf("day searching system (2023)\n");
while (DoYouWantToRestart == true)
{
k=0;
month=0;date=0;
restart = 'e';
day=0;week=0;
printf("what month is it:");
scanf("%d",&month);
printf("what date is it:");
scanf("%d",&date);
printf("loading......\n");
if(month<1 || month>12)
printf("error!\n");
else if (date>31)
printf("error!\n");
else if ((month==4 || month==6 || month==9 || month==11) && date>30)
printf("error!\n");
else if (month==2 && date>28)
printf("error!\n");
else
{
for(k=0;k<month-1;k++)
{
day=day+cmonth[0+k];
}
day=day+date;
week = day % 7 - 1;
switch(week)
{
case 0:
printf("%d/%d is on Sunday\n",month,date);
break;
case 1:
printf("%d/%d is on Monday\n",month,date);
break;
case 2:
printf("%d/%d is on Tuesday\n",month,date);
break;
case 3:
printf("%d/%d is on Wednesday\n",month,date);
break;
case 4:
printf("%d/%d is on Thursday\n",month,date);
break;
case 5:
printf("%d/%d is on Friday\n",month,date);
break;
case 6:
printf("%d/%d is on Saturday\n",month,date);
break;
}
}
while(restart == 'e')
{
printf("restart?(y/n):");
scanf("%s",&restart);
if(restart == 'y' || restart == 'Y')
DoYouWantToRestart=true;
else if (restart == 'n' || restart == 'N')
DoYouWantToRestart=false;
else
{
printf("error\n");
restart = 'e';
}
}
}
printf("Have a good day!\n");
return 0;
}
It can work when i use xcode, but not in terminal
xcode:
terminal:
I have tried some online C program tools, it's also works. But not work at Mac terminal.
The correct answer is Thursday, but when i do second time on terminal, it shows Monday.
Is it a bug or i made any mistake on it?